delegate_to & run_once for Orchestration
Run a task on one host for many.
delegate_to & run_once for Orchestration is a free Ansible Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Tasks Do Not Always Run Where You Think
By default each task runs on its target host. For orchestration you sometimes need a task to run somewhere else entirely. 🎯
Meet delegate_to
The delegate_to keyword runs a task on a different host than the current one, while still using that host's variables.
- name: Add web node to load balancer
community.general.haproxy:
host: "{{ inventory_hostname }}"
state: enabled
delegate_to: lb01A Classic Use Case
While looping over web nodes, delegate the load balancer change to the LB host. The play stays per-node but the action lands on the LB.
Delegate to localhost
Run a task on your control node by delegating to localhost. Great for calling an API or sending a notification from one place.
- name: Notify chat
ansible.builtin.uri:
url: "{{ webhook }}"
method: POST
delegate_to: localhostVariables Stay With the Original Host
Delegation moves where a task runs, not whose facts it sees. inventory_hostname still names the original target, not the delegate.
Meet run_once
The run_once keyword runs a task a single time for the whole batch, not once per host. Ansible picks the first host to execute it.
- name: Run database migration
ansible.builtin.command: ./migrate.sh
run_once: trueWhy run_once Matters
Some actions must happen only once, like a migration or a leader election. run_once stops every host from repeating the same shared step.
Pair Them Together
Combine run_once with delegate_to to do one action on one chosen host, like running a migration from a single app node.
- name: Migrate db once
ansible.builtin.command: ./migrate.sh
run_once: true
delegate_to: app01Results Are Shared
With run_once, a registered result is shared across all hosts in the batch. They all see the output from that single execution.
- ansible.builtin.command: cat /etc/version
run_once: true
register: verGather Facts From a Delegate
Use delegate_facts if you want gathered facts to attach to the delegate host instead of the current one. Off by default.
Orchestration, Not Just Configuration
Together these keywords turn per-host config into true orchestration: coordinating one action across a fleet from a single point.
Quick Check
Let us check these orchestration keywords.
Recap
Use delegate_to to run a task on another host and run_once to run it just once per batch, the core of orchestration. ✅
Frequently asked questions
Is the “delegate_to & run_once for Orchestration” lesson free?
Yes — the full text of “delegate_to & run_once for Orchestration” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.
What will I learn in “delegate_to & run_once for Orchestration”?
Run a task on one host for many. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ansible Academy?
No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “delegate_to & run_once for Orchestration” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ansible Academy lesson?
Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Automating Network Devices with cli_command
- delegate_to & run_once for Orchestration
- Coordinate Web, App & DB Tiers
- Load Balancer Drain & Re-Enable