Ansible Playbook Handlers

From bib. source

[…] handlers are basically glorified tasks that can be called by the notify[sic] option, but since they act as tasks themselves, they can chain themselves to other handlers[…]

That is to say, a handler is a task in a playbook that does not automatically execute while its play is being run: “Handlers will only be run if a task notifies the handler” (Geerling 2023, 85). Notifying a handler involves using the notify option in the notifying task.

This means that “if a task that would’ve notified the handlers is skipped […] the handler will not be run,” and that when a play fails on at least one host the handler won’t be run (Ibid). The latter can be overridden using the meta module as a separate task in the playbook or using the command line flag or option force-handlers when running the playbook (Ibid).

In addition (Ibid):

From bib. source

Handlers will run once, and only once, at the end of a play. […] to override this behavior and run handlers in the middle of a playbook, you can use the meta module to do so (e.g., - meta: flush_handlers).

That is, the execution of any handler occurs only once at the end of a play, regardless of where and how often that handler is placed in a playbook.

Ansible command_line command_lines ad-hoc_command ad-hoc_commands command_line_interface command_line_interfaces pllaybooks configuration_management configuration_file options commands flags


bibliography

  • “Ansible Playbooks - Beyond the Basics.” In Ansible for DevOps: Server and Configuration Management for Humans, 2nd ed., 84–122. Leanpub, 2023.