Interview Prep/Ansible/Playbooks, Plays, Tasks and Handlers

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 its listen topics) 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.

All Ansible questions Roadmap: Ansible

More on Playbooks, Plays, Tasks and Handlers