Interview Prep/Ansible/Rolling Updates, Delegation and Orchestration

You want one task to run only on the last host of a batch.

AdvancedScenarioRolling Updates, Delegation and Orchestration
Answer

Situation read → Batch-end action.

What I'd do →

- ansible.builtin.debug: { msg: "last in batch" }
  when: inventory_hostname == ansible_play_batch[-1]

Why → ansible_play_batch lists the hosts in the current serial batch, and ansible_play_hosts lists all active hosts in the play.

Interview tip → run_once combined with serial is the more common route.

All Ansible questions Roadmap: Ansible

More on Rolling Updates, Delegation and Orchestration