What are the behaviours and gotchas of handlers?
IntermediateConceptPlaybooks, Plays, Tasks and Handlers
Answer
- A handler runs once per play per host, even if notified by ten tasks.
- It runs at the end of the play section (pre_tasks, tasks, post_tasks each flush), not right after the notifying task.
- Handlers run in the order they are defined, not the order they were notified.
- The notification string must match the handler
name(or one of itslistentopics) exactly. - A handler does not run if the notifying task did not change anything, which includes the case where a previous run changed the file but failed before the handler.
handlers:
- name: Restart web stack
ansible.builtin.service: { name: nginx, state: restarted }
listen: "restart web services"
listen lets many handlers respond to one topic.
More on Playbooks, Plays, Tasks and Handlers
What is a playbook?BeginnerWhat are the main keys of a play?BeginnerWhy should every task have a `name`?BeginnerWhich playbook flags help you inspect and debug before a real run?BeginnerWhat is a handler and how is it triggered?BeginnerHow do you run handlers immediately instead of at the end of the play?Intermediate