Interview Prep/Ansible/Playbooks, Plays, Tasks and Handlers

How do you run handlers immediately instead of at the end of the play?

IntermediateConceptPlaybooks, Plays, Tasks and Handlers
Answer

Use the meta: flush_handlers task. All pending notifications run at that point.

- ansible.builtin.template:
    src: app.conf.j2
    dest: /etc/app/app.conf
  notify: Restart app

- ansible.builtin.meta: flush_handlers

- ansible.builtin.uri:
    url: http://localhost:8080/health
    status_code: 200

Use this when a following task depends on the service already running the new config, such as a health check or a database migration.

All Ansible questions Roadmap: Ansible

More on Playbooks, Plays, Tasks and Handlers