ansible json_query 多个值

ansible json_query multiple values

从先前的 GET api 调用 ansible 我得到了 json 结果。

api 调用看起来像这样:

- name: Refresh Datadog monitors list
  uri:
    url: http://my.api.com
    return_content: yes
    method: GET
    status_code: 200
    headers:
      Content-Type: "application/json"
  register: results

结果看起来像这样(虚拟数据):

[{
    "name": "bob",
    "id": 13590804,
    "colour": "blue",
    "created": "2019-11-21T07:41:33.148976+00:00",
    "modified": "2019-11-21T07:41:33.148976+00:00",
    "overall_state_modified": "2019-11-25T06:45:08+00:00",
    "overall_state": "OK"
},
{
    "name": "john",
    "id": 123124515,
    "colour": "green",
    "created": "2019-11-21T07:41:33.148976+00:00",
    "modified": "2019-11-21T07:41:33.148976+00:00",
    "overall_state_modified": "2019-11-25T06:45:08+00:00",
    "overall_state": "OK"
},
{
    "name": "carl",
    "id": 3252532,
    "colour": "orange",
    "created": "2019-11-21T07:41:33.148976+00:00",
    "modified": "2019-11-21T07:41:33.148976+00:00",
    "overall_state_modified": "2019-11-25T06:45:08+00:00",
    "overall_state": "OK"
},
{
    "name": "louis",
    "id": 5675467,
    "colour": "purple",
    "created": "2019-11-21T07:41:33.148976+00:00",
    "modified": "2019-11-21T07:41:33.148976+00:00",
    "overall_state_modified": "2019-11-25T06:45:08+00:00",
    "overall_state": "OK"
}]

在后续步骤中,我希望从 json 中仅提取两个值,使其看起来像:

[{
        "name": "bob",
        "id": 13590804
    },
    {
        "name": "john",
        "id": 123124515
    },
    {
        "name": "carl",
        "id": 3252532
    },
    {
        "name": "louis",
        "id": 5675467
    }
]

我尝试了来自不同来源的多次迭代来尝试得到这个结果,例如:

- name: "Display all id's with names"
  debug: msg = "{{ results | json_query '[id, names]')}}"

- name: "Display all id's with names"
  debug: msg = "{{ results | json_query '[*][id, names]')}}"

我能理解我对 jmespath 的了解存在差距,但文档有点多,我无法找到确切的解决方案。

您正在查找 JMESPath 文档中引用为 filters and multiselect hashes 的部分。

所以你的任务应该是:

- name: "Display all id's with names"
  debug: 
    msg: "{{ results | json_query('[*].{id: id, name: name}') }}"

鉴于剧本:

- hosts: all
  gather_facts: no  
        
  tasks:
    - debug:
        msg: "{{ results | json_query('[*].{id: id, name: name}') }}"
      vars:
        results: 
          - name: bob
            id: 13590804
            colour: blue
            created: 2019-11-21T07:41:33.148976+00:00
            modified: 2019-11-21T07:41:33.148976+00:00
            overall_state_modified: 2019-11-25T06:45:08+00:00
            overall_state: OK
          - name: john
            id: 123124515
            colour: green
            created: 2019-11-21T07:41:33.148976+00:00
            modified: 2019-11-21T07:41:33.148976+00:00
            overall_state_modified: 2019-11-25T06:45:08+00:00
            overall_state: OK
          - name: carl
            id: 3252532
            colour: orange
            created: 2019-11-21T07:41:33.148976+00:00
            modified: 2019-11-21T07:41:33.148976+00:00
            overall_state_modified: 2019-11-25T06:45:08+00:00
            overall_state: OK
          - name: louis
            id: 5675467
            colour: purple
            created: 2019-11-21T07:41:33.148976+00:00
            modified: 2019-11-21T07:41:33.148976+00:00
            overall_state_modified: 2019-11-25T06:45:08+00:00
            overall_state: OK

这个输出:

PLAY [all] ********************************************************************************************************

TASK [debug] ******************************************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "id": 13590804,
            "name": "bob"
        },
        {
            "id": 123124515,
            "name": "john"
        },
        {
            "id": 3252532,
            "name": "carl"
        },
        {
            "id": 5675467,
            "name": "louis"
        }
    ]
}

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