rado
rado

Reputation: 1

Two dictionaries - how to merge?

I am trying to to put some calculated network details in one dictionary and I have these two dictionaries:

    "netmk": {
        "host_A": {
            "netmask": "255.255.255.240"
        },
        "host_B": {
            "netmask": "255.255.255.128"
        }
    }


    "netip": {
        "host_A": {
            "ip": "192.168.10.1"
        },
        "host_B": {
            "ip": "192.168.20.1"
        }
    }

   

I want to make such structure:

     "net": {
        "host_A": {
            "ip": 192.168.10.1
            "netmask": "255.255.255.240"                  
        },
        "host_B": {
            "ip": 192.168.20.1
            "netmask": "255.255.255.128"
        }
    }

How to achieve something like this?

Upvotes: 0

Views: 39

Answers (1)

Zeitounator
Zeitounator

Reputation: 44615

Merging dictionaries in ansible/jinja2 is made with the combine filter. For the below oneliner example, I added your sample data to the file so_data.yml

$ ansible localhost -m debug -a msg="{{ netmk | combine(netip, recursive=true) }}" -e @so_data.yml
localhost | SUCCESS => {
    "msg": {
        "host_A": {
            "ip": "192.168.10.1",
            "netmask": "255.255.255.240"
        },
        "host_B": {
            "ip": "192.168.20.1",
            "netmask": "255.255.255.128"
        }
    }
}

Upvotes: 1

Related Questions