已归录

看下面这个剧本:

---
- hosts: demogrp
  remote_user: root
  tasks:
    - name: copy conf file
      copy: src=/etc/rsyslog.conf dest=/etc/ backup=yes
      notify: 
        - restart rsyslog
        - check rsyslog
    - name: start service
      service: name=rsyslog state=started enabled=yes
  handlers:
    - name: restart rsyslog
      service: name=rsyslog state=restarted
    - name: check rsyslog
      shell: killall -0 rsyslogd 2> /tmp/rsyslog.check

说明:0信号是个特殊的信号,该信号不对进程做任何干预,仅仅是确定该进程是否存在而已。

这个剧本,如果不要 notify 和 handlers 部分,这个 playbook 存在一个问题,修改了主控端的 rsyslog.conf 配置文件后,执行该脚本,会将新的文件复制到被控端,但是不会执行 start 这个动作,因为服务当前已经起起来了,这样一来,服务器将不会使用新的配置文件。有了 notify 和 handlers 后,因为文件发生了变动, notify 就会去调用名为 restart rsyslog 和 check rsyslog 的 handlers 动作。handlers 本质上也是 tasks,只不当只有在一定条件下才会被 notify 调用。

-- By 许望(RHCA、OCM、VCP)
最后修改:2020 年 07 月 03 日 09 : 45 AM
如果觉得我的文章对你有用,请随意赞赏