AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.
Popular Topics
About Luca Berton
Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.
Ansible localhost & delegate_to: Run Tasks on Control Node Guide — Video Tutorial
How to run Ansible tasks on localhost. Use connection local, delegate_to localhost, and local_action to execute on the Ansible control node.
What You'll Learn
- How to Execute command on the Ansible host?
- Execute command on the Ansible host options
- Links
- code
- execution
- idempotency
- Conclusion
- Methods to Run on Localhost
- Method 1: hosts: localhost
- Method 2: delegate_to localhost
Full Tutorial Content
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](https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html)
- [Implicit ‘localhost’](https://docs.ansible.com/ansible/latest/inventory/implicit_localhost.html)
## Playbook
How to Execute command on the Ansible host using connection: local method.
code
```yaml
---
- name: localhost Playbook
hosts: localhost
vars:
ansible_connection: local
ansible_python_interpreter: "{{ ansible_playbook_python }}"
tasks:
- name: print hostname
ansible.builtin.debug:
msg: "{{ inventory_hostname }}"
```
execution
```bash
ansible-pilot $ ansible-playbook ansible\ statements/localhost.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'
PLAY [localhost Playbook] *****************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [localhost]
TASK [print hostname] *****************************************************************************
ok: [localhost] => {
"msg": "localhost"
}
PLAY RECAP ****************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
```
idempotency
```bash
ansible-pilot $ ansible-playbook ansible\ statements/localhost.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'
PLAY [localhost Playbook] *****************************************************************************
TASK [Gathering Facts] *****************
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 8 min
- Category: installation
Read the full written article: Ansible localhost & delegate_to: Run Tasks on Control Node Guide
Related Video Tutorials
- Ansible 'Failed to Connect via SSH localhost:22': Fix Guide — Fix Ansible 'failed to connect to the host via ssh localhost port 22' error. Resolve SSH config, connection type, host key, and authentication issues.
- Ansible vs ansible-core: Package Differences Explained (2026) — Understand the difference between ansible and ansible-core packages. Compare contents, versions, installation, and choose the right package for your needs.
- Ansible Fix 'Role Not Found' Error: Path & Resolution Guide — Fix Ansible role not found error. Configure roles_path, install from Galaxy, check directory structure, and resolve common role loading issues.
- Ansible Read Files: lookup, slurp & fetch Module Guide — How to read files with Ansible. Use lookup plugin, slurp module, fetch module, and file content in conditionals with practical examples.
- ansible.builtin.git Module: Clone & Checkout Git Repositories (Guide) — How to clone and checkout Git repositories with Ansible git module (ansible.builtin.git). Deploy code from GitHub, manage branches, tags, force updates.
- Ansible git Clone via SSH: Deploy Keys & Repository Guide — How to clone git repositories via SSH with Ansible. Configure deploy keys, accept host keys, manage private repos, and automate git-based deployments.