Execute command on the Ansible host - Ansible localhost
How to execute Ansible command(s) or task(s) on localhost using the connection plugin local and the right ansible internals variables.


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 demo 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
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
demo
How to Execute command on the Ansible host using connection: local method.
code
---
- name: localhost demo
hosts: localhost
vars:
ansible_connection: local
ansible_python_interpreter: "{{ ansible_playbook_python }}"
tasks:
- name: print hostname
ansible.builtin.debug:
msg: "{{ inventory_hostname }}"
execution
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 demo] *****************************************************************************
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
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 demo] *****************************************************************************
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 $
Recap
Now you know how to Execute commands and tasks on the Ansible localhost. You know how to use it based on your use case.
Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate