Skip to content

Instantly share code, notes, and snippets.

@VireshDoshi
Created February 18, 2018 21:24
Show Gist options
  • Save VireshDoshi/d40e02a6668c5920b9708edc230656bc to your computer and use it in GitHub Desktop.
Save VireshDoshi/d40e02a6668c5920b9708edc230656bc to your computer and use it in GitHub Desktop.
[vdo023@localhost list_dicts]$ ansible-playbook -i "localhost," ./list_dicts-playbook.yml
PLAY [Test playbook for checking dictionaries] ****************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************
ok: [localhost]
TASK [Accessing direct dictionary] ****************************************************************************************************************
ok: [localhost] => {
"msg": "hostname is test and ip is 127.0.0.1"
}
TASK [Accessing list of dictionary] ***************************************************************************************************************
ok: [localhost] => (item={u'ip': u'127.0.0.2', u'hostname': u'test2'}) => {
"changed": false,
"item": {
"hostname": "test2",
"ip": "127.0.0.2"
},
"msg": "hostname is test2 and ip is 127.0.0.2"
}
ok: [localhost] => (item={u'ip': u'127.0.0.3', u'hostname': u'test3'}) => {
"changed": false,
"item": {
"hostname": "test3",
"ip": "127.0.0.3"
},
"msg": "hostname is test3 and ip is 127.0.0.3"
}
TASK [Accessing dictionary with one of the value as lists] ****************************************************************************************
ok: [localhost] => (item={u'ip': [u'127.0.0.4', u'127.0.0.5', u'127.0.0.6'], u'hostname': u'test4'})
PLAY RECAP ****************************************************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0
---
- name: Test playbook for checking dictionaries
hosts: localhost
connection: local
vars:
dict1: { hostname: 'test', ip: '127.0.0.1'}
list_dict2:
- { hostname: 'test2', ip: '127.0.0.2'}
- { hostname: 'test3', ip: '127.0.0.3'}
list_dict3:
- { hostname: 'test4', ip: [ '127.0.0.4', '127.0.0.5', '127.0.0.6' ] }
tasks:
- name: Accessing direct dictionary
debug:
msg: hostname is {{dict1.hostname}} and ip is {{dict1.ip}}
- name: Accessing list of dictionary
debug:
msg: "hostname is {{item.hostname}} and ip is {{ item.ip }}"
with_items: "{{ list_dict2 }}"
- name: Accessing dictionary with one of the value as lists
template:
src: host.j2
dest: target/message.txt
with_items: "{{ list_dict3 }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment