When Handlers Run & flush_handlers
Control end-of-play handler timing.
When Handlers Run & flush_handlers is a free Ansible Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Handlers Run at the End
By default, all notified handlers run once, at the very end of the play, after every task has finished. Not the moment they were notified.
Why Defer Them?
Deferring means a service restarts once even after many config changes. That avoids needless restarts mid-play and saves time.
Per Host, Not Global
Handler timing is tracked per host. Each host runs its own queued handlers at the end of the play, independent of the others.
The Late-Restart Problem
Sometimes a later task needs the service already restarted. Waiting until play end is too late, so you need to flush early.
Meet flush_handlers
The flush_handlers meta task forces all pending handlers to run right now, in the middle of the play.
- name: Run queued handlers now
ansible.builtin.meta: flush_handlersIt Is a meta Task
flush_handlers is a special meta action that controls Ansible itself rather than touching the remote host.
A Typical Use
Change config, flush so the service restarts, then run a smoke test task against the freshly restarted service.
- name: Update config
ansible.builtin.template:
src: app.j2
dest: /etc/app.conf
notify: Restart app
- ansible.builtin.meta: flush_handlers
- name: Check health
ansible.builtin.uri:
url: http://localhost/healthFailures and Handlers
If a host fails before play end, its pending handlers normally do not run. That is one reason an early flush can be safer.
force_handlers as a Safety Net
Set force_handlers to true on a play to run queued handlers even when a host fails later. Useful for cleanup restarts.
- hosts: web
force_handlers: true
tasks:
- ...Flush Sparingly
Reach for flush_handlers only when ordering truly requires it. Overusing it removes the batching benefit handlers give you.
The Timeline
Tasks run top to bottom, handlers wait in a queue, then they all fire at play end unless you flush them sooner. ⏱️
Quick Check
You must restart a service mid-play so a later task can hit it. What do you use?
Recap
Handlers normally run once at play end, per host. Use flush_handlers to fire them early, and force_handlers to survive failures. 🚀
Frequently asked questions
Is the “When Handlers Run & flush_handlers” lesson free?
Yes — the full text of “When Handlers Run & flush_handlers” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.
What will I learn in “When Handlers Run & flush_handlers”?
Control end-of-play handler timing. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ansible Academy?
No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “When Handlers Run & flush_handlers” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ansible Academy lesson?
Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Notify a Handler on Change
- Defining Handlers & Restart Patterns
- When Handlers Run & flush_handlers
- Listen: One Trigger, Many Handlers