B.ターゲットホスト1台、rootで実行
B-1.一般ユーザでターゲットホストに接続してbecome文(sudo)でrootになる
■準備
・一般ユーザ(server_user)がパスワード入力無しでrootになれる権限を付与する
[root@centos7_2 etc]# diff /etc/sudoers{.bak,}
100a101
> server_user ALL=NOPASSWD: ALL
・確認
[root@centos7_2 etc]# su - server_user
最終ログイン: 2022/06/09 (木) 09:09:58 JST日時 pts/0
[server_user@centos7_2 ~]$ whoami;cat /etc/shadow > /dev/null;echo $?
server_user
cat: /etc/shadow: 許可がありません
1
[server_user@centos7_2 ~]$ sudo -s
[root@centos7_2 server_user]# whoami;cat /etc/shadow > /dev/null;echo $?
root
0
B-1-1.copyモジュールの動作確認
[client_user@cetnos7_1 ansible_test]$ cat playbook_B_1_1.yml
---
- hosts: centos72
become: true ←この一行が入る
tasks:
- name: Copy file
copy:
src: /etc/shadow
dest: /tmp/ansible_test/shadow.bak
remote_src: yes
[client_user@cetnos7_1 ansible_test]$ ansible-playbook -u server_user -i inventory.ini playbook_B_1_1.yml
PLAY [centos72] *****************************************************************************************
TASK [Gathering Facts] *****************************************************************************************
ok: [192.168.100.72]
TASK [Copy file] *****************************************************************************************
changed: [192.168.100.72]
PLAY RECAP *****************************************************************************************
192.168.100.72 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@centos7_2 ansible_test]# pwd;ls -lA
/tmp/ansible_test
合計 16
-rw-r--r--. 1 root root 840 6月 9 08:59 shadow.bak
-rw-rw-r--. 1 server_user server_user 5 6月 9 01:06 test.txt
-rw-rw-r--. 1 server_user server_user 5 6月 9 01:06 test.txt.bak
-rwxrwxrwx. 1 server_user server_user 5 6月 9 05:47 test.txt.bak_2
→ターゲットホスト上でroot権限でplaybookが実行された
B-2.初めからrootでターゲットホストに接続する
■準備
・ターゲットホストにssh公開鍵を配置する
[client_user@cetnos7_1 ~]$ whoami; \
scp ~client_user/.ssh/RSA_2048_centos7.dagyah.com_dagyah2.pub \ server_user@192.168.100.72:/tmp
client_user
RSA_2048_centos7.dagyah.com_dagyah2.pub 100% 408 454.5KB/s 00:00
[root@centos7_2 ansible_test]# cat /tmp/RSA_2048_centos7.dagyah.com_dagyah2.pub >> ~root/.ssh/authorized_keys
[root@centos7_2 ansible_test]# chmod 600 ~root/.ssh/authorized_keys
[root@centos7_2 ansible_test]# ls -l ~root/.ssh/authorized_keys
-rw-------. 1 root root 408 6月 9 09:43 /root/.ssh/authorized_keys
→この時点で下記のようにansibleサーバ上の一般ユーザ(client_user)からターゲットホスト上のrootに鍵認証でsshログインできる。
[client_user@cetnos7_1 ~]$ hostname;whoami;echo ----; \
ssh root@192.168.100.72 '{ hostname;whoami; }'
cetnos7_1
client_user
----
centos7_2
root
・sshd_configで「PermitRootLogin without-password」を設定
現在は「PermitRootLogin yes」なので下記のようにansibleサーバ上のrootユーザからターゲットホスト上のrootにパスワード認証でsshログインできる。
[root@cetnos7_1 ssh]# hostname;whoami;echo ----; \
ssh 192.168.100.72 '{ hostname;whoami; }'
cetnos7_1
root
----
root@192.168.100.72's password: ←centos7_2のrootのパスワードを聞かれる
centos7_2
root
ここで「PermitRootLogin without-password」に変更してみる
[root@centos7_2 ssh]# diff sshd_config_20220609 sshd_config
38a39
> PermitRootLogin without-password
[root@centos7_2 ssh]# systemctl restart sshd
ansibleサーバ上のrootユーザからターゲットホスト上のrootにsshログインを試みる。
[root@cetnos7_1 ssh]# hostname;whoami;echo ----;ssh 192.168.100.72 '{ hostname;whoami; }'
cetnos7_1
root
----
root@192.168.100.72's password: ←centos7_2のrootのパスワードを聞かれる
Permission denied, please try again.
→このようにrootへのパスワード認証によるsshログインは拒否される。
一方、rootへの鍵認証によるsshログインは下記の通り許可される。
[client_user@cetnos7_1 ~]$ hostname;whoami;echo ----; \
ssh root@192.168.100.72 '{ hostname;whoami; }'
cetnos7_1
client_user
----
centos7_2
root
※これ以後は、すべてターゲットホストのrootにauthorized_keysを配置して、ansibleサーバからターゲットホストにrootで接続してタスクを実行する。
B-2-1.copyモジュールの動作確認
[client_user@cetnos7_1 ansible_test]$ cat playbook_B_2_1.yml
---
- hosts: centos72
tasks:
- name: Copy file
copy:
src: /etc/shadow
dest: /tmp/ansible_test/shadow.bak2
remote_src: yes
[client_user@cetnos7_1 ansible_test]$ whoami;ansible-playbook -u root -i inventory.ini playbook_B_2_1.yml
client_user
PLAY [centos72] *****************************************************************************************
TASK [Gathering Facts] *****************************************************************************************
ok: [192.168.100.72]
TASK [Copy file] *****************************************************************************************
changed: [192.168.100.72]
PLAY RECAP *****************************************************************************************
192.168.100.72 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@centos7_2 ansible_test]# pwd;ls -lA
/tmp/ansible_test
合計 20
-rw-r--r--. 1 root root 840 6月 9 08:59 shadow.bak
-rw-r--r--. 1 root root 840 6月 9 08:59 shadow.bak2
-rw-rw-r--. 1 server_user server_user 5 6月 9 01:06 test.txt
-rw-rw-r--. 1 server_user server_user 5 6月 9 01:06 test.txt.bak
-rwxrwxrwx. 1 server_user server_user 5 6月 9 05:47 test.txt.bak_2
→B-1-1と同様にターゲットホスト上でroot権限でplaybookが実行された。
B-2-2.groupモジュールの動作確認
B-2-2-1.グループを一つ作成
[client_user@cetnos7_1 ansible_test]$ pwd;whoami
/tmp/ansible_test
client_user
[client_user@cetnos7_1 ansible_test]$ cat playbook_B_2_2_1.yml
---
- hosts: centos72
tasks:
- name: Add Group
group:
name: subgroup1
gid: 2001
...
[root@centos7_2 ~]# grep subgroup1 /etc/group;echo $?
1
[client_user@cetnos7_1 ansible_test]$ ansible-playbook -i inventory.ini -u root playbook_B_2_2_1.yml
PLAY [centos72] **************************************************************
TASK [Gathering Facts] **************************************************************
ok: [192.168.100.72]
TASK [Add Group] **************************************************************
changed: [192.168.100.72]
PLAY RECAP **************************************************************
192.168.100.72 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@centos7_2 ~]# grep subgroup1 /etc/group
subgroup1:x:2001:
B-2-2-2.複数のグループを同時に作成
[client_user@cetnos7_1 ansible_test]$ cat playbook_B_2_2_2_E1.yml
---
- hosts: centos72
tasks:
- name: Add Group
group:
group: name=subgroup3 gid=2003
group: name=subgroup4 gid=2004
...
[client_user@cetnos7_1 ansible_test]$ ansible-playbook -i inventory.ini -u root playbook_B_2_2_2_E1.yml
[WARNING]: While constructing a mapping from /tmp/ansible_test/playbook_B_2_2_2_E1.yml, line 4, column 6, found a duplicate dict key (group). Using last defined value only.
PLAY [centos72] **************************************************************
TASK [Gathering Facts] **************************************************************
ok: [192.168.100.72]
TASK [Add Group] **************************************************************
changed: [192.168.100.72]
PLAY RECAP **************************************************************
192.168.100.72 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@centos7_2 ~]# grep subgroup /etc/group
subgroup1:x:2001:
subgroup4:x:2004: ←subgroup2とsubgroup3が作成されていない!
→いったんsubgroup4を削除して、playbook_B_2_2_1.yml実行直後の状態に戻す。
・失敗例2:変数を使ってグループを複数作成
[client_user@cetnos7_1 ansible_test]$ cat playbook_B_2_2_2_E2.yml
---
- hosts: centos72
vars_files:
- ./usergroup.yml
tasks:
- name: Add Group
group: name={{ usergroup.1.group }} gid={{ usergroup.1.gid }}
group: name={{ usergroup.2.group }} gid={{ usergroup.2.gid }}
group: name={{ usergroup.3.group }} gid={{ usergroup.3.gid }}
group: name={{ usergroup.4.group }} gid={{ usergroup.4.gid }}
...
[client_user@cetnos7_1 ansible_test]$ cat usergroup.yml
usergroup:
1:
group: subgroup1
gid: 2001
2:
group: subgroup2
gid: 2002
3:
group: subgroup3
gid: 2003
4:
group: subgroup4
gid: 2004
[client_user@cetnos7_1 ansible_test]$ ansible-playbook -i inventory.ini -u root playbook_B_2_2_2_E2.yml
[WARNING]: While constructing a mapping from /tmp/ansible_test/playbook_B_2_2_2_E2.yml, line 6, column 6, found a duplicate dict key (group). Using last defined value only.
PLAY [centos72] **************************************************************
TASK [Gathering Facts] **************************************************************
ok: [192.168.100.72]
TASK [Add Group] **************************************************************
changed: [192.168.100.72]
PLAY RECAP **************************************************************
192.168.100.72 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@centos7_2 ~]# grep subgroup /etc/group
subgroup1:x:2001:
subgroup4:x:2004: ←subgroup2とsubgroup3が作成されていない!
→いったんsubgroup4を削除して、playbook_B_2_2_1.yml実行直後の状態に戻す。
・成功例3:変数をループ使って代入してグループを複数作成(その1)
[client_user@cetnos7_1 ansible_test]$ cat playbook_B_2_2_2_S3.yml
---
- hosts: centos72
tasks:
- name: Add Group
group: name=subgroup{{ item }} gid=200{{ item }}
with_items:
- 1
- 2
- 3
- 4
...
[client_user@cetnos7_1 ansible_test]$ ansible-playbook -i inventory.ini -u root playbook_B_2_2_2_S3.yml
PLAY [centos72] *****************************************************************************************
TASK [Gathering Facts] *****************************************************************************************
ok: [192.168.100.72]
TASK [Add Group] *****************************************************************************************
ok: [192.168.100.72] => (item=1)
changed: [192.168.100.72] => (item=2)
changed: [192.168.100.72] => (item=3)
changed: [192.168.100.72] => (item=4)
PLAY RECAP *****************************************************************************************
192.168.100.72 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@centos7_2 ~]# grep subgroup /etc/group
subgroup1:x:2001:
subgroup2:x:2002:
subgroup3:x:2003:
subgroup4:x:2004:
→グループが複数作成できた。
・成功例4:変数をループ使って代入してグループを複数作成(その2)
[client_user@cetnos7_1 ansible_test]$ cat playbook_B_2_2_2_S4.yml
---
- hosts: centos72
tasks:
- name: Add Group
group: name={{ item.name }} gid={{ item.gid }}
with_items:
- { name: subgroup5, gid: 2005 }
- { name: subgroup6, gid: 2006 }
- { name: subgroup7, gid: 2007 }
...
[client_user@cetnos7_1 ansible_test]$ ansible-playbook -i inventory.ini -u root playbook_B_2_2_2_S4.yml
PLAY [centos72] **************************************************************
TASK [Gathering Facts] **************************************************************
ok: [192.168.100.72]
TASK [Add Group] **************************************************************
changed: [192.168.100.72] => (item={u'gid': 2005, u'name': u'subgroup5'})
changed: [192.168.100.72] => (item={u'gid': 2006, u'name': u'subgroup6'})
changed: [192.168.100.72] => (item={u'gid': 2007, u'name': u'subgroup7'})
PLAY RECAP **************************************************************
192.168.100.72 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@centos7_2 ~]# grep subgroup /etc/group
subgroup1:x:2001:
subgroup2:x:2002:
subgroup3:x:2003:
subgroup4:x:2004:
subgroup5:x:2005:
subgroup6:x:2006:
subgroup7:x:2007:
→グループが複数作成できた。
・成功例5:グループ一覧を外部ファイルから読み込んでグループを複数作成
編集中
B-2-3.userモジュールの動作確認
[client_user@cetnos7_1 ansible_test]$ cat playbook_B_2_3.yml
---
- hosts: centos72
tasks:
- name: Add User
user:
name: "puni"
state: present
groups: "subgroup1,subgroup2"
createhome: yes
# password_expire_max: "99999" ←うまく動かず
password: "{{ 'P@ssw0rd' | password_hash('sha512') }}"
# generate_ssh_key=yes ←うまく動かず
# ssh_key_bite=1024
# ssh_key_file=.ssh/id_rsa_1024
...
[root@centos7_2 ~]# id puni
id: puni: no such user
[client_user@cetnos7_1 ansible_test]$ ansible-playbook -u root -i inventory.ini playbook_B_2_3.yml
PLAY [centos72] **************************************************************
TASK [Gathering Facts] **************************************************************
ok: [192.168.100.72]
TASK [Add User] **************************************************************
changed: [192.168.100.72]
PLAY RECAP **************************************************************
192.168.100.72 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@centos7_2 ~]# id puni
uid=1002(puni) gid=1002(puni) groups=1002(puni),2001(subgroup1),2002(subgroup2)
[root@centos7_2 ~]# ls -ld ~puni
drwx------. 2 puni puni 62 6月 14 21:35 /home/puni
[root@centos7_2 ~]# ls -lA ~puni
合計 12
-rw-r--r--. 1 puni puni 18 11月 25 2021 .bash_logout
-rw-r--r--. 1 puni puni 193 11月 25 2021 .bash_profile
-rw-r--r--. 1 puni puni 231 11月 25 2021 .bashrc
[root@centos7_2 ~]# grep puni /etc/shadow
puni:$6$XCTKBnyfmKA6NTLQ$Pmc2ii8trBFyGlr1e8Svzqx237xRI9/b931.cq3S2IUJePsYe3/qpn8XCeTe5zItGPWdelXEvdB2mf8fw4pfd.:19157:0:99999:7:::
・puniユーザでログインしてみる
[root@centos7_2 ~]# su - server_user
最終ログイン: 2022/06/09 (木) 09:24:07 JST centos7_1から開始日時 pts/1
[server_user@centos7_2 ~]$ whoami
server_user
[server_user@centos7_2 ~]$ su - puni
パスワード: ←「P@ssw0rd」と入力
[puni@centos7_2 ~]$ whoami
puni ←ログインできた
B-2-4.fileモジュールの動作確認
B-2-4-0.touchコマンドでファイルを新規作成
[client_user@cetnos7_1 ansible_test]$ cat playbook_B_2_4_0.yml
---
- hosts: centos72
tasks:
- name: file module
file: path=/tmp/ansible_test/test.file state=touch owner=root group=root mode="u=rw,g=r,o=r"
...
[root@centos7_2 ansible_test]# ls -l `pwd`/test.file;echo $?
ls: /tmp/ansible_test/test.file にアクセスできません: そのようなファイルやディレクトリはありません
2
[client_user@cetnos7_1 ansible_test]$ ansible-playbook -u root -i inventory.ini playbook_B_2_4_0.yml
PLAY [centos72] **************************************************************
TASK [Gathering Facts] *********************************************************************************************************************************************************
ok: [192.168.100.72]
TASK [file module] **************************************************************
changed: [192.168.100.72]
PLAY RECAP **************************************************************
192.168.100.72 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@centos7_2 ansible_test]# ls -l `pwd`/test.file;echo $?
-rw-r--r--. 1 root root 0 6月 15 00:40 /tmp/ansible_test/test.file
0
→touchコマンドでファイルが新規作成されたダニ
[root@centos7_2 ansible_test]# echo unkokko > `pwd`/test.file
[root@centos7_2 ansible_test]# cat /tmp/ansible_test/test.file
unkokko
B-2-4-1.パーミッション、所有者、所有グループが変更
[client_user@cetnos7_1 ansible_test]$ cat playbook_B_2_4_1.yml
---
- hosts: centos72
tasks:
- name: file module
file: path=/tmp/ansible_test/test.file owner=puni group=dagyah mode=0777
...
[root@centos7_2 ansible_test]# ls -l `pwd`/test.file
-rw-r--r--. 1 root root 8 6月 15 00:06 /tmp/ansible_test/test.file
[client_user@cetnos7_1 ansible_test]$ ansible-playbook -u root -i inventory.ini playbook_B_2_4_1.yml
PLAY [centos72] **************************************************************
TASK [Gathering Facts] **************************************************************
ok: [192.168.100.72]
TASK [file module] **************************************************************
changed: [192.168.100.72]
PLAY RECAP **************************************************************
192.168.100.72 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@centos7_2 ansible_test]# ls -l `pwd`/test.file
-rwxrwxrwx. 1 puni dagyah 8 6月 15 00:06 /tmp/ansible_test/test.file
→パーミッション、所有者、所有グループが変更されたダニ。
B-2-4-2.シンボリックリンク作成
[client_user@cetnos7_1 ansible_test]$ cat playbook_B_2_4_2.yml
---
- hosts: centos72
tasks:
- name: file module
file: src=/tmp/ansible_test/test.file dest=/tmp/test.file.link owner=root group=server_user state=link
...
[root@centos7_2 ansible_test]# ls -l /tmp/test.file.link;echo $?
ls: /tmp/test.file.link にアクセスできません: そのようなファイルやディレクトリはありません
2
[client_user@cetnos7_1 ansible_test]$ ansible-playbook -u root -i inventory.ini playbook_B_2_4_2.yml
PLAY [centos72] **************************************************************
TASK [Gathering Facts] **************************************************************
ok: [192.168.100.72]
TASK [file module] **************************************************************
changed: [192.168.100.72]
PLAY RECAP **************************************************************
192.168.100.72 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@centos7_2 ansible_test]# ls -l /tmp/test.file.link;echo $?
lrwxrwxrwx. 1 root server_user 27 6月 15 00:23 /tmp/test.file.link -> /tmp/ansible_test/test.file
0
[root@centos7_2 ansible_test]# cat /tmp/test.file.link
unkokko
→シンボリックリンクが作成されたダニ



