Interview Prep/Ansible/Playbook Design, Handlers and Idempotency

A handler needs to run in the middle of a play, before the next set of tasks.

BeginnerScenarioPlaybook Design, Handlers and Idempotency
Answer

Situation read → Ordering dependency on a handler.

What I'd do →

- ansible.builtin.template: { src: app.conf.j2, dest: /etc/app.conf }
  notify: Restart app
- ansible.builtin.meta: flush_handlers
- ansible.builtin.uri: { url: http://localhost:8080/health }

Why → flush_handlers runs pending handlers immediately instead of waiting for the end of the play.

Interview tip → Handlers also flush at the end of each of pre_tasks, roles/tasks and post_tasks sections.

All Ansible questions Roadmap: Ansible

More on Playbook Design, Handlers and Idempotency