Skip to content

Instantly share code, notes, and snippets.

@halberom
Last active July 10, 2018 06:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halberom/bfac31287c0405a3df129d022a4ef318 to your computer and use it in GitHub Desktop.
Save halberom/bfac31287c0405a3df129d022a4ef318 to your computer and use it in GitHub Desktop.
ansible - example of using default var or result from nested dict
PLAY [localhost] ***************************************************************************************************************************************************************************************************************************************************************
TASK [debug] *******************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "a exists!"
}
PLAY [this should work, it doesn't] ********************************************************************************************************************************************************************************************************************************************
TASK [debug] *******************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": ""
}
PLAY [this works - but uses unoffically exposed 'vars'] ************************************************************************************************************************************************************************************************************************
TASK [debug] *******************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "bcde exists!"
}
PLAY [this works] **************************************************************************************************************************************************************************************************************************************************************
TASK [set_fact] ****************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug] *******************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "bcde exists!"
}
PLAY RECAP *********************************************************************************************************************************************************************************************************************************************************************
localhost : ok=5 changed=0 unreachable=0 failed=0
---
- name: testing for a
hosts: localhost
gather_facts: False
connection: local
vars:
a: a exists!
tasks:
- debug:
msg: "{{ a|default(inventory_hostname|extract(hostvars, ['b', 'c', 'd', 'e'])) }}"
- name: testing for b: this should work, it doesn't
hosts: localhost
gather_facts: False
connection: local
vars:
b:
c:
d:
e: 'bcde exists!'
tasks:
- debug:
msg: "{{ a|default(inventory_hostname|extract(hostvars, ['b', 'c', 'd', 'e'])) }}"
- name: testing for b: this works - but uses unoffically exposed 'vars'
hosts: localhost
gather_facts: False
connection: local
vars:
b:
c:
d:
e: 'bcde exists!'
tasks:
- debug:
msg: "{{ a|default('b'|extract(vars, ['c', 'd', 'e'])) }}"
- name: testing for b: this works
hosts: localhost
gather_facts: False
connection: local
tasks:
- set_fact:
b:
c:
d:
e: 'bcde exists!'
- debug:
msg: "{{ a|default(inventory_hostname|extract(hostvars, ['b', 'c', 'd', 'e'])) }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment