Skip to content

Instantly share code, notes, and snippets.

@moreati
Created May 15, 2024 10:25
Show Gist options
  • Save moreati/8c88021135e9fef03e0e7296ca697252 to your computer and use it in GitHub Desktop.
Save moreati/8c88021135e9fef03e0e7296ca697252 to your computer and use it in GitHub Desktop.
Different ways to execute Ansible tasks on the controller
- hosts: targets
gather_facts: false
vars:
ansible_python_interpreter: python3
tasks:
- command: echo hello
delegate_to: localhost
- command: echo hello
connection: local
- local_action: command echo hello
@moreati
Copy link
Author

moreati commented May 15, 2024

TIL In addition to delegate_to and local_action there is also connection: local to execute Ansible playbook on the controller, rather than the usual targets. It comes with its own set of caveats and quirks https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_delegation.html#local-playbooks

$ ansible-playbook -i inventory.yml locals.yml

PLAY [targets] *************************************************************************************

TASK [command] *************************************************************************************
changed: [u2204 -> localhost]
changed: [debian12 -> localhost]

TASK [command] *************************************************************************************
changed: [u2204]
changed: [debian12]

TASK [command] *************************************************************************************
changed: [debian12 -> localhost]
changed: [u2204 -> localhost]

PLAY RECAP *****************************************************************************************
debian12     : ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
u2204        : ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment