两个字典之间的匹配

Match between two dict

这是上一个问题的延续。我只想显示来自 lpar_proc 的与 validar_:

的 LPARNAME 匹配的字典
   "validar_": [
        {
            "LPARNAME": [
                "server1",
                "server2"
            ]
        },
        {
            "LPARNAME": [
                "server3"
            ]
        }
    ]
}
    "lpar_proc": [
        {
            "LPARNAME": "server1",
            "Name": "CONT1",
            "proc": "1.05"
        },
        {
            "LPARNAME": "server2",
            "Name": "CONT1",
            "proc": "0.25"
        },
        {
            "LPARNAME": "server3",
            "Name": "CONT2",
            "proc": "0.05"
        },
        {
            "LPARNAME": "server4",
            "Name": "CONT2",
            "proc": "0.35"
        },
        {
            "LPARNAME": "server5",
            "Name": "PRD1",
            "proc": "1.15"
        }
    ]
}

预期输出:

    "lpar_proc": [
        {
            "LPARNAME": "server1",
            "Name": "CONT1",
            "proc": "1.05"
        },
        {
            "LPARNAME": "server2",
            "Name": "CONT1",
            "proc": "0.25"
        },
        {
            "LPARNAME": "server3",
            "Name": "CONT2",
            "proc": "0.05"
        }

我正在尝试使用 selectattr 在 validar_ 中进行搜索,但它没有显示任何内容。为什么不起作用?

    - name: final
      debug:
        var: "{{ reserved }}"
      vars:
        reserved: "{{ lpar_proc | selectattr('LPARNAME', 'in', validar_) }}"   

你有一个列表列表,你必须展平所有列表, 也许有一个只有一项任务的解决方案,但这个剧本可以完成这项工作:

  vars:
    validar:
      - LPARNAME:
        - server1
        - server2
      - LPARNAME:
        - server3
    lpar_proc:
      - LPARNAME: server1
        Name: CONT1
        proc: '1.05'
      - LPARNAME: server2
        Name: CONT1
        proc: '0.25'
      - LPARNAME: server3
        Name: CONT2
        proc: '0.05'
      - LPARNAME: server4
        Name: CONT2
        proc: '0.35'
      - LPARNAME: server5
        Name: PRD1
        proc: '1.15'
  tasks:
    - set_fact:
        clist: "{{ clist | d([]) + item.LPARNAME }}"
      loop: "{{ validar }}"

    - debug:
        msg: "{{ reserved }}" 
      vars:
        reserved: "{{ lpar_proc | selectattr('LPARNAME', 'in', clist) }}"

结果:

ok: [localhost] => {
    "msg": [
        {
            "LPARNAME": "server1",
            "Name": "CONT1",
            "proc": "1.05"
        },
        {
            "LPARNAME": "server2",
            "Name": "CONT1",
            "proc": "0.25"
        },
        {
            "LPARNAME": "server3",
            "Name": "CONT2",
            "proc": "0.05"
        }
    ]
}

你只能使用一个任务,我正在使用 jinja2:

  tasks:
    - debug:
        msg: "{{ reserved }}"
      vars:
        clist: >-
            {% set ns = namespace() %}
            {% set ns.clist = [] %}
            {% for lpar in validar %}
            {% set ns.clist =  ns.clist + lpar.LPARNAME %}
            {% endfor %}
            {{ ns.clist }}
        reserved: "{{ lpar_proc | selectattr('LPARNAME', 'in', clist) }}"