Interview Prep/Ansible/Playbook Design, Handlers and Idempotency

You need a task to run once per play, not once per host, such as sending a Slack notification.

IntermediateScenarioPlaybook Design, Handlers and Idempotency
Answer

Situation read → A play-wide side effect.

What I'd do →

- ansible.builtin.uri:
    url: "{{ slack_webhook }}"
    method: POST
    body_format: json
    body: { text: "Deploy finished" }
  run_once: true
  delegate_to: localhost

Why → run_once executes on the first host in the batch; delegate_to: localhost makes the call from the controller.

Interview tip → With serial, run_once fires once per batch; use a condition on ansible_play_hosts if you need once overall.

All Ansible questions Roadmap: Ansible

More on Playbook Design, Handlers and Idempotency