# Jinja2 template not getting parsed correctly when using include and with\_items

**URL:** <https://forum.ansible.com/t/jinja2-template-not-getting-parsed-correctly-when-using-include-and-with-items/24957>\
**Category:** Ansible Project\
**Created:** [July 11, 2017, 8:15pm UTC](https://forum.ansible.com/t/jinja2-template-not-getting-parsed-correctly-when-using-include-and-with-items/24957 "2017-07-11T20:15:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rasmus\_Sjorslev](https://avatars.discourse-cdn.com/v4/letter/r/5f8ce5/32.png) [@Rasmus\_Sjorslev](https://forum.ansible.com/u/Rasmus_Sjorslev)\
**Post date:** [July 11, 2017, 8:15pm UTC](https://forum.ansible.com/t/jinja2-template-not-getting-parsed-correctly-when-using-include-and-with-items/24957/1 "2017-07-11T20:15:18Z")

</div>

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 -

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 11, 2017, 8:27pm UTC](https://forum.ansible.com/t/jinja2-template-not-getting-parsed-correctly-when-using-include-and-with-items/24957/2 "2017-07-11T20:27:52Z")

</div>

> - name: Register VM information which will later be written to disk as YAML  
> &nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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  
> &nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 }} + ............

---

<div class="post-metadata">

**Author:** ![Rasmus\_Sjorslev](https://avatars.discourse-cdn.com/v4/letter/r/5f8ce5/32.png) [@Rasmus\_Sjorslev](https://forum.ansible.com/u/Rasmus_Sjorslev)\
**Post date:** [July 11, 2017, 8:40pm UTC](https://forum.ansible.com/t/jinja2-template-not-getting-parsed-correctly-when-using-include-and-with-items/24957/3 "2017-07-11T20:40:42Z")

</div>

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!
