# iteration over list of dictionaries.

**URL:** <https://forum.ansible.com/t/iteration-over-list-of-dictionaries/16839>\
**Category:** Ansible Project\
**Created:** [November 21, 2014, 2:59am UTC](https://forum.ansible.com/t/iteration-over-list-of-dictionaries/16839 "2014-11-21T02:59:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael.ibarra](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@michael.ibarra](https://forum.ansible.com/u/michael.ibarra)\
**Post date:** [November 21, 2014, 2:59am UTC](https://forum.ansible.com/t/iteration-over-list-of-dictionaries/16839/1 "2014-11-21T02:59:52Z")

</div>

Hi,

I’m trying to iterate over this list of dictionaries but I got:

“One or more undefined variables: ‘hostname’ is undefined”.

Task:

- name: LXC - creating directory  
file: path={{ lxc\_dir }}/{{ item.hostname}} owner=root group=root owner=755  
with\_items: containers

Vars:

lxc\_dir: “/var/lib/lxc”  
containers: [  
{  
hostname : “test”,  
lxc\_rootfs : ‘{{ lxc\_dir }}/{{ hostname }}/rootfs’,  
lxc\_mount : ‘{{ lxc\_dir }}/{{ hostname }}/fstab’,  
lxc\_utsname : ‘{{ hostname }}’,  
lxc\_arch : ‘{{ ansible\_architecture }}’,  
lxc\_ip\_addr : “10.0.3.101”,  
lxc\_prefix\_ip\_addr : “24”,  
lxc\_broadcast\_addr : “10.0.0.255”,  
lxc.network.ipv4 : ‘{{ lxc\_ip\_addr }}/{{ lxc\_prefix\_ip\_addr }} {{ lxc\_broadcast\_addr }}’,  
lxc.network.ipv4.gateway : “10.0.3.1”,  
lxc\_network\_type : ‘{{ | default(veth) }}’,  
lxc\_network\_flags : ‘{{ | default(up) }}’,  
lxc\_network\_link : ‘{{ | default(link) }}’  
},  
]

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 21, 2014, 3:04am UTC](https://forum.ansible.com/t/iteration-over-list-of-dictionaries/16839/2 "2014-11-21T03:04:27Z")

</div>

try item[0].hostname

---

<div class="post-metadata">

**Author:** ![michael.ibarra](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@michael.ibarra](https://forum.ansible.com/u/michael.ibarra)\
**Post date:** [November 21, 2014, 3:49am UTC](https://forum.ansible.com/t/iteration-over-list-of-dictionaries/16839/3 "2014-11-21T03:49:53Z")

</div>

It doesn’t work.

I tried with:

- name: LXC - Debug  
debug: msg={{item.[0].hostname}}  
with\_items: containers

ok: [127.0.0.1] =\> (item={‘hostname’: ‘test’, ‘lxc\_utsname’: u’{{hostname}}‘, ‘lxc\_network\_flags’: u’{{| default(up)}}‘, ‘lxc\_arch’: u’x86\_64’, ‘lxc\_ip  
\_addr’: ‘10.0.3.101’, ‘lxc\_rootfs’: u’{{lxc\_dir}}/{{hostname}}/rootfs’, ‘lxc\_network\_link’: u’{{| default(link)}}‘, ‘lxc\_prefix\_ip\_addr’: ‘24’, ‘lxc\_br  
oadcast\_addr’: ‘10.0.0.255’, ‘lxc\_network\_type’: u’{{| default(veth)}}‘, ‘lxc\_mount’: u’{{lxc\_dir}}/{{hostname}}/fstab’, ‘lxc.network.ipv4’: u’{{lxc\_ip  
\_addr}}/{{lxc\_prefix\_ip\_addr}} {{lxc\_broadcast\_addr}}', ‘lxc.network.ipv4.gateway’: ‘10.0.3.1’}) =\> {  
“item”: {  
“hostname”: “test”,  
“lxc.network.ipv4”: “{{lxc\_ip\_addr}}/{{lxc\_prefix\_ip\_addr}} {{lxc\_broadcast\_addr}}”,  
“lxc.network.ipv4.gateway”: “10.0.3.1”,  
“lxc\_arch”: “x86\_64”,  
“lxc\_broadcast\_addr”: “10.0.0.255”,  
“lxc\_ip\_addr”: “10.0.3.101”,  
“lxc\_mount”: “{{lxc\_dir}}/{{hostname}}/fstab”,  
“lxc\_network\_flags”: “{{| default(up)}}”,  
“lxc\_network\_link”: “{{| default(link)}}”,  
“lxc\_network\_type”: “{{| default(veth)}}”,  
“lxc\_prefix\_ip\_addr”: “24”,  
“lxc\_rootfs”: “{{lxc\_dir}}/{{hostname}}/rootfs”,  
“lxc\_utsname”: “{{hostname}}”  
},  
**“msg”: “{{item.[0].hostname}}”**  
}

No success obtaining the dictionary values.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 21, 2014, 4:00am UTC](https://forum.ansible.com/t/iteration-over-list-of-dictionaries/16839/4 "2014-11-21T04:00:37Z")

</div>

could you change the vars definition to:

containers:  
&nbsp;&nbsp;&nbsp;- hostname : "test"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lxc\_rootfs : '{{ lxc\_dir }}/{{ hostname }}/rootfs'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lxc\_mount : '{{ lxc\_dir }}/{{ hostname }}/fstab'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...

this should make item.hostname work

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 21, 2014, 4:54am UTC](https://forum.ansible.com/t/iteration-over-list-of-dictionaries/16839/5 "2014-11-21T04:54:25Z")

</div>

never mind, it wasn't that, these are the problem:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lxc\_rootfs : '{{ lxc\_dir }}/{{ hostname }}/rootfs',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lxc\_mount : '{{ lxc\_dir }}/{{ hostname }}/fstab',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lxc\_utsname : '{{ hostname }}',

the 'hostname' variable you are referencing there does not exist. If  
you are trying to reference the key in the same dictionary, that will  
not work.

---

<div class="post-metadata">

**Author:** ![michael.ibarra](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@michael.ibarra](https://forum.ansible.com/u/michael.ibarra)\
**Post date:** [November 21, 2014, 11:02am UTC](https://forum.ansible.com/t/iteration-over-list-of-dictionaries/16839/6 "2014-11-21T11:02:32Z")

</div>

It worked.

- name: LXC - Debug  
debug: msg={{ item.hostname }}  
with\_items: containers

containers: [  
{  
hostname : “test”,  
lxc\_rootfs : “{{ lxc\_dir }}/test/rootfs”,  
lxc\_mount : “{{ lxc\_dir }}/test/fstab”,  
lxc\_utsname : “test”,  
lxc\_arch : “{{ ansible\_architecture }}”,  
lxc\_network\_ipv4 : “[10.0.3.103/24](http://10.0.3.103/24) 10.0.3.255”,  
lxc\_network\_ipv4\_gateway : “10.0.3.1”,  
lxc\_network\_type : “{{ | default(veth) }}”,  
lxc\_network\_flags : “{{ | default(up) }}”,  
lxc\_network\_link : “{{ | default(link) }}”,  
},  
]

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![rektide](https://avatars.discourse-cdn.com/v4/letter/r/a88e57/32.png) [@rektide](https://forum.ansible.com/u/rektide)\
**Post date:** [November 21, 2014, 4:04pm UTC](https://forum.ansible.com/t/iteration-over-list-of-dictionaries/16839/7 "2014-11-21T16:04:51Z")

</div>

{{item[0].hostname}}

no dot, afaik

also, when reporting errors, helpfully provide the output you get too.
