have you every tryed to create a List of Dictionarys in Ansible?
For one of my projects, I need to create such a list. See the following two tasks:
- name: create List
ansible.builtin.set_fact:
LIST: [ {'size_gb': '{{ A[0] }}', 'type': 'eagerzeroedthick', 'autoselect_datastore': 'True'} ]
delegate_to: localhost
- name: fill up the List
ansible.builtin.set_fact:
LIST: "{{ LIST + [ {'size_gb': '{{ item }}', 'type': 'eagerzeroedthick', 'autoselect_datastore': 'True'} ] }}"
loop: "{{ A }}"
when: A|length >= 2
delegate_to: localhost
The problem, the secound task creates in the first run a dictionary, where the object {{ item }} is used as string instead of as placeholder. See following output:
TASK [create List] *********************************************************************************
task path: /ansible/playbooks.yml:81
ok: [CGN449 -> localhost] => {
"ansible_facts": {
"LIST": [
{
"autoselect_datastore": "True",
"size_gb": "10",
"type": "eagerzeroedthick"
}
]
},
"changed": false
}
TASK [fill up the List] *********************************************************************************
task path: /ansible/playbooks.yml:86
ok: [CGN449-> localhost] => (item=10) => {
"ansible_facts": {
"LIST": [
{
"autoselect_datastore": "True",
"size_gb": "10",
"type": "eagerzeroedthick"
},
{
"autoselect_datastore": "True",
"size_gb": "{{ item }}",
"type": "eagerzeroedthick"
}
]
},
"ansible_loop_var": "item",
"changed": false,
"item": "10"
}
ok: [CGN449 -> localhost] => (item=20) => {
"ansible_facts": {
"LIST": [
{
"autoselect_datastore": "True",
"size_gb": "10",
"type": "eagerzeroedthick"
},
{
"autoselect_datastore": "True",
"size_gb": "20",
"type": "eagerzeroedthick"
},
{
"autoselect_datastore": "True",
"size_gb": "{{ item }}",
"type": "eagerzeroedthick"
}
]
},
"ansible_loop_var": "item",
"changed": false,
"item": "20"
}
ok: [CGN449 -> localhost] => (item=30) => {
"ansible_facts": {
"LIST": [
{
"autoselect_datastore": "True",
"size_gb": "10",
"type": "eagerzeroedthick"
},
{
"autoselect_datastore": "True",
"size_gb": "20",
"type": "eagerzeroedthick"
},
{
"autoselect_datastore": "True",
"size_gb": "30",
"type": "eagerzeroedthick"
},
{
"autoselect_datastore": "True",
"size_gb": "{{ item }}",
"type": "eagerzeroedthick"
}
]
},
"ansible_loop_var": "item",
"changed": false,
"item": "30"
}
ok: [CGN449 -> localhost] => (item=40) => {
"ansible_facts": {
"LIST": [
{
"autoselect_datastore": "True",
"size_gb": "10",
"type": "eagerzeroedthick"
},
{
"autoselect_datastore": "True",
"size_gb": "20",
"type": "eagerzeroedthick"
},
{
"autoselect_datastore": "True",
"size_gb": "30",
"type": "eagerzeroedthick"
},
{
"autoselect_datastore": "True",
"size_gb": "40",
"type": "eagerzeroedthick"
},
{
"autoselect_datastore": "True",
"size_gb": "{{ item }}",
"type": "eagerzeroedthick"
}
]
},
"ansible_loop_var": "item",
"changed": false,
"item": "40"
Actually, I use following Version:
ansible [core 2.16.3]
config file = None
configured module search path = ['/home/alphabeit/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
ansible collection location = /home/alphabeit/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.12.3 (main, Apr 10 2024, 05:33:47) [GCC 13.2.0] (/usr/bin/python3)
jinja version = 3.1.2
libyaml = True
So, why this happen? Does anyone have an idea and know how to fix it?
Best Regards
Alphabeit