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

What does `run_once` do and what are its subtleties?

AdvancedConceptPlaybooks, Plays, Tasks and Handlers
Answer

run_once: true runs the task on the first host of the current batch only. The result is applied (registered) to all hosts in the batch, so later tasks can read it from any host.

Subtleties:

  • With serial, "first host of the batch" means it runs once per batch, not once per play.
  • It does not mean "on the controller"; combine with delegate_to: localhost for that.
  • If the first host is skipped or unreachable, the task may be skipped.
- ansible.builtin.command: /opt/app/bin/migrate-db
  run_once: true
All Ansible questions Roadmap: Ansible

More on Playbooks, Plays, Tasks and Handlers