Jinja2 template not getting parsed correctly when using include and with_items

61 views
Skip to first unread message

Rasmus Sjørslev

unread,
Jul 11, 2017, 4:15:18 PM7/11/17
to Ansible Project
Im adding to an empty array/list defined in groups_vars/all as:
vminfo: []

the playbook creates new VMs and i need to register some information for later reference (teardown) so i add the relevant info to this vminfo list insude my include with_items as such:

- name: Register VM information which will later be written to disk as YAML
  set_fact
:
    vminfo
: "{{ vminfo + [ {'name': '{{ item.hostname }}', 'ip': '{{ free_ip.json.data | ipv4 }}', 'uuid': '{{ vm_creation.instance.hw_product_uuid }}'} ] }}"




The first time this gets interpreted / parsed it looks fine and when i debug it shows as:

TASK [common : DEBUG | Print vminfo list after set_fact] *************************************************************************************************************************
ok
: [localhost] => {
   
"msg": [
       
{
           
"ip": "192.168.111.26",
           
"name": "swarm01",
           
"uuid": "4218386c-7c2b-a2d1-0f80-22e7c14ff4d1"
       
}
   
]
}


however when it comes around to create the second VM this happens:

TASK [common : DEBUG | Print vminfo list after set_fact] *************************************************************************************************************************
ok
: [localhost] => {
   
"msg": [
       
{
           
"ip": "192.168.111.27",
           
"name": "swarm02",
           
"uuid": "42189c05-6fb7-2fdb-108a-4ca980844f4d"
       
},
       
{
           
"ip": "{{ free_ip.json.data | ipv4 }}",
           
"name": "{{ item.hostname }}",
           
"uuid": "{{ vm_creation.instance.hw_product_uuid }}"
       
}
   
]
}

the first object in the array/list is being replaced with the 2nd VM info and then it looks like it's no longer parsing the jinja2 expression correctly.
when it comes around to create the 3rd vm the node the just wrote over the 1st VM is still there but a 3rd object is added which is also not being parsed correctly:

TASK [common : DEBUG | Print vminfo list after set_fact] *************************************************************************************************************************
ok
: [localhost] => {
   
"msg": [
       
{
           
"ip": "192.168.111.27",
           
"name": "swarm02",
           
"uuid": "42189c05-6fb7-2fdb-108a-4ca980844f4d"
       
},
       
{
           
"ip": "{{ free_ip.json.data | ipv4 }}",
           
"name": "{{ item.hostname }}",
           
"uuid": "{{ vm_creation.instance.hw_product_uuid }}"
       
},
       
{
           
"ip": "{{ free_ip.json.data | ipv4 }}",
           
"name": "{{ item.hostname }}",
           
"uuid": "{{ vm_creation.instance.hw_product_uuid }}"
       
}
   
]
}

i do something similar as a separate task where i add just strings to an array/list which i use with add_host later but this does not behave as the above:

- name: Register the assigned IP address for later use
  set_fact
:
    new_vms
: "{{ new_vms }} + [ '{{ free_ip.json.data | ipv4 }}' ]"

this results in a regular array

['192.168.0.1', '192.168.0.2', '192.168.0.3']

even though im referencing the same attribute.

is this a bug and/or is there a better way of achieving what i am trying to do in terms of building out this array with the data i need collected?

thanks - 


Kai Stian Olstad

unread,
Jul 11, 2017, 4:28:11 PM7/11/17
to ansible...@googlegroups.com
On 11. juli 2017 22:15, Rasmus Sjørslev wrote:
> - name: Register VM information which will later be written to disk as YAML
> set_fact:
> vminfo: "{{ vminfo + [ {'name': '{{ item.hostname }}', 'ip': '{{
> free_ip.json.data | ipv4 }}', 'uuid': '{{
> vm_creation.instance.hw_product_uuid }}'} ] }}"

<snip />

> - name: Register the assigned IP address for later use
> set_fact:
> new_vms: "{{ new_vms }} + [ '{{ free_ip.json.data | ipv4 }}' ]"

Well, they have not the same structure.
Have you tried writing the first one in the same manner that the second
one is?

{{ vminfo }} + ............

--
Kai Stian Olstad

Rasmus Sjørslev

unread,
Jul 11, 2017, 4:40:42 PM7/11/17
to Ansible Project, ansible-pr...@olstad.com
that worked! it's getting late here apparently...

vminfo: "{{ vminfo }} + [ {'name': 'test', 'ip': '{{ ansible_date_time.date }}', 'uuid': 'uuid-of-some-sort'} ]"

instead of:

vminfo: "{{ vminfo + [ {'name': 'test', 'ip': '{{ ansible_date_time.date }}', 'uuid': 'uuid-of-some-sort'} ] }}"

thank you so much once again!
Reply all
Reply to author
Forward
0 new messages