playbook中的notify和handler
当执行一个playbook时,第一次执行和第二次执行效果相同,当服务的配置文件发生变化时再次安装时已经启动的服务不会重启,此时就需要使用到notify。
notify的action可用于在每个play的最后被触发,这样可以避免多次有改变发生时每次都执行指定的操作,仅在所有的变化发生完成后一次性地执行指定操作。
notify是触发条件,headlers是执行的任务
notify和handler的使用方法
此处以上一节中的httpd为例,刚才已经启动了HTTP服务,http的端口为8080,此时再次修改httpd.conf,将端口改为80
1 2
| [root@ansible data] Listen 80
|
2.对yaml文件进行修改,增加notify及handlers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| [root@ansible data] ---
- hosts: webserver remote_user: root
tasks: - name: install yum: name=httpd - name: config copy: src=/data/httpd.conf dest=/etc/httpd/conf/httpd.conf notify: restart service - name: service service: name=httpd state=started
handlers: - name: restart service service: name=httpd state=restarted
|
3.检查语法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| [root@ansible data] PLAY [webserver] ************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************** ok: [192.168.73.135] ok: [192.168.73.134]
TASK [install] ************************************************************************************************** ok: [192.168.73.135] ok: [192.168.73.134]
TASK [config] *************************************************************************************************** changed: [192.168.73.135] changed: [192.168.73.134]
TASK [service] ************************************************************************************************** ok: [192.168.73.135] ok: [192.168.73.134]
RUNNING HANDLER [restart service] ******************************************************************************* changed: [192.168.73.134] changed: [192.168.73.135]
PLAY RECAP ****************************************************************************************************** 192.168.73.134 : ok=5 changed=2 unreachable=0 failed=0 192.168.73.135 : ok=5 changed=2 unreachable=0 failed=0
|
4.执行playbook
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| [root@ansible data]
PLAY [webserver] ************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************** ok: [192.168.73.135] ok: [192.168.73.134]
TASK [install] ************************************************************************************************** ok: [192.168.73.135] ok: [192.168.73.134]
TASK [config] *************************************************************************************************** changed: [192.168.73.135] changed: [192.168.73.134]
TASK [service] ************************************************************************************************** ok: [192.168.73.135] ok: [192.168.73.134]
RUNNING HANDLER [restart service] ******************************************************************************* changed: [192.168.73.134] changed: [192.168.73.135]
PLAY RECAP ****************************************************************************************************** 192.168.73.134 : ok=5 changed=2 unreachable=0 failed=0 192.168.73.135 : ok=5 changed=2 unreachable=0 failed=0
|
5.验证远程服务器上的端口是否变为80
1 2 3 4 5 6
| [root@ansible data] 192.168.73.135 | CHANGED | rc=0 >> LISTEN 0 128 :::80 :::*
192.168.73.134 | CHANGED | rc=0 >> LISTEN 0 128 :::80 :::*
|