I have the following list in Ansible and would like to get a flat structure. Input:
[
{
id: "one",
level: "3"
},
{
id: "two",
level: "8"
},
...
]
Desired output:
[
{
one: "3"
},
{
two: "8"
},
...
]
There is no easy way out of the box.
You can either use custom template filter:
my_list | map('template','{"<<item.id>>":"<<item.level>>"}',from_json=true) | list
Or generate new variable with set_fact + register + with_items, see Process complex variables with set_fact and with_items.
- set_fact:
tmp_item: '{ "{{ item.id }}": "{{ item.level }}" }'
with_items: "{{ my_list }}"
register: tmp_list
- debug:
msg: "{{ tmp_list.results | map(attribute='ansible_facts.tmp_item') | list }}"