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) }}’
},
]
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.
Brian_Coca
(Brian Coca)
4
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
Brian_Coca
(Brian Coca)
5
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.
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.
rektide
7
{{item[0].hostname}}
no dot, afaik
also, when reporting errors, helpfully provide the output you get too.