zoukankan      html  css  js  c++  java
  • ansible-handlers

    配置文件被修改后,有可能需要重启程序,此时我们可以配置一个handlers,类似触发器。

    1、Handlers 只有在其所在的执行时,才会执行;如果一个任务中定义了notify调用Handlers,但是由于条件判断等原因,

      该任务未执行,那么Handlers同样不会被执行。

    2、Handlers只会在play的末尾运行一次;如果想在一个Playbook的中间运行Handlers,则需要使用meta模块实现。例如:- meta:flush_handlers

    3、如果一个play在运行到调用Handlers的语句之前失败了,那么这个Handlers将不会被执行。我们可以使用mega模块的--force-handlers选项来强制

      执行Handlers,即使是Handlers所在的Play中途运行失败也能运行

     场景1:创建文件

    ---
    - hosts: test
      remote_user: dwchensenwen
      become: yes
      become_method: sudo
      tasks:
        - name: make file task1
          file: path=/data/task1.txt state=touch
          notify: task1
        - name: make file task2
          file: path=/data/task2.txt state=touch
          notify: task2
    
      handlers:
        - name: task1
          file: path=/data/1.txt state=touch
        - name: task2
          file: path=/data/2.txt state=touch

    运行结果:

    从上图运行结果看出,Handlers执行的顺序与Handlers在playbook中定义的顺序是相同的,与“handler”被notify的顺序无关

    场景2:

    默认情况下,所有task执行完毕后,才会执行各个handler,并不是执行完某个task后,立即执行对应的handler,

    如果想要在执行完某些task后立即执行对应的handler,则需要使用meta模块

    ---
    - hosts: test
      remote_user: dwchensenwen
      become: yes
      become_method: sudo
      tasks:
        - name: make file task1
          file: path=/data/task1.txt state=touch
          notify: task1
        - meta: flush_handlers
    
        - name: make file task2
          file: path=/data/task2.txt state=touch
          notify: task2
    
      handlers:
        - name: task1
          file: path=/data/1.txt state=touch
        - name: task2
          file: path=/data/2.txt state=touch

    运行结果:

    修改程序配置文件的操作使用notify:restart nginx出发Handlers,从而实现nginx的重启

    tasks:
      - name: 修改nginx配置文件
        copy: src=./nginx.conf dest=/application/nginx/conf/nginx.conf
        notify: restart nginx
    
    handlers:
      - name: restart nginx
        service: name=nginx state=restarted

    若实现Handlers调用Handlers,则直接在Handlers中使用notify选项即可以

    handlers:
      - name: restart nginx
        service: name=nginx state=restarted
        notify: restart memcached
    
      - name: restart memcached
        service: name=memcached state=restarted
  • 相关阅读:
    C#磁吸屏幕窗体类库
    准备
    我写的诗
    How to turn off a laptop keyboard
    How to tell which commit a tag points to in Git?
    Why should I care about lightweight vs. annotated tags?
    How to get rid of “would clobber existing tag”
    Facebook, Google and Twitter threaten to leave Hong Kong over privacy law changes
    The need for legislative reform on secrecy orders
    Can a foreign key be NULL and/or duplicate?
  • 原文地址:https://www.cnblogs.com/mustark/p/11097751.html
Copyright © 2011-2022 走看看