こんにちは! JQです。
前回は『OSS編~Ansibleでサーバ構成管理  パート①~』と題して、Ansibleを試してみました。
今回は『OSS編~Ansibleでサーバ構成管理  パート②~』と題して、AnsibleでPlaybookを試してみたいと思います。
Playbook
4.グループ
先ずはインベントリにグループを設定します。
| 1 2 3 | $ vim ansible_hosts [test] ec2-xx-xx-xx-xx.compute-1.amazonaws.com | 
5.Playbookファイルの作成
今回はApacheをインストールするWEBサーバのPlaybookを書いてみます。
| 1 2 3 4 5 6 7 8 9 | $ vim web.yml - hosts: test   remote_user: ec2-user   sudo: yes   tasks:   - name: ensure apache is at the latest version     yum: pkg=httpd state=latest   - name: ensure apache is running and enabled     service: name=httpd state=started enabled=yes  | 
次に「–syntax-check」オプションを利用して設定ファイルを確認してみます。
| 1 | $ ansible-playbook --syntax-check web.yml | 
| 1 | playbook: web.yml | 
「–list-tasks」オプションでは実行内容が確認出来ます。
エラーが無ければ下記のように表示されます。
| 1 | $ ansible-playbook --list-tasks web.yml | 
| 1 | playbook: web.yml | 
| 1 2 3 |  play #1 (test):     ensure apache is at the latest version     ensure apache is running and enabled | 
「–check」オプションを利用すると実際に実行はしませんが、
実行した際の想定結果が出力されます。
| 1 | $ ansible-playbook --check web.yml | 
| 1 | PLAY [test] ******************************************************************* | 
| 1 2 | GATHERING FACTS *************************************************************** ok: [ec2-54-80-166-18.compute-1.amazonaws.com] | 
| 1 2 | TASK: [ensure apache is at the latest version] ******************************** changed: [ec2-54-80-166-18.compute-1.amazonaws.com] | 
| 1 2 | TASK: [ensure apache is running and enabled] ********************************** ok: [ec2-54-80-166-18.compute-1.amazonaws.com] | 
| 1 2 | PLAY RECAP ******************************************************************** ec2-54-80-166-18.compute-1.amazonaws.com : ok=3    changed=1    unreachable=0    failed=0 | 
それでは実際に実行してみましょう!
| 1 | $ ansible-playbook web.yml | 
| 1 | PLAY [test] ******************************************************************* | 
| 1 2 | GATHERING FACTS *************************************************************** ok: [ec2-54-80-166-18.compute-1.amazonaws.com] | 
| 1 2 | TASK: [ensure apache is at the latest version] ******************************** changed: [ec2-54-80-166-18.compute-1.amazonaws.com] | 
| 1 2 | TASK: [ensure apache is running and enabled] ********************************** changed: [ec2-54-80-166-18.compute-1.amazonaws.com] | 
| 1 2 | PLAY RECAP ******************************************************************** ec2-54-80-166-18.compute-1.amazonaws.com : ok=3    changed=2    unreachable=0    failed=0 | 
クライアント側で確認してみます。
| 1 2 3 4 | $ sudo /etc/rc.d/init.d/httpd status httpd (pid  18171) is running... $ sudo chkconfig --list httpd httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off | 
成功しました!
いかがでしたでしょうか?
次回もお楽しみに!!!

