我如何在字典列表中搜索以获得一些密钥?

How can I search in a list of dict to get some key?

我正在尝试使用“validar”上的“servers”键名进行搜索。如果它存在我想显示这样的东西: 名称:server7 proc_r:0.1 但我总是 0 如果它不存在,则搜索执行某些命令的过程。我做错了什么?

- hosts: localhost
  gather_facts: no
  vars:
    servers:
      - name: server5
        pri_sys: prod2
        proc: 0.7
        sec_sys: cont2
      - name: server7
        pri_sys: prod2
        proc: 0.1
        sec_sys: cont2
      - name: server2
        pri_sys: prod1
        proc: 0.35
        sec_sys: cont1
    validar:
      - sec_sys: cont2
        name:
          - server7
          - server9
          - server11
      - sec_sys: cont1
        name:
          - server4
          - server6

  tasks:
    - name: Create list
      set_fact:
        proc_reserved: >-
          {{ 
            proc_reserved | default([])
            + [{
              'proc_r': servers
                | selectattr('name', '==', item.name)
                | map(attribute="proc")
                |float,
              'name': item.name
            }]
          }}
      loop: "{{ validar }}"
      loop_control:
        label: "{{ item.name}}"

    - name: DISPLAY PROC_RESERVED
      debug:
        var: proc_reserved


    - name: Getting proc not listed on servers
      debug:
        msg: "Search proc of {{ item.name }}"
      with_items: "{{ servers }}"
      when: item.name not in server_ 
      vars:
        server_: "{{ validar | selectattr('name') }}" 

我相信以下示例符合您的期望,尽管重构所选服务器密钥对我来说似乎有点过分。

该解决方案使用 json_query 到 limit/change 初始字典列表中的选定键。请参阅 requirements 至 运行 过滤器。

play.yml

- hosts: localhost
  gather_facts: no
  vars:
    servers:
      - name: server5
        pri_sys: prod2
        proc: 0.7
        sec_sys: cont2
      - name: server7
        pri_sys: prod2
        proc: 0.1
        sec_sys: cont2
      - name: server2
        pri_sys: prod1
        proc: 0.35
        sec_sys: cont1
    validar:
      - sec_sys: cont2
        name:
          - server7
          - server9
          - server11
      - sec_sys: cont1
        name:
          - server4
          - server6
    validar_servers: "{{ validar | map(attribute='name') | flatten | unique | sort }}"
    reserved_servers: "{{ servers | selectattr('name', 'in', validar_servers) }}"

  tasks:
    - name: Display the raw calculated var (which you can use as is in loop for example)
      debug:
        var: reserved_servers

    - name: Refactor the var although this does not really seem necessary
      vars:
        query: >-
          [].{name: name, proc_r: proc}
      debug:
        msg: "{{ reserved_servers | json_query(query) }}"

给出:

$ ansible-playbook play.yml 

PLAY [localhost] ***********************************************************************************************************************************************

TASK [Display the raw calculated var (which you can use as is in loop for example)] ****************************************************************************
ok: [localhost] => {
    "reserved_servers": [
        {
            "name": "server7",
            "pri_sys": "prod2",
            "proc": 0.1,
            "sec_sys": "cont2"
        }
    ]
}

TASK [Refactor the var although this does not really seem necessary] *******************************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "name": "server7",
            "proc_r": 0.1
        }
    ]
}

PLAY RECAP *****************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0