结合条件和不同结构的字典

Combine dictionaries with condition and different structure

当 vpn_peer_ip 在 dic1 和 dic2 上匹配时,我希望通过将 ike_version 添加到 dic1 来合并这两个词典。

非常感谢任何帮助。

      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"

当 vpn_peer_ip 在 dic1 和 dic2 上匹配时,我希望通过将 ike_version 添加到 dic1 来合并这两个词典。

非常感谢任何帮助。

比如先创建字典

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

给予

  ike_version:
    12.2.2.1: IKEv1
    170.54.6.3: IKEv2

然后,在循环中,更新字典

    - 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]})} }}"

给予

  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