Skip to content

Instantly share code, notes, and snippets.

@mohclips
Created January 28, 2017 12:28
Show Gist options
  • Save mohclips/e331fb8ef73455dfa77bfb5076b38e5a to your computer and use it in GitHub Desktop.
Save mohclips/e331fb8ef73455dfa77bfb5076b38e5a to your computer and use it in GitHub Desktop.
Testing delegation to localhost
# vim: noai:ts=2:sw=2:et
---
- name: "testing hosts and delgation"
# ansible-playbook --connection=local -i "localhost,ubuntu" a.yml
hosts: all
gather_facts: no
tasks:
- name: "always run"
debug: msg="inventory_hostname is {{inventory_hostname}}"
- name: "only run this task when on localhost"
debug: msg="we are localhost"
when: "{{ inventory_hostname == 'localhost' }}"
- name: "delegate to 127.0.0.1 if localhost or no delegate"
command: hostname
delegate_to: "{{ '127.0.0.1' if inventory_hostname == 'localhost' else omit }}"
register: cmd
- debug: var=cmd.stdout
- name: "delegate to 127.0.0.1 if ubuntu or no delegate"
command: hostname
delegate_to: "{{ '127.0.0.1' if inventory_hostname == 'ubuntu' else omit }}"
register: cmd
- debug: var=cmd.stdout
nick@nick-Lenovo-Z710:~/workspace/openstack/k5-infra2$ ansible-playbook --connection=local -i "localhost,ubuntu" a.yml
PLAY [testing hosts and delgation] *********************************************
TASK [always run] **************************************************************
ok: [localhost] => {
"msg": "inventory_hostname is localhost"
}
ok: [ubuntu] => {
"msg": "inventory_hostname is ubuntu"
}
TASK [only run this task when on localhost] ************************************
ok: [localhost] => {
"msg": "we are localhost"
}
skipping: [ubuntu]
TASK [delegate to 127.0.0.1 if localhost or no delegate] ***********************
changed: [localhost -> 127.0.0.1]
changed: [ubuntu]
TASK [debug] *******************************************************************
ok: [localhost] => {
"cmd.stdout": "nick-Lenovo-Z710"
}
ok: [ubuntu] => {
"cmd.stdout": "TX200-S5"
}
TASK [delegate to 127.0.0.1 if ubuntu or no delegate] **************************
changed: [ubuntu -> 127.0.0.1]
changed: [localhost]
TASK [debug] *******************************************************************
ok: [localhost] => {
"cmd.stdout": "nick-Lenovo-Z710"
}
ok: [ubuntu] => {
"cmd.stdout": "nick-Lenovo-Z710"
}
PLAY RECAP *********************************************************************
localhost : ok=6 changed=2 unreachable=0 failed=0
ubuntu : ok=5 changed=2 unreachable=0 failed=0
delegate to 127.0.0.1 if localhost or no delegate ----------------------- 0.33s
delegate to 127.0.0.1 if ubuntu or no delegate -------------------------- 0.14s
only run this task when on localhost ------------------------------------ 0.04s
always run -------------------------------------------------------------- 0.03s
------------------------------------------------------------------------ 0.03s
Playbook finished: Sat Jan 28 12:25:54 2017, 5 total tasks. 0:00:00 elapsed.
@pablogrs
Copy link

cool stuff, thanks

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