Home > Mobile >  Ansible - Combine two dictionaries, but with a loop over the one
Ansible - Combine two dictionaries, but with a loop over the one

Time:09-16

I have a static dictionary which I need to combine (to add it) to dynamic dictionary.

   "static": [
        {
            "item1": "abc",
            "item2": "def",
            "item3": "dkslks",
            "item4": "kkk"
        }
    ]


    "dynamic": [
        {
            "item5": "aa",
            "item6": "aaa",
            "item7: "aaaa"
        },
        {
            "item5": "bb",
            "item6": "bbb",
            "item7: "bbbb"
        },
        {
            "item5": "cc",
            "item6": "ccc",
            "item7: "cccc"
        }
    ]

I need to get the following result:

    "combined": [
        {
            "item1": "abc",
            "item2": "def",
            "item3": "dkslks",
            "item4": "kkk"
            "item5": "aa",
            "item6": "aaa",
            "item7: "aaaa"
        },
        {
            "item1": "abc",
            "item2": "def",
            "item3": "dkslks",
            "item4": "kkk"
            "item5": "bb",
            "item6": "bbb",
            "item7: "bbbb"
        },
        {
            "item1": "abc",
            "item2": "def",
            "item3": "dkslks",
            "item4": "kkk"
            "item5": "cc",
            "item6": "ccc",
            "item7: "cccc"
        }
    ]

I managed to combine it but just for the first part of this dict, not for all the members. Result was like this:

  - set_fact:
      combined: "{{ static|combine(dynamic) }}"


    "combined": [
        {
            "item1": "abc",
            "item2": "def",
            "item3": "dkslks",
            "item4": "kkk"
            "item5": "aa",
            "item6": "aaa",
            "item7: "aaaa"
        }
    ]

So I need to put it in a loop, but cannot find the right way. Here is some things I tried:

 - set_fact:
         combined: "{{ static | combine(json_query('[*]') | dynamic) }}"

or similar variations like this one

 - set_fact:
     combined: "{{ static | combine(dynamic) | json_query('[*]') }}"

or

  - set_fact:
      combined: "{{ dynamic | combine(static) }}"
    loop: "{{dynamic | json_query('[*]')}}"

Would someone be kind to help?

Thanks!

CodePudding user response:

A problem with several of your attempts is that both dynamic and static are lists, so they can't be used with the combine filter.

This seems to work with your sample data:

- hosts: localhost
  gather_facts: false
  vars_files:
    - data.yaml
  tasks:
    - set_fact:
        combined: "{{ combined   [item|combine(static[0])] }}"
      vars:
        combined: []
      loop: "{{ dynamic }}"

    - debug:
        var: combined

This outputs:

PLAY [localhost] ***************************************************************

TASK [set_fact] ****************************************************************
ok: [localhost] => (item={'item5': 'aa', 'item6': 'aaa', 'item7': 'aaaa'})
ok: [localhost] => (item={'item5': 'bb', 'item6': 'bbb', 'item7': 'bbbb'})
ok: [localhost] => (item={'item5': 'cc', 'item6': 'ccc', 'item7': 'cccc'})

TASK [debug] *******************************************************************
ok: [localhost] => {
    "combined": [
        {
            "item1": "abc",
            "item2": "def",
            "item3": "dkslks",
            "item4": "kkk",
            "item5": "aa",
            "item6": "aaa",
            "item7": "aaaa"
        },
        {
            "item1": "abc",
            "item2": "def",
            "item3": "dkslks",
            "item4": "kkk",
            "item5": "bb",
            "item6": "bbb",
            "item7": "bbbb"
        },
        {
            "item1": "abc",
            "item2": "def",
            "item3": "dkslks",
            "item4": "kkk",
            "item5": "cc",
            "item6": "ccc",
            "item7": "cccc"
        }
    ]
}

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

  • Related