ansible notify和handlers

原文链接:https://www.cpweb.top/826
参考文章:http://www.zsythink.net/archives/2624

  notify 和 handlers,译为通知和处理程序。它们两者关系是notify通知调用hanlers其下的任务执行。
  hanlders和tasks是同级,两者下面都定义了任务。但是handlers下的任务,在没有notify调用它下面任务的情况下,它是不会触发执行的。
  那么如何去调用,我们可以在tasks下面的某个任务中去定义notify调用handlers下的任务。一般在一个任务的结尾,和模块同级。
 可以说 notify 监控这个任务,如果任务是changed状态(即真正的进行实际操作,造成了实际的改变),那么notify会通知调用hanlers其下的某个任务执行。如果任务没有处在changed状态,即任务没造成实际改变,那么notify就不会去通知调用。
示例:

[root@m1 test]# vim notify.yml
- hosts: web1
  tasks:
    - name: configure nginx server
      template:
        src: /root/nginx.conf
        dest: /etc/nginx/nginx.conf
      notify: restart nginx server

  handlers:
    - name: restart nginx server
      systemd:
        name: nginx
        state: restarted

  需要注意的是默认情况下,在tasks下的所有task执行完毕后,才会执行各个handler,并不是执行完某个任务后,立即执行其对应的handler,如果你想要在执行完某些task以后立即执行对应的handler,则需要使用meta模块,示例如下:

[root@m1 test]# cat notify2.yml 
- hosts: web1
  tasks:
    - name: test1
      shell: 'echo "1"'
      notify: touch test1

    - name: test2
      shell: 'echo "2"'
      notify: touch test2

    - meta: flush_handlers
    
    - name: test3
      shell: 'echo "3"'
      notify: touch test3

  handlers:
    - name: touch test1
      file:
        name: /root/test1
        state: touch

    - name: touch test2
      file:
        name: /root/test2
        state: touch

    - name: touch test3
      file:
        name: /root/test3
        state: touch

  上例中,meta任务的参数值为flush_handlers,"meta: flush_handlers"表示立即执行之前的task所对应handler。我们来看下运行结果:

[root@m1 test]# ansible-playbook notify2.yml 

PLAY [web1] *************************************************************************************

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

TASK [test1] ************************************************************************************
changed: [172.16.1.7]

TASK [test2] ************************************************************************************
changed: [172.16.1.7]

RUNNING HANDLER [touch test1] *******************************************************************
changed: [172.16.1.7]

RUNNING HANDLER [touch test2] *******************************************************************
changed: [172.16.1.7]

TASK [test3] ************************************************************************************
changed: [172.16.1.7]

RUNNING HANDLER [touch test3] *******************************************************************
changed: [172.16.1.7]

PLAY RECAP **************************************************************************************
172.16.1.7                 : ok=7    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

  假如我们想要在一个task中用一个notify调用多个handler。我们可能会想到定义多个handler使用相同的name,但是这样并不可行,因为当多个handler的name相同时,只有一个handler会被执行,即遇到第一个剩下同名的不会执行。所以,我们并不能用这种方式去实现。
  想要用一个notify调用多个handler,则需要借助另一个关键字,它就是 listen ,我们可以把 listen 理解成组名,我们可以把多个handler分到这 listen定义的组中,使用notify去调用这个组即可。示例如下:

[root@m1 test]# cat notify2.yml 
- hosts: web1
  tasks:
    - name: test1
      shell: 'echo "1"'
      notify: group

  handlers:
    - name: touch test1
      listen: group
      file:
        name: /root/test1
        state: touch

    - name: touch test2
      listen: group
      file:
        name: /root/test2
        state: touch

运行结果如下:

[root@m1 test]# ansible-playbook notify2.yml 

PLAY [web1] *************************************************************************************

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

TASK [test1] ************************************************************************************
changed: [172.16.1.7]

RUNNING HANDLER [touch test1] *******************************************************************
changed: [172.16.1.7]

RUNNING HANDLER [touch test2] *******************************************************************
changed: [172.16.1.7]

PLAY RECAP **************************************************************************************
172.16.1.7                 : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ansible中,notify参数用于定义在任务执行后要调用的处理程序(handlers)。在一个任务的结尾,使用notify参数来指定要调用的处理程序的名称。如果该任务的状态为"changed"(即任务实际进行了更改),那么处理程序将被调用执行。如果任务的状态不是"changed",处理程序将不会被调用。 以下是一个使用notify参数的示例: ``` - name: configure nginx server template: src: /root/nginx.conf dest: /etc/nginx/nginx.conf notify: restart nginx server ``` 在这个示例中,当配置nginx服务器的任务状态为"changed"时,会调用名为"restart nginx server"的处理程序。 在运行Ansible playbook时,可以看到处理程序的执行情况。例如,在运行结果中,可以看到"changed=3",表示有3个处理程序被调用执行。 希望这个回答对您有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Ansible playbook 处理器(notifyhandlers)](https://blog.csdn.net/qq_34556414/article/details/108365191)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [ansible notifyhandlers](https://blog.csdn.net/supahero/article/details/108667849)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值