LoginSignup
0
1

More than 3 years have passed since last update.

ansibleで複数ユーザ管理(アクセス権付与)をやってみた

Last updated at Posted at 2020-01-01

前提

  • すでに仮想環境(ターゲットホスト)をvagrantで構築済である。
  • ansibleはインストールしているが実行させるプレイブックがない。

やりたいこと

user01は、user02配下にアクセス権限はあるが、
user02は、user01配下にアクセス権限がない状態を実現したい。

グループsample2に、user01を追加する。

[root@websvr ~]# usermod -aG sample2 user01

確認

[root@websvr ~]# groups user01
user01 : sample1 sample2
[root@websvr ~]# groups user02
user02 : sample2

また、user01に対するuser02配下のアクセス権限はファイルやディレクトリのRead権限のみとしたいので、
user02ディレクトリに対して、以下を実施する。

[root@websvr ~]# chmod 750 /home/user02
[root@websvr ~]# ll /home/
total 4
drwx------.  3 user01  sample1   78 Jan  1 14:56 user01
drwxr-x---.  3 user02  sample2   78 Jan  1 14:56 user02
drwx------. 17 vagrant vagrant 4096 Dec 31 19:31 vagrant

グループに対して、xを付与してやらないと、user01/home/user02に移動出来ない。

user01user02作成のファイルを削除出来ないことを確認出来た。

[user01@websvr user02]$ whoami
user01
[user01@websvr user02]$ pwd
/home/user02
[user01@websvr user02]$ ll
total 0
-rw-r--r--. 1 user02 sample2 0 Jan  1 15:03 test.txt
[user01@websvr user02]$ rm test.txt
rm: remove write-protected regular empty file ‘test.txt’? y
rm: cannot remove ‘test.txt’: Permission denied

このようなことをansibleで実現したい。

公開鍵の登録

コントロールノード(ホスト環境)から、ターゲットノード(ゲスト環境)にsshでパスワード無しにログインする設定を実施する。
コントロールノードで作成した公開鍵をターゲットノードに登録する。

ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key ($HOME/.ssh/id_rsa):
$HOME/.ssh/id_rsa already exists.
Overwrite (y/n)? n

すでに作成してあったので、中断する。
公開鍵をターゲットノードに登録する。

ssh-copy-id vagrant@192.168.33.10
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "$HOME/.ssh/id_rsa.pub"
The authenticity of host '192.168.33.10 (192.168.33.10)' can't be established.
ECDSA key fingerprint is SHA256:mjGym7gkqWjPvW2JXhKjqWl4XC6wuhgNIukldSVtkFk.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
vagrant@192.168.33.10's password:

Number of key(s) added:        1

Now try logging into the machine, with:   "ssh 'vagrant@192.168.33.10'"
and check to make sure that only the key(s) you wanted were added.

パスワード無しでログイン出来ることを確認する。

ssh vagrant@192.168.33.10
Last login: Tue Dec 31 18:41:18 2019 from 10.0.2.2
[vagrant@websvr ~]$ exit
ログアウト
Connection to 192.168.33.10 closed.

ansible.cfgの設定

コントロールノード側でansible.cfgを用意する。
ansible.cfgは個別の設定をする事があるため、以下に作成する。

pwd
$HOME/Desktop/workspace/ansible/ansible-study
touch ansible.cfg

.ansible.cfgに以下の通り設定する。

[defaults]
forks = 10
log_path = $HOME/.ansible/ansible.log
host_key_checking = False
gathering = smart
inventory = ./inventory.ini
remote_user = vagrant
private_key_file = /path/vagrant_private_key

[ssh_connection]

上記のprivate_key_fileは以下のIdentityFileから取得する。

cd $HOME/Desktop/workspace/vagrant
vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /path/vagrant_private_key
  IdentitiesOnly yes
  LogLevel FATAL

インベントリの準備

ターゲットノードの情報をinventory.iniに記載する。

touch inventory.ini
cat inventory.ini
[test_servers]
192.168.33.10

ansibleコマンドの実行

ターゲットノードにpingコマンドを実行する。

$ ansible -i inventory.ini test_servers -m ping -u vagrant
192.168.33.10 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

ansible.cfgに必要な情報を記載しているため、以下のコマンドでも良い。

$ ansible test_servers -m ping
192.168.33.10 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

ターゲットノードのロールバック準備

ターゲットノードをロールバック出来るように、以下を実施する。
プレイブック実行時に失敗しても何度もやり直せる。

$ vagrant sandbox status
[default] Sandbox mode is off

設定を有効化

$ vagrant sandbox on
[default] Starting sandbox mode...
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

設定確認

$ vagrant sandbox status
[default] Sandbox mode is on

プレイブック実行

上記のやりたいことを実現させるプレイブックは以下の通りとなる。

create_user.yml
---
- hosts: test_servers
  become: yes
  vars_files:
    - user_list.yml
  tasks:
    - name: Create group
      group:
        name: "{{ item.name }}"
      with_items:
        - "{{ group_list }}"
      when: group_list

    - name: Create user
      user:
        name: "{{ item.name }}"
        group: "{{ item.group }}"
        groups: "{{ item.groups }}"
        password: "{{ item.password }}"
        shell: /bin/bash
        state: present
      with_items:
        - "{{ users_list }}"
      when: users_list

    - name: Permission of directory /home/user02 is '0750'
      file:
        dest: /home/{{ item.name }}
        mode: "0750"
      with_items:
        - "{{ subordinate_authority }}"
user_list.yml
group_list:
  - { name: sample1 }
  - { name: sample2 }

users_list:
  - {
      name: "user01",
      group: "sample1",
      groups: "sample1, sample2",
      password: "{{ 'password'|password_hash('sha512') }}",
      comment: "user01",
    }
  - {
      name: "user02",
      group: "sample2",
      groups: "sample2",
      password: "{{ 'password'|password_hash('sha512') }}",
      comment: "user02",
    }

superior_authority:
  - { name: user01 }

subordinate_authority:
  - { name: user02 }

実行する。

ansible-playbook create_user.yml

PLAY [test_servers] *************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
ok: [192.168.33.10]

TASK [Create group] *************************************************************************************************************************************************************************************************************************
[DEPRECATION WARNING]: evaluating [{'name': 'sample1'}, {'name': 'sample2'}] as a bare variable, this behaviour will go away and you might need to add |bool to the expression in the future. Also see CONDITIONAL_BARE_VARS configuration
toggle.. This feature will be removed in version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
changed: [192.168.33.10] => (item={'name': 'sample1'})
changed: [192.168.33.10] => (item={'name': 'sample2'})

TASK [Create user] **************************************************************************************************************************************************************************************************************************
[DEPRECATION WARNING]: evaluating [{'name': 'user01', 'group': 'sample1', 'groups': 'sample1, sample2', 'password': '$6$cuuPF7HsudjoJ9/v$G1zztM4ZLPs6UJk/fxwgjbOAkXJ32pszjjw.J.M2t.KTlO99Bus2D1AzucT.872WBs7oqp.RfOIEjf187X6cR1', 'comment':
 'user01'}, {'name': 'user02', 'group': 'sample2', 'groups': 'sample2', 'password': '$6$cuuPF7HsudjoJ9/v$G1zztM4ZLPs6UJk/fxwgjbOAkXJ32pszjjw.J.M2t.KTlO99Bus2D1AzucT.872WBs7oqp.RfOIEjf187X6cR1', 'comment': 'user02'}] as a bare variable,
this behaviour will go away and you might need to add |bool to the expression in the future. Also see CONDITIONAL_BARE_VARS configuration toggle.. This feature will be removed in version 2.12. Deprecation warnings can be disabled by
setting deprecation_warnings=False in ansible.cfg.
changed: [192.168.33.10] => (item={'name': 'user01', 'group': 'sample1', 'groups': 'sample1, sample2', 'password': '$6$VpS/Wgs9fy5KDTI8$ZXwfSsnz4jeQ6rAOX4X8HP4xu3ndeVtasHiY0SOaeZoYUvPU3CQQQ4ww3y6VfEJAlS4jJPpXn7rRxUljY.Sc60', 'comment': 'user01'})
[DEPRECATION WARNING]: evaluating [{'name': 'user01', 'group': 'sample1', 'groups': 'sample1, sample2', 'password': '$6$wjPKJca0K2oKgwFY$5JzcugEn1.kc1KXN9XYBJVub9e.AzUT28S4ZOGy.vwKhRVkSB9dqUjUcs/sqRPf8kDG94nqAT3.S8YaA3cime1', 'comment':
 'user01'}, {'name': 'user02', 'group': 'sample2', 'groups': 'sample2', 'password': '$6$wjPKJca0K2oKgwFY$5JzcugEn1.kc1KXN9XYBJVub9e.AzUT28S4ZOGy.vwKhRVkSB9dqUjUcs/sqRPf8kDG94nqAT3.S8YaA3cime1', 'comment': 'user02'}] as a bare variable,
this behaviour will go away and you might need to add |bool to the expression in the future. Also see CONDITIONAL_BARE_VARS configuration toggle.. This feature will be removed in version 2.12. Deprecation warnings can be disabled by
setting deprecation_warnings=False in ansible.cfg.
changed: [192.168.33.10] => (item={'name': 'user02', 'group': 'sample2', 'groups': 'sample2', 'password': '$6$VpS/Wgs9fy5KDTI8$ZXwfSsnz4jeQ6rAOX4X8HP4xu3ndeVtasHiY0SOaeZoYUvPU3CQQQ4ww3y6VfEJAlS4jJPpXn7rRxUljY.Sc60', 'comment': 'user02'})

TASK [Permission of directory /home/user02 is '0750'] ***************************************************************************************************************************************************************************************
changed: [192.168.33.10] => (item={'name': 'user02'})

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
192.168.33.10              : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

ターゲットノードの実行結果は以下の通り。

[root@websvr ~]# ll /home/
total 4
drwx------.  3 user01  sample1   78 Jan  1 15:11 user01
drwxr-x---.  3 user02  sample2   78 Jan  1 15:11 user02
drwx------. 17 vagrant vagrant 4096 Dec 31 19:31 vagrant
[root@websvr ~]# cat /etc/group | grep user01
sample1:x:1002:user01
sample2:x:1003:user01,user02

user02でファイルを作成する。
その後、user01になり、アクセス権を確認する。

[root@websvr ~]# su - user02
[user02@websvr ~]$ touch test.txt
[user02@websvr ~]$ ll
total 0
-rw-r--r--. 1 user02 sample2 0 Jan  1 15:32 test.txt
[user02@websvr ~]$ su - user01
Password: 
Last login: Wed Jan  1 15:32:11 UTC 2020 on pts/0
[user01@websvr ~]$ cd /home/user02/
[user01@websvr user02]$ ll
total 0
-rw-r--r--. 1 user02 sample2 0 Jan  1 15:32 test.txt

ファイルも消せない。

[user01@websvr user02]$ rm test.txt 
rm: remove write-protected regular empty file ‘test.txt’? y
rm: cannot remove ‘test.txt’: Permission denied

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1