Pcornel
Automation · Ansible

Handlers

Cheat sheet · https://pcornel.dk/topic.php?topic=5407

Back to the Topic

Answer

Ansible handlers are deferred tasks normally notified by changed tasks and executed later at defined flush points, allowing expensive restart/reload actions to occur only when notifications are queued.

Remember

  • Task changed ≠ handler executed; notification, later failure or play flow can prevent execution.
  • Handler notified multiple times ≠ handler runs multiple times by default; notifications are commonly coalesced.
  • Handler task success ≠ application ready after restart/reload.
  • No change ≠ no reload needed necessarily; out-of-band drift may exist outside notifier semantics.

First checks

  • notify: and handlers — restart on change
  • ansible-playbook site.yml --force-handlers — run handlers on failure
  • meta: flush_handlers — ordering

Verify in this order

task changed → notify → handler runs once

Where it usually breaks

Handler success proves the deferred Ansible action ran; it does not prove the service reached application-ready state.

Handlers Ansible boundary map

BoundaryHealthy evidenceSuspicious evidence
Inputs / selectionCorrect inventory, hosts, variables/facts and content are explicitWrong scope, stale facts, hidden precedence or content-version drift
ExecutionConnection, privilege, module/task context and ordering match intentUnreachable, wrong execution host, privilege/plugin failure or misleading skip/change state
Result semanticsPer-host/item results are preserved and interpreted correctlyAggregate recap hides partial/skipped/handled failures
Target proofResource/workload read-back and rerun confirm convergenceGreen Ansible status treated as end-to-end acceptance

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5407

Confirmation

Confirm action

Continue?