OSS編~Ansibleでサーバ構成管理 パート②~

こんにちは! JQです。

前回は『 OSS編~Ansibleでサーバ構成管理 パート①~ 』と題して、Ansibleを試してみました。

今回は『OSS編~Ansibleでサーバ構成管理 パート②~』と題して、AnsibleでPlaybookを試してみたいと思います。

Playbook

4.グループ

先ずはインベントリにグループを設定します。

1$ vim ansible_hosts2[test]3ec2-xx-xx-xx-xx.compute-1.amazonaws.com

5.Playbookファイルの作成

今回はApacheをインストールするWEBサーバのPlaybookを書いてみます。

1$ vim web.yml2- hosts: test3  remote_user: ec2-user4  sudo: yes5  tasks:6  - name: ensure apache is at the latest version7    yum: pkg=httpd state=latest8  - name: ensure apache is running and enabled9    service: name=httpd state=started enabled=yes

次に「–syntax-check」オプションを利用して設定ファイルを確認してみます。

1$ ansible-playbook --syntax-check web.yml
1playbook: web.yml

「–list-tasks」オプションでは実行内容が確認出来ます。

エラーが無ければ下記のように表示されます。

1$ ansible-playbook --list-tasks web.yml
1playbook: web.yml
1play #1 (test):2    ensure apache is at the latest version3    ensure apache is running and enabled

「–check」オプションを利用すると実際に実行はしませんが、

実行した際の想定結果が出力されます。

1$ ansible-playbook --check web.yml
1PLAY [test] *******************************************************************
1GATHERING FACTS ***************************************************************2ok: [ec2-54-80-166-18.compute-1.amazonaws.com]
1TASK: [ensure apache is at the latest version] ********************************2changed: [ec2-54-80-166-18.compute-1.amazonaws.com]
1TASK: [ensure apache is running and enabled] **********************************2ok: [ec2-54-80-166-18.compute-1.amazonaws.com]
1PLAY RECAP ********************************************************************2ec2-54-80-166-18.compute-1.amazonaws.com : ok=3    changed=1    unreachable=0    failed=0

それでは実際に実行してみましょう!

1$ ansible-playbook web.yml
1PLAY [test] *******************************************************************
1GATHERING FACTS ***************************************************************2ok: [ec2-54-80-166-18.compute-1.amazonaws.com]
1TASK: [ensure apache is at the latest version] ********************************2changed: [ec2-54-80-166-18.compute-1.amazonaws.com]
1TASK: [ensure apache is running and enabled] **********************************2changed: [ec2-54-80-166-18.compute-1.amazonaws.com]
1PLAY RECAP ********************************************************************2ec2-54-80-166-18.compute-1.amazonaws.com : ok=3    changed=2    unreachable=0    failed=0

クライアント側で確認してみます。

1$ sudo /etc/rc.d/init.d/httpd status2httpd (pid  18171) is running...3$ sudo chkconfig --list httpd4httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off

成功しました!

いかがでしたでしょうか?

次回もお楽しみに!!!

この記事を書いた人

aws-recipe-user