Ansible localhost & delegate_to: Run Tasks on Control Node Guide
By Luca Berton · Published 2024-01-01 · Category: installation
How to run Ansible tasks on localhost. Use connection local, delegate_to localhost, and local_action to execute on the Ansible control node.
How to Execute command on the Ansible host? When Ansible becomes part of your daily workflow it is natural you would like to automate also task in your local machine. I'm going to show you a live Playbook with some simple Ansible code. I'm Luca Berton and welcome to today's episode of Ansible Pilot.
Execute command on the Ansible host options • connection plugin • delegate_to: localhost • local_action
There are three ways to execute modules and commands on the Ansible Controller host. The first and my favorite is using the connection plugin local and applying it to the Ansible Play level of your Playbook. The tricky was is to adjust some ansible variables about the python interpreter. I consider it the best way nowadays. The second way is using the delegate_to at the Task level. This has the advantage to delegate only one task to localhost but still needs only the implicit localhost scheme. The third way is using the local_action statement. I personally don't like it but it's one alternative as well at Task level, so same as the previous.
Links • Controlling where tasks run: delegation and local actions • Implicit ‘localhost’
## Playbook
How to Execute command on the Ansible host using connection: local method.
code
execution
idempotency
Conclusion
Now you know how to Execute commands and tasks on the Ansible localhost. You know how to use it based on your use case.
Methods to Run on Localhost
Method 1: hosts: localhost
Method 2: delegate_to localhost
Method 3: local_action
Common Use Cases
Health check before deployment
Generate files locally, deploy remotely
Wait for service after deploy
localhost in Inventory
delegate_to vs connection: local
| Approach | When to Use | |----------|-------------| | hosts: localhost | Entire play runs locally | | delegate_to: localhost | Single task runs locally | | connection: local | Set on play or in inventory | | local_action: | Shorthand for delegate_to localhost |
FAQ
Why does localhost need connection: local?
Without it, Ansible tries to SSH to localhost, which may fail or be slow. connection: local runs directly without SSH.
How do I run on localhost AND remote in the same play?
Use delegate_to for specific tasks:
Does delegate_to pass remote host variables?
Yes - the task has access to the remote host's variables even though it runs locally. That's why you can use {{ inventory_hostname }} in delegated tasks.
Run Entire Play on Localhost
delegate_to localhost
Local Actions
Common Use Cases
API Calls from Controller
Generate Files Locally
Run Local Script Before Remote Tasks
Wait for Remote Service from Controller
Inventory for Localhost
connection: local vs delegate_to
| Feature | connection: local | delegate_to: localhost | |---------|-------------------|------------------------| | Scope | Entire play | Single task | | Host context | localhost | Original host's vars | | Use case | All-local plays | Mixed local/remote | | Facts | Local facts | Remote host facts |
FAQ
Why use localhost instead of a remote host?
For API calls, file generation, notifications, and any task that should run on the Ansible controller rather than remote hosts.
Does delegate_to affect variables?
The task runs on localhost but still has access to the original host's variables (facts, hostvars). This is useful for API calls that reference remote host info.
Can I gather facts for localhost?
Run on localhost
delegate_to localhost
local_action
Common Use Cases
API Calls from Controller
Download Then Upload
Generate Config Locally, Deploy Remotely
Wait for Service After Deploy
run_once with delegate_to
localhost in Inventory
FAQ
connection: local vs delegate_to: localhost?
connection: local runs the entire play locally. delegate_to: localhost runs a single task locally while the play targets remote hosts.
Does delegate_to have access to remote facts?
Yes — hostvars[inventory_hostname] still refers to the remote host's facts, even when delegated to localhost.
How to avoid "changed" on localhost tasks?
Use changed_when: false for read-only tasks like API checks or file reads.
Related Articles • Ansible Become Guide • Ansible Inventory Guide
Category: installation
Watch the video: Ansible localhost & delegate_to: Run Tasks on Control Node Guide — Video Tutorial