0

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"
},
...
]

1 Answer 1

1

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 }}"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.