Interview Prep/Ansible/Rolling Updates, Delegation and Safe Deployments

How do you perform a one-time action during a multi-host play?

IntermediateProductionRolling Updates, Delegation and Safe Deployments
Answer

run_once: true, usually with delegate_to for an action on a specific host.

- name: Clear CDN cache after the whole batch
  ansible.builtin.uri:
    url: https://cdn.example.com/purge
    method: POST
  delegate_to: localhost
  run_once: true

Remember that with serial, run_once runs once per batch, not once per play. To run only at the very end, put the action in a later play (hosts: localhost) or use when: inventory_hostname == ansible_play_hosts_all[-1].

All Ansible questions Roadmap: Ansible

More on Rolling Updates, Delegation and Safe Deployments