Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible handler does not run multiple handler tasks

Tags:

We have one Ansible role that needs to run three tasks in the handlers/main.yml task file, but it only runs the first task. How do I force it to run the other two tasks? I do have the ignore flag on for if the first task fails.

The tasks/main.yml file looks like:

- name: openfire | Copy plugins into openfire/plugins   copy: src={{ srcdir }}/xmpp/{{ item }} dest=${bindir}/openfire/plugins/{{ item }}   with_items:    - x.jar    - y.jar   sudo: yes   sudo_user: ${tomcat_user}   notify: restart openfire  - name: openfire | Copy jars into openfire/lib   copy: src={{ srcdir }}/xmpp/{{ item }} dest=${bindir}/openfire/lib/{{ item }}   with_items:    - a.jar    - b.jar   sudo: yes   sudo_user: ${tomcat_user}   notify: restart openfire 

The handlers/main.yml file looks like:

- name: restart openfire   service: name=openfire state=stopped   ignore_errors: true   sudo: yes  - name: restart openfire   file: path=/var/run/openfire.pid state=absent   sudo: yes  - name: restart openfire   service: name=openfire state=restarted enabled=yes   sudo: yes 

Only the first handler task (shut down openfire) runs.

like image 724
Nova Avatar asked Jan 27 '14 19:01

Nova


People also ask

How do I run multiple tasks in Ansible?

If you need to execute a task with Ansible more than once, write a playbook and put it under source control. Then you can use the playbook to push out new configuration or confirm the configuration of remote systems.

Can a handler notify another handler?

Handlers are executed in the order they are defined in the handlers section, not in the order listed in the notify statement. Notifying the same handler multiple times will result in executing the handler only once regardless of how many tasks notify it.

How do handlers work in Ansible?

In a nutshell, handlers are special tasks that only get executed when triggered via the notify directive. Handlers are executed at the end of the play, once all tasks are finished. In Ansible, handlers are typically used to start, reload, restart, and stop services.


2 Answers

Its possible for handler to call another notify. Multiple notify calls are also allowed:

--- - name: restart something   command: shutdown.sh    notify:     - wait for stop     - start something     - wait for start  - name: wait for stop   wait_for: port={{port}} state=stopped  - name: start something   command: startup.sh  - name: wait for start   wait_for: port={{port}} state=started 
like image 131
iTake Avatar answered Oct 20 '22 11:10

iTake


As of Ansible 2.2, you can now notify multiple handlers at the same time using the listen directive:

- name: stop openfire   listen: restart openfire   service: name=openfire state=stopped   ignore_errors: true   sudo: yes  - name: remove openfire pid file   listen: restart openfire   file: path=/var/run/openfire.pid state=absent   sudo: yes  - name: restart openfire   listen: restart openfire   service: name=openfire state=restarted enabled=yes   sudo: yes 
like image 23
Xiong Chiamiov Avatar answered Oct 20 '22 13:10

Xiong Chiamiov