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.複数のグループを同時に作成

・失敗例1

[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

→シンボリックリンクが作成されたダニ

 

■ざっと考えたこと

※オライリーの「はじめてのAnsible」を流し読みしつつ

1.接続ユーザとplaybook実行ユーザ

・ansibleサーバ→ターゲットホストへ一般ユーザでssh接続してターゲットホスト側で一般ユーザ権限で実行できるplaybookのみ実行してみる

・ansibleサーバ→ターゲットホストが一般ユーザでssh接続の場合

 →playbook上でsuかsudoでrootになる必要がある(ほとんどのコマンドで)

 →playbookの「become文」とは?ターゲット側で/etc/sudoersに許可設定入れ

  とくのか?

・ansibleサーバ→ターゲットホストへrootでssh接続してplaybook実行。その際、ターゲットホスト側のsshd_configで「PermitRootLogin without-password」にして鍵認証によるsshログインをrootに許可する。

2.playbookでターゲットホストの何かのconfigファイルを編集する際の注意点

 - 正規表現マッチングして行置換

  →例えば、「Listen 443」という行と「#Listen 443」という行が両方あっ

   た場合、これらを「Listen 8443」に変更する場合はどうするか?

  →例えばVirtualHostの数だけ「Listen 443」とか「Listen 8443」とかが複   

   数あった場合で特定のVirtualHostの設定だけ選んで変更する場合どうするか?

  →正規表現でマッチングする行が無い場合は最下行に追記されるらしいが、

   configファイルの種類によっては、ブロックの概念があって、最下行ではな

   く適切なブロック内に追記する必要があるものもある

  →正規表現でマッチングする行が無い場合は最下行に追記されるらしいが、

   configファイルの種類によっては、設定項目(ディレクティブ)に順序性

   があって、適切な場所に行を追記しないと想定した動作をしないconfigも

   ある。

 - configの事前バックアップ

  →configによっては「.bak」などのサフィックスをファイル名につけて、

   同階層にコピーしてはいけない種類のconfigもある(例えばnetwork-scripts

   network-scriptsは「存在するファイル」はnetworkサービス起動時にすべて

   読み込まれるので、「.bak」が付与されたスクリプトも一緒に読みこまれて

   しまう。NetworkManagerでネットワークを制御する場合も、一部の

   ファイルに関してはnetwork-scripts内のスクリプトが読み込まれる。

3.playbookの変数の使い方把握

4.ansibleの予約変数、予約語

5.playbookを書くうえで「""」や「''」で囲まないといけないケース

6.playbookの「when」分岐の動作把握

7.playbookの「with_items」ループの動作把握

8.playbookの冪等性のためにerror確認をplaybookに適切に書く必要があるか?

 変更対象ファイルの事前チェック、対象ファイルをすでにopenしているプロ

 セスの有無確認などを行い変更後のあるべき状態にすでになっていたらスキップ

 してなにもしないこととする

9.大規模なplaybookを書く上で洗練されたファイル構成

 - インベントリ(ini)

 - playbook(yml)の分割 ※構造化?

 - groupの使い方

 - 変数の外部ファイル(txt)への書き出し

 - FACT

 - role構成

 - その他外出しファイル

 

10.既存モジュールの種類の把握と応用のコツ

 - copy

 - template

 - lineinfile

 - user

 - raw,shell,command

 - file

 - oracleDBにSQLを発行するモジュール

 ※postgreSQLではユーザ作成用のPostgresql_userモジュール、

  データベース作成用のPostgresql_dbモジュールというのがある

 

■簡単なplaybookの実行

※ロールバックできるようにAnsibleサーバとターゲットホストの初期状態のスナップショットを検証前にとっておく(VirtualBox無料なのに便利すぎ)

※ansibleホスト側作業ディレクトリは「/tmp/ansible_test」とする

 

A.ターゲットホスト1台、一般ユーザ(server_user)で実行

Ansibleサーバホストとターゲットホストは下記のssh鍵設定をした直後の状態を本検証の初期状態とする。

つまり、

Ansibleサーバホスト(centos7_1)のclient_userからターゲットホスト(centos7_2)のserver_userへの鍵認証によるsshログインが可能な構成。

※初期状態ではAnsibleサーバホスト(centos7_1)からターゲットホスト(centos7_2)のrootへの鍵認証によるsshログインが不可。

・ansibleサーバ側の検証時の状態

[client_user@cetnos7_1 ansible_test]$ hostname;whoami;pwd
cetnos7_1
client_user
/tmp/ansible_test

・インベントリファイル

[client_user@cetnos7_1 ansible_test]$ cat inventory.ini
[centos71]
192.168.100.71

[centos72]
192.168.100.72

 

1.pingモジュールの動作確認

[client_user@cetnos7_1 ansible_test]$ cat playbook_1.yml
---
 - hosts: centos72
   tasks:
   - name: Ping Connection
     ping:

[client_user@cetnos7_1 ansible_test]$ ansible-playbook -u server_user -i inventory.ini playbook_1.yml

PLAY [centos72] *****************************************************************************************

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

TASK [Ping Connection] *****************************************************************************************
ok: [192.168.100.72]

PLAY RECAP *****************************************************************************************
192.168.100.72             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

 

2.copyモジュールの動作確認(fromリモートtoリモート)

[client_user@cetnos7_1 ansible_test]$ cat playbook_2.yml
---
 - hosts: centos72
   tasks:
   - name: Copy file
     copy:
       src: /tmp/ansible_test/test.txt
       dest: /tmp/ansible_test/test.txt.bak
       remote_src: yes

[server_user@centos7_2 ansible_test]$ hostname;whoami;pwd
centos7_2
server_user
/tmp/ansible_test

[server_user@centos7_2 ansible_test]$ ls -ld /tmp/ansible_test/
drwxrwxr-x. 2 server_user server_user 22  6月  9 05:25 /tmp/ansible_test/

[server_user@centos7_2 ansible_test]$  ls -lA /tmp/ansible_test/
合計 4
-rw-rw-r--. 1 server_user server_user 5  6月  9 01:06 test.txt

[server_user@centos7_2 ansible_test]$ cat /tmp/ansible_test/test.txt
hoge

※以後ターゲットホスト側のコマンドライン入出力を薄黄色のマーカーにする

[client_user@cetnos7_1 ansible_test]$ ansible-playbook -u server_user -i inventory.ini playbook_2.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

[server_user@centos7_2 ansible_test]$ ls -lA /tmp/ansible_test/
合計 8
-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

→test.txt.bakがコピーされた。

 

2-1.copyモジュールの動作確認(fromローカルtoリモート)

[client_user@cetnos7_1 ansible_test]$ cat playbook_2_1.yml
---
 - hosts: centos72
   tasks:
   - name: Copy file
     copy:
       src: /tmp/test.txt
       dest: /tmp/ansible_test/test.txt.bak_2
       remote_src: no
       mode: 0777

[client_user@cetnos7_1 ansible_test]$ cat /tmp/test.txt
fuga

[client_user@cetnos7_1 ansible_test]$ ansible-playbook -u server_user -i inventory.ini playbook_2_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

[server_user@centos7_2 ansible_test]$ ls -lA /tmp/ansible_test/
合計 12
-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

[server_user@centos7_2 ansible_test]$ cat /tmp/ansible_test/test.txt.bak_2
fuga

 

 

 

 

 

 

 

■やること

下記の構成でAnsibleを構築し、Ansibleによる設定変更を試す。

 

Ansible(ホスト名:centoos7_1)---被設定変更ホスト(ホスト名:centoos7_2)

 

 

■構成

・Ansibleホスト(centoos7_1)

・OS:CentOS7.9

・ハードウェア構成:VirtualBox6.1上のVM、vcpu×1コア、Mem2GB

・Ansible2.9

・被設定変更ホスト(centoos7_2)

・OS:CentOS7.9

・ハードウェア構成:VirtualBox6.1上のVM、vcpu×1コア、Mem2GB

 

 

■インストール要件

・Python 2 (バージョン 2.7) または Python 3 (バージョン 3.5 以降) 

 

■インストール

・pythonバージョン確認

[root@cetnos7_1 ~]# python --version
Python 2.7.5

・ansible2.8用リポジトリ追加

下記のCentOS7インストール直後のレポジトリリストは下記の通り。

[root@centos7_1 yum.repos.d]# yum repolist
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.tsukuba.wide.ad.jp
 * extras: ftp.tsukuba.wide.ad.jp
 * updates: ftp-srv2.kddilabs.jp
リポジトリー ID                                        リポジトリー名                                                                状態
base/7/x86_64                                        CentOS-7 - Base                                                        10,072
extras/7/x86_64                                      CentOS-7 - Extras                                                          512
updates/7/x86_64                                   CentOS-7 - Updates                                                     3,863
repolist: 14,447

[root@cetnos7_1 ~]# yum list | grep ansible
ansible-collection-microsoft-sql.noarch   1.1.0-1.el7_9                extras
centos-release-ansible-27.noarch          1-1.el7                      extras
centos-release-ansible-28.noarch          1-1.el7                      extras
centos-release-ansible-29.noarch          1-1.el7                      extras
centos-release-ansible26.noarch           1-3.el7.centos               extras

上記はそれぞれansibleの各バージョンごとのインストール用リポジトリらしい。

※下記を参考にしてepelリポジトリを追加してepelリポジトリからansible本体をインストールしようとすると依存パッケージがうまくインストールできないっぽい。

 

なのでCentOS7.9にマージ済みの上記の「centos-release-ansible-28.noarch」をインストールする。

[root@cetnos7_1 yum.repos.d]# yum install centos-release-ansible-28.noarch

---(略)---

[root@cetnos7_1 yum.repos.d]# yum repolist
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.tsukuba.wide.ad.jp
 * centos-ansible-28: ftp.tsukuba.wide.ad.jp
 * extras: ftp.tsukuba.wide.ad.jp
 * updates: mirrors.cat.net
リポジトリー ID                                         リポジトリー名                                                                  状態
base/7/x86_64                                         CentOS-7 - Base                                                           10,072
centos-ansible-28/7/x86_64                      CentOS Configmanagement SIG - ansible-28                          18
extras/7/x86_64                                      CentOS-7 - Extras                                                               512
updates/7/x86_64                                    CentOS-7 - Updates                                                         3,863

repolist: 14,465

→「CentOS Configmanagement SIG - ansible-28 」という名のリポジトリができた。

[root@cetnos7_1 yum.repos.d]# yum list | grep ansible
 * centos-ansible-28: mirrors.cat.net
centos-release-ansible-28.noarch          1-1.el7                      @extras
ansible.noarch                            2.8.19-1.el7                 centos-ansible-28  ←ansible2.8が現れた。
ansible-collection-microsoft-sql.noarch   1.1.0-1.el7_9                extras
centos-release-ansible-27.noarch          1-1.el7                      extras
centos-release-ansible-29.noarch          1-1.el7                      extras
centos-release-ansible26.noarch           1-3.el7.centos               extras
python-keyczar.noarch                     0.71c-2.el7                  centos-ansible-28
python2-crypto.x86_64                     2.6.1-15.el7                 centos-ansible-28

 

・ansible2.8インストール

[root@cetnos7_1 yum.repos.d]# yum install ansible
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.nara.wide.ad.jp
 * centos-ansible-28: ftp.nara.wide.ad.jp
 * extras: ftp.nara.wide.ad.jp
 * updates: ftp.nara.wide.ad.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ ansible.noarch 0:2.8.19-1.el7 を インストール
--> 依存性の処理をしています: PyYAML のパッケージ: ansible-2.8.19-1.el7.noarch
--> 依存性の処理をしています: python-jinja2 のパッケージ: ansible-2.8.19-1.el7.noarch
--> 依存性の処理をしています: python-paramiko のパッケージ: ansible-2.8.19-1.el7.noarch
--> 依存性の処理をしています: python-setuptools のパッケージ: ansible-2.8.19-1.el7.noarch
--> 依存性の処理をしています: python-six のパッケージ: ansible-2.8.19-1.el7.noarch
--> 依存性の処理をしています: python2-cryptography のパッケージ: ansible-2.8.19-1.el7.noarch
--> 依存性の処理をしています: sshpass のパッケージ: ansible-2.8.19-1.el7.noarch
--> トランザクションの確認を実行しています。
---> パッケージ PyYAML.x86_64 0:3.10-11.el7 を インストール
--> 依存性の処理をしています: libyaml-0.so.2()(64bit) のパッケージ: PyYAML-3.10-11.el7.x86_64
---> パッケージ python-jinja2.noarch 0:2.7.2-4.el7 を インストール
--> 依存性の処理をしています: python-babel >= 0.8 のパッケージ: python-jinja2-2.7.2-4.el7.noarch
--> 依存性の処理をしています: python-markupsafe のパッケージ: python-jinja2-2.7.2-4.el7.noarch
---> パッケージ python-paramiko.noarch 0:2.1.1-9.el7 を インストール
--> 依存性の処理をしています: python2-pyasn1 のパッケージ: python-paramiko-2.1.1-9.el7.noarch
---> パッケージ python-setuptools.noarch 0:0.9.8-7.el7 を インストール
--> 依存性の処理をしています: python-backports-ssl_match_hostname のパッケージ: python-setuptools-0.9.8-7.el7.noarch
---> パッケージ python-six.noarch 0:1.9.0-2.el7 を インストール
---> パッケージ python2-cryptography.x86_64 0:1.7.2-2.el7 を インストール
--> 依存性の処理をしています: python-idna >= 2.0 のパッケージ: python2-cryptography-1.7.2-2.el7.x86_64
--> 依存性の処理をしています: python-cffi >= 1.4.1 のパッケージ: python2-cryptography-1.7.2-2.el7.x86_64
--> 依存性の処理をしています: python-ipaddress のパッケージ: python2-cryptography-1.7.2-2.el7.x86_64
--> 依存性の処理をしています: python-enum34 のパッケージ: python2-cryptography-1.7.2-2.el7.x86_64
---> パッケージ sshpass.x86_64 0:1.06-2.el7 を インストール
--> トランザクションの確認を実行しています。
---> パッケージ libyaml.x86_64 0:0.1.4-11.el7_0 を インストール
---> パッケージ python-babel.noarch 0:0.9.6-8.el7 を インストール
---> パッケージ python-backports-ssl_match_hostname.noarch 0:3.5.0.1-1.el7 を インストール
--> 依存性の処理をしています: python-backports のパッケージ: python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch
---> パッケージ python-cffi.x86_64 0:1.6.0-5.el7 を インストール
--> 依存性の処理をしています: python-pycparser のパッケージ: python-cffi-1.6.0-5.el7.x86_64
---> パッケージ python-enum34.noarch 0:1.0.4-1.el7 を インストール
---> パッケージ python-idna.noarch 0:2.4-1.el7 を インストール
---> パッケージ python-ipaddress.noarch 0:1.0.16-2.el7 を インストール
---> パッケージ python-markupsafe.x86_64 0:0.11-10.el7 を インストール
---> パッケージ python2-pyasn1.noarch 0:0.1.9-7.el7 を インストール
--> トランザクションの確認を実行しています。
---> パッケージ python-backports.x86_64 0:1.0-8.el7 を インストール
---> パッケージ python-pycparser.noarch 0:2.14-1.el7 を インストール
--> 依存性の処理をしています: python-ply のパッケージ: python-pycparser-2.14-1.el7.noarch
--> トランザクションの確認を実行しています。
---> パッケージ python-ply.noarch 0:3.4-11.el7 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

====================================================================
 Package                  アーキテクチャー              バージョン                       リポジトリー                            容量
====================================================================
インストール中:
 ansible                                      noarch     2.8.19-1.el7      centos-ansible-28     15    M
依存性関連でのインストールをします:
 PyYAML                                           x86_64     3.10-11.el7      base                    153    k
 libyaml                                      x86_64     0.1.4-11.el7_0      base                    55    k
 python-babel                                  noarch     0.9.6-8.el7      base                    1.4    M
 python-backports                             x86_64     1.0-8.el7           base                    5.8    k
 python-backports-ssl_match_hostname    noarch     3.5.0.1-1.el7      base                    13    k
 python-cffi                                  x86_64     1.6.0-5.el7      base                    218    k
 python-enum34                                  noarch     1.0.4-1.el7      base                    52    k
 python-idna                                  noarch     2.4-1.el7           base                    94    k
 python-ipaddress                             noarch     1.0.16-2.el7      base                    34    k
 python-jinja2                                  noarch     2.7.2-4.el7      base                    519    k
 python-markupsafe                             x86_64     0.11-10.el7      base                    25    k
 python-paramiko                             noarch     2.1.1-9.el7      base                    269    k
 python-ply                                      noarch     3.4-11.el7           base                    123    k
 python-pycparser                             noarch     2.14-1.el7           base                    104    k
 python-setuptools                             noarch     0.9.8-7.el7      base                    397    k
 python-six                                      noarch     1.9.0-2.el7      base                    29    k
 python2-cryptography                        x86_64     1.7.2-2.el7      base                    502    k
 python2-pyasn1                                  noarch     0.1.9-7.el7      base                    100    k
 sshpass                                      x86_64     1.06-2.el7           extras               21    k

トランザクションの要約
====================================================================
インストール  1 パッケージ (+19 個の依存関係のパッケージ)

総ダウンロード容量: 19 M
インストール容量: 102 M
Is this ok [y/d/N]: y
Downloading packages:
(1/20): python-backports-1.0-8.el7.x86_64.rpm                                                            | 5.8 kB  00:00:00
---(略)---

 

(17/20): python2-pyasn1-0.1.9-7.el7.noarch.rpm                                                          | 100 kB  00:00:01
warning: /var/cache/yum/x86_64/7/centos-ansible-28/packages/ansible-2.8.19-1.el7.noarch.rpm: Header V4 RSA/SHA1 Signature, key ID 6e8b7e8a: NOKEY 2.5 MB/s |  17 MB  00:00:00 ETA
ansible-2.8.19-1.el7.noarch.rpm の公開鍵がインストールされていません
(18/20): ansible-2.8.19-1.el7.noarch.rpm                                                                  |  15 MB  00:00:06
(19/20): libyaml-0.1.4-11.el7_0.x86_64.rpm                                                                 |  55 kB  00:00:15
(20/20): python-babel-0.9.6-8.el7.noarch.rpm                                                               | 1.4 MB  00:00:16
-----------------------------------------------------------------------------------------------------------------------------
合計                                                                                                              1.1 MB/s |  19 MB  00:00:16
file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-ConfigManagement から鍵を取得中です。
Importing GPG key 0x6E8B7E8A:
 Userid     : "CentOS Config Management SIG (https://wiki.centos.org/SpecialInterestGroup/ConfigManagementSIG) <security@centos.org>"
 Fingerprint: c75a fb57 d5c0 f238 cb15 bec8 1ae1 10fa 6e8b 7e8a
 Package    : centos-release-configmanagement-1-1.el7.centos.noarch (@extras)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-ConfigManagement
上記の処理を行います。よろしいでしょうか? [y/N]y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  インストール中          : python-six-1.9.0-2.el7.noarch                                                                        1/20

 

---(略)---

インストール:
  ansible.noarch 0:2.8.19-1.el7

依存性関連をインストールしました:
  PyYAML.x86_64 0:3.10-11.el7                                 libyaml.x86_64 0:0.1.4-11.el7_0       python-babel.noarch 0:0.9.6-8.el7       python-backports.x86_64 0:1.0-8.el7
  python-backports-ssl_match_hostname.noarch 0:3.5.0.1-1.el7  python-cffi.x86_64 0:1.6.0-5.el7      python-enum34.noarch 0:1.0.4-1.el7      python-idna.noarch 0:2.4-1.el7
  python-ipaddress.noarch 0:1.0.16-2.el7                      python-jinja2.noarch 0:2.7.2-4.el7    python-markupsafe.x86_64 0:0.11-10.el7  python-paramiko.noarch 0:2.1.1-9.el7
  python-ply.noarch 0:3.4-11.el7                              python-pycparser.noarch 0:2.14-1.el7  python-setuptools.noarch 0:0.9.8-7.el7  python-six.noarch 0:1.9.0-2.el7
  python2-cryptography.x86_64 0:1.7.2-2.el7                   python2-pyasn1.noarch 0:0.1.9-7.el7   sshpass.x86_64 0:1.06-2.el7
完了しました!

・バージョン確認

[root@cetnos7_1 yum.repos.d]# ansible --version
ansible 2.8.19
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Nov 16 2020, 22:23:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]