Ansible Series

⏳ Understanding Ansible Handler Timing From a Top 1% Perspective — notify Isn't 'Execute Immediately,' It's 'Reserve for Later'

Introduction

This article is part of the Top 1% Series Complete Article Guide, and article 3.1 in the Ansible/IaC Series.

Prerequisite Knowledge

Getting the Big Picture

When a task notifies a handler, that handler doesn’t run the instant it’s notified. Ansible’s default behavior is to flush notified handlers all at once, immediately after each section of a play finishes — pre_tasks, roles/tasks, and post_tasks. This is a “reservation,” not “execute immediately” — and by design, no matter how many times the same handler gets notified, it only ever runs once.

graph TB
    T1["Task 1 notifies"] --> Q["Handler reservation<br/>(not run yet)"]
    T2["Task 2 notifies"] --> Q
    Q --> End["End of section (or meta: flush_handlers)"]
    End --> H["Handler runs exactly once"]

Deep Dive Into the Fundamentals

flush_handlers Clears the Reservation Immediately

If you need an already-notified handler to run right now, mid-play, without waiting for the end of the section, insert a task with meta: flush_handlers. This task triggers whatever handlers have been notified up to that point, right on the spot. And don’t forget: a handler that’s already been flushed can still be notified again later in the play, making it eligible to run once more.

A Failed Task Stops the Play, Along With Any Notified Handlers

If a task fails partway through, the entire play stops, and any handlers notified before that point never run. Enabling the force_handlers setting (in the playbook itself, in ansible.cfg, or via --force-handlers) forces already-notified handlers to run even when a task has failed — useful when you still want cleanup to happen even after a failure.

Note: a variable in a handler name can backfire because of how early it gets templated

A handler’s name gets templated very early in the play, so if a variable used inside that name isn’t yet available at that point, the entire play can fail.

What a Pro Sees Here (Top 1% Understanding)

“Notified” and “Executed” Happen at Different Moments

notify isn’t a command to run a handler on the spot — it’s a command that registers a reservation: “run this later, all together.” Dropping the assumption that “the service must already be restarted after this task” and instead checking “when will the handler actually be processed next” — weighing both section boundaries and flush_handlers — is the perspective a top-1% engineer carries.

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. A task right after a config-file change ends up accessing the service while it’s still running on the old config: Insert meta: flush_handlers right after that task to settle the service restart immediately.
  2. A task fails partway through the play, leaving the service in a half-restarted state: Enable force_handlers so already-notified handlers still run even on failure.
  3. A play fails entirely when using a variable inside a handler name: Check whether that variable is already available at the point the handler name gets templated.

Summary

Takeaways to Apply Today

  1. If a task right after a config change accesses that same service, always check whether meta: flush_handlers is needed.
  2. For a play that needs cleanup after a failure, consider the force_handlers setting.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).