2017-10-11 handlers的使用

---
- hosts: centos6
  tasks:
    - name: see hostname
      shell: ifconfig
    - name: copy_test
      copy: src=/root/iptables dest=/etc/sysconfig/iptables backup=yes
      notify:
        - restart iptables
  handlers:
    - name: restart iptables
      service: name=iptables state=restarted

handlers 任务需要 notify 来触发,notify所在的任务项 造成客户端系统文件改变的时候 才会触发notify.
一个play中的handlers只会被执行一次 最后执行.
Handlers 最佳的应用场景是用来重启服务,或者触发系统重启操作.除此以外很少用到了

  • meta: flush_handlers
    在下面的例子中,任何在排队等候的 handlers 会在执行到 ‘meta’ 部分时,优先执行.这个技巧在有些时候也能派上用场.
tasks:
   - shell: some tasks go here
     notyfy:
         - handlers job name
   - meta: flush_handlers
   - shell: some other tasks

你可能感兴趣的:(2017-10-11 handlers的使用)