How do I write an Ansible handler with multiple tasks?

前端 未结 3 442
青春惊慌失措
青春惊慌失措 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:31

    Edit: If you have Ansible 2.2 or above, use mkadan's answer. The answer below does not work with newer versions of Ansible. Also note that as per Enis Afgan's comment below, due to a bug, this answer does not work with Ansible versions between 2.0.2 and 2.1.2.


    As of Ansible 2.0, you can use an include action in your handler to run multiple tasks.

    For example, put your tasks in a separate file restart_tasks.yml (if you use roles, that would go into the tasks subdirectory, not in the handlers subdirectory):

    - name: Restart conditionally step 1
      shell: check_is_started.sh
      register: result
    
    - name: Restart conditionally step 2
      service: name=service state=restarted
      when: result
    

    Your handler would then simply be:

    - name: Restart conditionally
      include: restart_tasks.yml
    

    Source: issue thread on github: https://github.com/ansible/ansible/issues/14270

提交回复
热议问题