Combine dictionaries with condition and different structure

Viewed 36

I'm looking to combine the two dictionaries by adding the ike_version to dic1 when the vpn_peer_ip match on both dic1 and dic2.

Any help is very appreciated.

      dic1:
        2aaac0695040: 
            vpn_encryption: "ESP Tunnel PFS AES-256 SHA256.."
            vpn_local_hosts: "10.1.1.1"
            vpn_name: "Vpn1"
            vpn_peer_ip: "12.2.2.1"
            vpn_remote_hosts: "192.168.10.1"
        2aaac0695708: 
            vpn_encryption: "ESP Tunnel PFS AES-256 SHA256.."
            vpn_local_hosts: "10.1.1.5"
            vpn_name: "Vpn1"
            vpn_peer_ip: "12.2.2.1"
            vpn_remote_hosts: "192.168.10.10-192.168.10.20"
        2aaac06958f8: 
            vpn_encryption: "ESP Tunnel PFS AES-256 SHA256.."
            vpn_local_hosts: "10.1.1.10-10.1.1.20"
            vpn_name: "Vpn1"
            vpn_peer_ip: "12.2.2.1"
            vpn_remote_hosts: "192.168.30.10-192.168.10.40"
        2aaac0d1f040: 
            vpn_encryption: "ESP Tunnel AES-256 SHA256      "
            vpn_local_hosts: "10.1.1.10-10.1.1.20"
            vpn_name: "Vpn2"
            vpn_peer_ip: "170.54.6.3"
            vpn_remote_hosts: "180.40.20.2"
        2aaac0d1f138: 
            vpn_encryption: "ESP Tunnel AES-256 SHA256      "
            vpn_local_hosts: "10.1.1.50"
            vpn_name: "Vpn2"
            vpn_peer_ip: "170.54.6.3"
            vpn_remote_hosts: "180.40.20.2"

      dic2:    
        fw_fico_chicago: 
            ike_version: "IKEv1"
            vpn_peer_ip: "12.2.2.1"
        fw_iic: 
            ike_version: "IKEv2"
            vpn_peer_ip: "170.54.6.3"

I'm looking to combine the two dictionaries by adding the ike_version to dic1 when the vpn_peer_ip match on both dic1 and dic2.

Any help is very appreciated.

1 Answers

For example, create a dictionary first

ike_version: "{{ dic2.values()|list|items2dict(key_name='vpn_peer_ip',
                                               value_name='ike_version') }}"

gives

  ike_version:
    12.2.2.1: IKEv1
    170.54.6.3: IKEv2

Then, in the loop, update the dictionary

    - set_fact:
        dic1: "{{ dic1|combine(_update) }}"
      loop: "{{ dic1|dict2items }}"
      when: item.value.vpn_peer_ip in ike_version.keys()|list
      vars:
        _update: "{{ {item.key: item.value|
                                combine({'ike_version':
                                          ike_version[item.value.vpn_peer_ip]})} }}"

gives

  dic1:
    2aaac0695040:
      ike_version: IKEv1
      vpn_encryption: ESP Tunnel PFS AES-256 SHA256..
      vpn_local_hosts: 10.1.1.1
      vpn_name: Vpn1
      vpn_peer_ip: 12.2.2.1
      vpn_remote_hosts: 192.168.10.1
    2aaac0695708:
      ike_version: IKEv1
      vpn_encryption: ESP Tunnel PFS AES-256 SHA256..
      vpn_local_hosts: 10.1.1.5
      vpn_name: Vpn1
      vpn_peer_ip: 12.2.2.1
      vpn_remote_hosts: 192.168.10.10-192.168.10.20
    2aaac06958f8:
      ike_version: IKEv1
      vpn_encryption: ESP Tunnel PFS AES-256 SHA256..
      vpn_local_hosts: 10.1.1.10-10.1.1.20
      vpn_name: Vpn1
      vpn_peer_ip: 12.2.2.1
      vpn_remote_hosts: 192.168.30.10-192.168.10.40
    2aaac0d1f040:
      ike_version: IKEv2
      vpn_encryption: 'ESP Tunnel AES-256 SHA256      '
      vpn_local_hosts: 10.1.1.10-10.1.1.20
      vpn_name: Vpn2
      vpn_peer_ip: 170.54.6.3
      vpn_remote_hosts: 180.40.20.2
    2aaac0d1f138:
      ike_version: IKEv2
      vpn_encryption: 'ESP Tunnel AES-256 SHA256      '
      vpn_local_hosts: 10.1.1.50
      vpn_name: Vpn2
      vpn_peer_ip: 170.54.6.3
      vpn_remote_hosts: 180.40.20.2
Related