I am trying to create a dict from a var file from here and here, unsuccessfully.
I import a var file which looks like this (vm id is defined by the key)
---
k8s_vms:
888:
ip: 10.0.30.110
mac: ca:d1:23:45:4e:01
999:
ip: 10.0.30.111
mac: ca:d1:23:45:4e:02
...
In my playbook, I wrote this for debbuging :
pre_tasks:
- include_vars: 'vm_vars.yml'
[...]
- name: Populate dict (test with IP)
set_fact:
vms_infos: "{{ vms_infos | default([]) + [ {'id': item.key, 'ip': item.value.ip} ] }}"
with_items:
- "{{ k8s_vms }}"
- name: Debug
debug: var=vms_infos
- name: ID and IP
debug:
msg: "VM={{ item.id }}, IP={{ item.ip }}"
with_items: "{{ vms_infos }}"
I can't find a way to access the values, so my output is as follows :
TASK [DEBUG] ***********************************************************************************************************************
ok: [x.x.x.x] => {
"k8s_vms": {
"888": {
"ip": "10.0.30.110",
"mac": "ca:d1:23:45:4e:01"
},
"999": {
"ip": "10.0.30.111",
"mac": "ca:d1:23:45:4e:02"
}
}
}
TASK [Populate dict] ***************************************************************************************************************
ok: [x.x.x.x] => (item={888: {'ip': '10.0.30.110', 'mac': 'ca:d1:23:45:4e:01'}, 999: {'ip': '10.0.30.111', 'mac': 'ca:ca:d1:23:45:4e:02'}})
TASK [DEBUG] ***********************************************************************************************************************
ok: [x.x.x.x] => {
"vms_infos": "[{'id': AnsibleUndefined, 'ip': AnsibleUndefined}]"
}
TASK [ID and IP] ******************************************************************************************************************
fatal: [x.x.x.x]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'id'\n\nThe error appears to be in 'xxx/deploy.yml': line 31, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: ID and IP\n ^ here\n"}