今回は、以前に作成した環境のうちコントロールノードであるインスタンスAから、ターゲットノードであるインスタンスBへ、playbookを使ってAnsibleコマンドを実行してみようと思う。
インベントリの作成
test02を作成
[ec2-user@ip-172-16-23-121 .ansible]$ mkdir -vp test02/inventory mkdir: created directory ‘test02’ mkdir: created directory ‘test02/inventory’ [ec2-user@ip-172-16-23-121 .ansible]$ touch test02/inventory/test02_inventory.ini
インベントリにターゲットノード(インスタンスB)のIPアドレスを記載
[targetnode] 172.16.22.159
プレイブックの作成
$HOME/.ansibleフォルダの直下にtest02_playbok.ymlを作成
[ec2-user@ip-172-16-23-121 .ansible]$ touch test02_playbook.yml
プレイブックを記述していく
- hosts: targetnode tasks: - name: make folder file: path: /home/ec2-user/.ansible/test02/temp state: directory owner: ec2-user mode: 0755 - name: copy file copy: src: /home/ec2-user/test02_ansible.txt dest: /home/ec2-user/.ansible/test02/temp/test02_ansible.txt remote_src: yes owner: ec2-user mode: 0644
- hosts:インベントリに記載したターゲットノード
- file:フォルダを作成(stateで選択)
- copy:コピー元、コピー先を指定してファイルをコピー ※[余談1] 参照
[余談1] copyのsrcについて
ちょっとわかりにくかった部分を覚書きとして残す。
copyモジュールのsrcはデフォルトでは、コントロールノードのパスを指す。
これを、ターゲットノード側のファイル指定したい場合は、remote_srcをyesにする必要がある。(指定しないとデフォルトはnoなので、srcはコントロールノードを指す)
最初このことに気づいていなくて、ファイルコピーのタスクでエラーが出た。
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: If you are using a module and expect the file to exist on the remote, see the remote_src option
fatal: [172.16.22.159]: FAILED! => {"changed": false, "msg": "Could not find or access '/home/ec2-user/test02_ansible.txt' on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}
ファイルが見つからない、とあるが、ターゲットノードには確かにそのファイルは存在している。
なぜないと言われるのか?・・・と思い調べていたところ、srcがターゲットノードのことだということが判明して、remote_srcオプションを追加したのである。
とても分かりにくい。
プレイブックの実行
何度も失敗した挙句、最終的に成功したプレイブック実行結果がこちら。
[ec2-user@ip-172-16-23-121 inventory]$ ansible-playbook -i test02_inventory.ini $HOME/.ansible/test02_playbook.yml PLAY [targetnode] ************************************************************** TASK [Gathering Facts] ********************************************************* ok: [172.16.22.159] TASK [make folder] ************************************************************* ok: [172.16.22.159] TASK [copy file] *************************************************************** changed: [172.16.22.159] PLAY RECAP ********************************************************************* 172.16.22.159 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
コマンドが成功すると、okもしくはchangedになって、結果のまとめが表示される。
フォルダ生成とファイルコピーができていることを確認してみる。
[ec2-user@ip-172-16-22-159 ~]$ cd .ansible [ec2-user@ip-172-16-22-159 .ansible]$ ls -a . .. test02 tmp [ec2-user@ip-172-16-22-159 .ansible]$ cd test02 [ec2-user@ip-172-16-22-159 test02]$ ls -a . .. temp [ec2-user@ip-172-16-22-159 test02]$ cd temp [ec2-user@ip-172-16-22-159 temp]$ ls -al total 0 drwxr-xr-x 2 ec2-user ec2-user 32 Apr 29 13:05 . drwxr-xr-x 3 ec2-user ec2-user 18 Apr 29 12:47 .. -rw-r--r-- 1 ec2-user ec2-user 0 Apr 29 12:52 test02_ansible.txt
$HOME/.ansible/test02/temp/test02_ansible.txtができていることがわかる。OK!