title: Merge lists in Ansible when a key/value pair from each matches
body: <p>I have two lists of dictionaries (port_info and int_trunk) that I am trying to merge when the value of the 'port' key matches however not every port will be represented in 'int_trunk'.  In this instance, just leave the 'trunked_vlans' value blank.  I am using 'with_nested' to iterate over the lists.  I can't seem to get the output to do what I want.  I have tried the combine() filter along with manually trying to create vars but can't quite get it.  How do I get these two lists to combine?</p>
  
  <pre><code>---
  - hosts: localhost
    connection: local
    gather_facts: no
  
    vars:
      trunk_ports: []
      non_trunk_ports: []
      new_port_info: []
      port_info: 
        - desc: "*** Voice Server Port ***"
          duplex: "a-full"
          port: "Gi1/0/1"
          speed: "a-1000"
          status: connected
          trunk_vlans: ""
          type: "10/100/1000BaseTX"
          vlan: 3
        - desc: "*** Voice Server Port ***"
          duplex: "a-full"
          port: "Gi1/0/2"
          speed: "a-1000"
          status: connected
          trunk_vlans: ""
          type: "10/100/1000BaseTX"
          vlan: 3
        - desc: "Some Port"
          duplex: auto
          port: "Gi1/0/3"
          speed: auto
          status: notconnect
          trunk_vlans: ""
          type: "10/100/1000BaseTX"
          vlan: 23
      int_trunk:
        - port: "Gi1/0/1"
          vlans: "1-50"
        - port: "Gi1/0/2"
          vlans: "50-60"
  </code></pre>
  
  <p>The output of this should be:</p>
  
  <pre><code>port_info: 
    - desc: "*** Voice Server Port ***"
      duplex: "a-full"
      port: "Gi1/0/1"
      speed: "a-1000"
      status: connected
      trunk_vlans: "1-50"
      type: "10/100/1000BaseTX"
      vlan: 3
    - desc: "*** Voice Server Port ***"
      duplex: "a-full"
      port: "Gi1/0/2"
      speed: "a-1000"
      status: connected
      trunk_vlans: "50-60"
      type: "10/100/1000BaseTX"
      vlan: 3
    - desc: "Some Port"
      duplex: auto
      port: "Gi1/0/3"
      speed: auto
      status: notconnect
      trunk_vlans: ""
      type: "10/100/1000BaseTX"
      vlan: 23
  </code></pre>
  
  <p>This is one of the things I have tried:</p>
  
  <pre><code>---
  - hosts: localhost
    connection: local
    gather_facts: no
  
    vars:
      trunk_ports: []
      non_trunk_ports: []
      new_port_info: []
      port_info: 
        - desc: "*** Voice Server Port ***"
          duplex: "a-full"
          port: "Gi1/0/1"
          speed: "a-1000"
          status: connected
          trunk_vlans: ""
          type: "10/100/1000BaseTX"
          vlan: 3
        - desc: "*** Voice Server Port ***"
          duplex: "a-full"
          port: "Gi1/0/2"
          speed: "a-1000"
          status: connected
          trunk_vlans: ""
          type: "10/100/1000BaseTX"
          vlan: 3
        - desc: " "
          duplex: auto
          port: "Gi1/0/3"
          speed: auto
          status: notconnect
          trunk_vlans: ""
          type: "10/100/1000BaseTX"
          vlan: 23
      int_trunk:
        - port: "Gi1/0/1"
          vlans: "1-50"
        - port: "Gi1/0/2"
          vlans: "50-60"
  
    tasks:
      - name: Merge trunk ports
        set_fact:
          trunk_ports: "{{ trunk_ports + [ { 'desc': item.0.desc, 'duplex': item.0.duplex, 'port': item.0.port, 'speed': item.0.speed, 'status
  ': item.0.status, 'trunk_vlans': item.1.vlans, 'type': item.0.type, 'vlan': item.0.vlan } ] }}"
        with_nested:
          - "{{ port_info }}"
          - "{{ int_trunk }}"
        when: item.0.port == item.1.port 
  
      - name: Merge non-trunk ports
        set_fact:
          non_trunk_ports: "{{ non_trunk_ports + [ { 'desc': item.0.desc, 'duplex': item.0.duplex, 'port': item.0.port, 'speed': item.0.speed,
   'status': item.0.status, 'trunk_vlans': item.0.trunk_vlans, 'type': item.0.type, 'vlan': item.0.vlan } ] }}"
        with_nested:
          - "{{ port_info }}"
          - "{{ int_trunk }}"
        when: item.0.port != item.1.port
  
      - name: Merge all ports
        set_fact:
          new_port_info: "{{ new_port_info + [item.0|combine(item.1)] }}"
        with_nested:
          - "{{ port_info }}"
          - "{{ trunk_ports }}"
        when: item.0.port == item.1.port
  
      - name: Echo
        debug: var=trunk_ports
  
  
      - name: Echo
        debug: var=non_trunk_ports
  
      - name: Echo
        debug: var=new_port_info
  </code></pre>
  
  <p>I end up with ports Gi1/0/1 and Gi1/0/2 but not Gi1/0/3.</p>