How do I write an Ansible handler with multiple tasks?

前端 未结 3 408
青春惊慌失措
青春惊慌失措 2021-01-30 04:08

In response to a change, I have multiple related tasks that should run. How do I write an Ansible handler with multiple tasks?

For example, I would like a handler that r

3条回答
  •  离开以前
    2021-01-30 04:34

    There is proper solution to this problem as of Ansible 2.2.

    handlers can also “listen” to generic topics, and tasks can notify those topics as follows:

    handlers:
        - name: restart memcached
          service: name=memcached state=restarted
          listen: "restart web services"
        - name: restart apache
          service: name=apache state=restarted
          listen: "restart web services"
    
    tasks:
        - name: restart everything
          command: echo "this task will restart the web services"
          notify: "restart web services"
    

    This use makes it much easier to trigger multiple handlers. It also decouples handlers from their names, making it easier to share handlers among playbooks and roles

    Specifically to the question, this should work:

    - name: Check if restarted
      shell: check_is_started.sh
      register: result
      listen: Restart processes
    
    - name: Restart conditionally step 2
      service: name=service state=restarted
      when: result
      listen: Restart processes
    

    and in the task, notify handlers via 'Restart processes'

    http://docs.ansible.com/ansible/playbooks_intro.html#handlers-running-operations-on-change

提交回复
热议问题