How do I write an Ansible handler with multiple ta

2020-05-11 10:58发布

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 restarts a service only if already started:

- name: Restart conditionally
  shell: check_is_started.sh
  register: result

- name: Restart conditionally step 2
  service: name=service state=restarted
  when: result

3条回答
成全新的幸福
2楼-- · 2020-05-11 11:33

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

查看更多
霸刀☆藐视天下
3楼-- · 2020-05-11 11:53

In your handler file, chain the different steps together using notify.

- name: Restart conditionally
  debug: msg=Step1
  changed_when: True
  notify: Restart conditionally step 2

- name: Restart conditionally step 2
  debug: msg=Step2
  changed_when: True
  notify: Restart conditionally step 3

- name: Restart conditionally step 3
  debug: msg=Step3

Then refer to it from a task with notify: Restart conditionally.

Note that you can only notify to handlers below the current one. So for example, Restart conditionally step 2 can't notify Restart conditionally.

Source: #ansible at irc.freenode.net. I'm unsure whether this will continue to work in the future as it's not mentioned as a feature in the official documentation.

查看更多
淡お忘
4楼-- · 2020-05-11 11:58

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

查看更多
登录 后发表回答