How do I write an Ansible handler with multiple tasks?

Question:

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:

Answer:

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:

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:

and in the task, notify handlers via ‘Restart processes’

https://docs.ansible.com/ansible/latest/user_guide/playbooks_handlers.html#naming-handlers

Leave a Reply