こんにちは! JQです。
前回は『 OSS編~AnsibleでRoleを試してみる02~ 』と題して、AnsibleでRoleのTemplatesとFileを試してみました。
今回は『OSS編~AnsibleでRoleを試してみる03~』と題して、AnsibleでRoleのVarsとHandlersを引き続き試してみたいと思います。
1.varsの設定
先ずはvarsの設定を行います。
1$ sudo vim roles/apache/vars/main.yml2---3test_var: hello次に上記の変数を設定したテンプレートファイルを作成します。
1$ sudo vim roles/apache/templates/test.html.j22{{ test_var }}設定した変数でファイルを作成するタスクを追加します。
1$ sudo vim roles/apache/tasks/main.yml2---3- name: apache install4 yum: pkg={{ item }} state=present5 with_items:6 - httpd78- name: template conf9 template: src=test.conf.j2 dest=/etc/httpd/conf.d/test.conf mode=06441011- name: make dir12 file: path=/var/www/test state=directory1314- name: index file15 copy: src=index.html dest=/var/www/test/index.html owner=root group=root mode=06441617- name: template var file18 template: src=test.html.j2 dest=/var/www/test/{{ test_var }}.html mode=06441920- name: on httpd21 service: name=httpd enabled=yes state=restartedそれでは実行してみます。
1$ sudo ansible-playbook site.yml2 [WARNING]: The version of gmp you have installed has a known issue regarding3timing vulnerabilities when used with pycrypto. If possible, you should update4it (ie. yum update gmp).56PLAY [testservers] ************************************************************78GATHERING FACTS ***************************************************************9ok: [127.0.0.1]1011TASK: [apache | apache install] ***********************************************12ok: [127.0.0.1] => (item=httpd)1314TASK: [apache | template conf] ************************************************15ok: [127.0.0.1]1617TASK: [apache | make dir] *****************************************************18ok: [127.0.0.1]1920TASK: [apache | index file] ***************************************************21ok: [127.0.0.1]2223TASK: [apache | template var file] ********************************************24changed: [127.0.0.1]2526TASK: [apache | on httpd] *****************************************************27changed: [127.0.0.1]2829PLAY RECAP ********************************************************************30127.0.0.1 : ok=7 changed=2 unreachable=0 failed=0確認してみます。
以下の様に変数が反映されていれば成功です。
1$ curl http://127.0.0.1/test/hello.html2hello2.Handlersの設定
次にHandlersを試してみます。
Apache再起動のHandlersを作成します。
1$ sudo vim roles/apache/handlers/main.yml2---3- name: restart httpd4 service: name=httpd state=restarted今回はタスクの「template conf」の処理に上記の設定を追加します。
また、「on httpd」のstate=restartedを外してわかりやすくします。
1$ sudo vim roles/apache/tasks/main.yml2---3- name: apache install4 yum: pkg={{ item }} state=present5 with_items:6 - httpd78- name: template conf9 notify:10 - restart httpd11 template: src=test.conf.j2 dest=/etc/httpd/conf.d/test.conf mode=06441213- name: make dir14 file: path=/var/www/test state=directory1516- name: index file17 copy: src=index.html dest=/var/www/test/index.html owner=root group=root mode=06441819- name: template var file20 template: src=test.html.j2 dest=/var/www/test/{{ test_var }}.html mode=06442122- name: on httpd23 service: name=httpd enabled=yes「template conf」が更新されるようにテンプレートファイルを更新しておきます。
1$ sudo vim roles/apache/templates/test.conf.j22Alias /handler /var/www/test実行してみます。
1$ sudo ansible-playbook site.yml2 [WARNING]: The version of gmp you have installed has a known issue regarding3timing vulnerabilities when used with pycrypto. If possible, you should update4it (ie. yum update gmp).56PLAY [testservers] ************************************************************78GATHERING FACTS ***************************************************************9ok: [127.0.0.1]1011TASK: [apache | apache install] ***********************************************12ok: [127.0.0.1] => (item=httpd)1314TASK: [apache | template conf] ************************************************15changed: [127.0.0.1]1617TASK: [apache | make dir] *****************************************************18ok: [127.0.0.1]1920TASK: [apache | index file] ***************************************************21ok: [127.0.0.1]2223TASK: [apache | template var file] ********************************************24ok: [127.0.0.1]2526TASK: [apache | on httpd] *****************************************************27ok: [127.0.0.1]2829NOTIFIED: [apache | restart httpd] ********************************************30changed: [127.0.0.1]3132PLAY RECAP ********************************************************************33127.0.0.1 : ok=8 changed=2 unreachable=0 failed=0「template conf」が更新され、NOTIFIEDが実施されたのがわかります。
正しく反映されています。
1$ curl http://127.0.0.1/handler/hello.html2helloいかがでしたでしょうか?
次回は『OSS編~AnsibleでRoleを試してみる04~』と題して、AnsibleでRoleのmetaを試してみたいと思います。
お楽しみに!!!