iteration over list of dictionaries.

56 views
Skip to first unread message

Luis M. Ibarra

unread,
Nov 20, 2014, 9:59:56 PM11/20/14
to ansible...@googlegroups.com

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) }}'
    }, 
]


--
Luis M. Ibarra

Brian Coca

unread,
Nov 20, 2014, 10:04:30 PM11/20/14
to ansible...@googlegroups.com
try item[0].hostname



--
Brian Coca

Luis M. Ibarra

unread,
Nov 20, 2014, 10:49:57 PM11/20/14
to ansible...@googlegroups.com
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.


try item[0].hostname



--
Brian Coca

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8m%2BLoNo7xDxWm_hC2trEXCN11ttZ9nSm%3DfVsUBScpdnoA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.



--
Luis M. Ibarra

Brian Coca

unread,
Nov 20, 2014, 11:00:43 PM11/20/14
to ansible...@googlegroups.com
could you change the vars definition to:

containers:
- hostname : "test"
lxc_rootfs : '{{ lxc_dir }}/{{ hostname }}/rootfs'
lxc_mount : '{{ lxc_dir }}/{{ hostname }}/fstab'
...


this should make item.hostname work
> https://groups.google.com/d/msgid/ansible-project/CAFyQ8TSeymhgKzGLzeL-hzZZWkkDo%3DBK4hRA2X1i5tNmYnpMYA%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

Brian Coca

unread,
Nov 20, 2014, 11:54:31 PM11/20/14
to ansible...@googlegroups.com
never mind, it wasn't that, these are the problem:

lxc_rootfs : '{{ lxc_dir }}/{{ hostname }}/rootfs',
lxc_mount : '{{ lxc_dir }}/{{ hostname }}/fstab',
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.


--
Brian Coca

Luis M. Ibarra

unread,
Nov 21, 2014, 6:02:36 AM11/21/14
to ansible...@googlegroups.com
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 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.



--
Brian Coca

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Luis M. Ibarra

rek...@voodoowarez.com

unread,
Nov 21, 2014, 11:04:56 AM11/21/14
to ansible...@googlegroups.com
On Thu, Nov 20, 2014 at 10:49:53PM -0500, Luis M. Ibarra wrote:
> - name: LXC - Debug
> debug: msg={{item.[0].hostname}}
> with_items: containers

{{item[0].hostname}}

no dot, afaik

also, when reporting errors, helpfully provide the output you get too.
Reply all
Reply to author
Forward
0 new messages