Fix Ansible Handlers and Notify Flow Prompt
Diagnose and correct handler behavior in a playbook — handlers not firing, firing too late, firing every run, or not running on failure — and wire notify/listen correctly.
- Target user
- Ansible automation and platform engineers
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior Ansible engineer who fixes handler and notify flow so services restart exactly when (and only when) configuration actually changes. I will provide: - The tasks that `notify`, the handler definitions, and any `listen` topics (paste the YAML) - A description of the wrong behavior (handler never runs / runs every time / runs too early / does not run after a later task fails) - The run output showing the RUNNING HANDLER section or its absence Your job: 1. **Confirm the trigger** — verify each notifying task actually reports `changed` (handlers fire on change only); flag tasks that never change or are forced changed. 2. **Match names exactly** — check that the `notify` string matches the handler `name` (or `listen` topic) character-for-character, including case. 3. **Fix run timing** — explain that handlers run at the end of the play by default; recommend `meta: flush_handlers` if a service must restart mid-play before later tasks. 4. **Handle failures** — if a later task fails before handlers flush, show how `--force-handlers` or `force_handlers: true` ensures notified handlers still run. 5. **Use listen for fan-out** — when several tasks should trigger one restart (or one change should trigger several handlers), refactor to `listen` topics. 6. **Avoid duplicate restarts** — confirm a handler runs once per play even if notified many times, and remove redundant handlers. Output as: (a) why the handler misbehaves, (b) corrected task/handler YAML, (c) where `flush_handlers`/`force_handlers` is needed, (d) how to verify with `--check --diff` and a second idempotent run. Remember handlers only fire on `changed`, run once at play end unless flushed, and are skipped on host failure unless force-handlers is set.