ansible-playbook-handlers和notify 触发器

发布时间 2023-09-26 10:54:21作者: 家乐福的搬砖日常

Handlers

Handlers是Ansible Playbook中的一种特殊任务,无法直接运行。它需要被其他任务通知后才会运行。它的主要作用是处理Playbook中各个任务之间的通知和协调。当某个任务完成后,如果满足特定条件,就会触发相应的Handler任务。

Notify

Notify是Ansible Playbook中的一个触发器,它用于通知Handlers任务。当某个任务完成后,如果满足特定条件,就会触发相应的Notify任务。Notify任务本身并不执行任何操作,它的作用是通知其他任务(如Handlers任务)执行相应的操作。

handlers和notify 触发器举例

ansible-playbook安装了httpd,当修改了 httpd 的配置文件后我们需要重启httpd服务才会使得配置文件生效;ansible-playbook可以通过触发器来实现一旦发现配置文件有修改就重启服务的操作.

ansible-playbook

[root@localhost handlersAndNotify]# cat handlersandnotify.yml 
---
 - hosts: localhostservers
   remote_user: root
   tasks:
     - name: install httpd package
       yum: name=httpd state=present
     - name: copy config file
       copy: src=/etc/httpd/httpd.conf dest=/etc/httpd/conf/ 
       notify: restart service
     - name: service start
       service: name=httpd state=started enabled=yes

   handlers:
     - name: restart service
       service: name=httpd state=restarted

把配置文件httpd.conf复制到上个目录,修改复制后的配置文件(可以在注释里进行修改,达到文件更新的作用)

[root@localhost handlersAndNotify]# ansible-playbook handlersandnotify.yml 

PLAY [localhostservers] ********************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************
ok: [127.0.0.1]

TASK [install httpd package] ********************************************************************************************************
ok: [127.0.0.1]

TASK [copy config file] ********************************************************************************************************
changed: [127.0.0.1]

TASK [service start] ********************************************************************************************************
ok: [127.0.0.1]

RUNNING HANDLER [restart service] ********************************************************************************************************
changed: [127.0.0.1]

PLAY RECAP ********************************************************************************************************
127.0.0.1                  : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

首次执行的时候可能会报找不到文件,需要执行两次。查看服务进行,进程号改变,说明服务已被重启。

[root@localhost conf]# ps -ef|grep httpd
root      33049      1  0 19:18 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    33054  33049  0 19:18 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    33060  33049  0 19:18 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    33061  33049  0 19:18 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    33062  33049  0 19:18 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    33063  33049  0 19:18 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root      88351   2888  0 19:33 pts/0    00:00:00 grep --color=auto httpd
[root@localhost conf]# ps -ef|grep httpd
root      91772      1  0 19:34 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    91773  91772  0 19:34 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    91774  91772  0 19:34 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    91775  91772  0 19:34 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    91776  91772  0 19:34 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    91777  91772  0 19:34 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root      92402   2888  0 19:34 pts/0    00:00:00 grep --color=auto httpd