Jai
Jai

Reputation: 177

How to manipulate a list of dictionaries in Ansible?

There is a list of dictionaries as shown below:

"test_list_of_dicts": [
{
  "inner_dict":[
    {"f1":"a","f2":"b"},
    {"f1":"c","f2":"d"}
  ],
  "id":"id1"
},
{
  "inner_dict":[
    {"f1":"e","f2":"f"},
    {"f1":"g","f2":"h"}
  ],
  "id":"id2"
}
]

I need to modify above structure and get a new list of dicts like below:

"new_list_of_dicts": [
    {"f1":"a","f2":"b","id":"id1"},
    {"f1":"c","f2":"d","id":"id1"},
    {"f1":"e","f2":"f","id":"id2"},
    {"f1":"g","f2":"h","id":"id2"}
]

I couldn't find a way to achieve this in Ansible.

Upvotes: 2

Views: 552

Answers (2)

Zeitounator
Zeitounator

Reputation: 44615

Basically the same example as in @vladimir's answer but without the need for a loop:

- name: manipulate dict
  hosts: localhost
  gather_facts: false

  vars:
    # Your var definition on a single line for legibility
    test_list_of_dicts: [{"inner_dict":[{"f1":"a","f2":"b"},{"f1":"c","f2":"d"}],"id":"id1"},{"inner_dict":[{"f1":"e","f2":"f"},{"f1":"g","f2":"h"}],"id":"id2"}]

    new_list_of_dicts: "{{ lookup('subelements', test_list_of_dicts, 'inner_dict')
      | map('combine') | list }}"

  tasks:
    - name: Show result
      debug:
        var: new_list_of_dicts

Which give as well:

PLAY [manipulate dict] *****************************************************************************************************************************************************************************************************************

TASK [show result] *********************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "new_list_of_dicts": [
        {
            "f1": "a",
            "f2": "b",
            "id": "id1"
        },
        {
            "f1": "c",
            "f2": "d",
            "id": "id1"
        },
        {
            "f1": "e",
            "f2": "f",
            "id": "id2"
        },
        {
            "f1": "g",
            "f2": "h",
            "id": "id2"
        }
    ]
}

PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Upvotes: 2

Vladimir Botka
Vladimir Botka

Reputation: 68034

For example

    - set_fact:
        new_list_of_dicts: "{{ new_list_of_dicts|d([]) + [item|combine] }}"
      with_subelements:
        - "{{ test_list_of_dicts }}"
        - inner_dict

gives

  new_list_of_dicts:
    [
        {
            "f1": "a",
            "f2": "b",
            "id": "id1"
        },
        {
            "f1": "c",
            "f2": "d",
            "id": "id1"
        },
        {
            "f1": "e",
            "f2": "f",
            "id": "id2"
        },
        {
            "f1": "g",
            "f2": "h",
            "id": "id2"
        }
    ]

See with_subelements and subelements.

Upvotes: 2

Related Questions