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 -