JSON Ansible Playbook 中的查询未能select 所需数据

JSON query in Ansible Playbook failing to select desired data

我是编写 Ansible 剧本的新手,遇到了障碍。我正在尝试使用 Site24x7 API 来安排维护,并且需要从监视器组列表中获取特定 ID。我创建了以下内容:

- name: Download Monitor Groups from Site24x7
  uri:
    url: https://www.site24x7.com/api/monitor_groups
    method: GET
    return_content: yes
    headers:
        Accept: "application/json; version=2.1"
        Authorization: "Zoho-oauthtoken {{authtoken.json.access_token}}"
    body: 
    return_content: yes
  register: monitor_groups

- name: Get Monitor Group ID
  set_fact:
    monitorGroupID_query: "[?display_name=='{{hostname.stdout}}'].group_id"

- name: Assign Monitor Group ID
  set_fact:
    monitorGroupID: "{{ monitor_groups.json | json_query(monitor_group_id_query) }}"

- debug:
    var: monitorGroupID

我的 API 调用 returns 数据如下所示

"monitor_groups.json": {
    "code": 0, 
    "data": [
        {
            "description": "System Generated", 
            "display_name": "server1", 
            "group_id": "319283000000505864", 
            "group_type": 1, 
            "health_threshold_count": 1, 
            "monitors": [
                "319283000000483017"
            ]
        }, 
        {
            "display_name": "server2", 
            "group_id": "319283000004701003", 
            "group_type": 3, 
            "health_threshold_count": 1, 
            "monitors": [
                "319283000003989345", 
                "319283000004061005"
            ]
        }
    ], 
    "message": "success"
}

我的查询不断returns一个空字符串。

任务 [分配监视器组 ID] *************************************** ****************************************************** * 好的:[server1.fdu.edu] => {"ansible_facts": {"monitorGroupID": ""}, "changed": false}

任务 [调试] ****************************************** *************************************************** 好的:[server1.fdu.edu] => { “监控组 ID”:“” }

提前感谢您的帮助

单引号在 JMESPath 中的工作方式与它在几乎所有其他“查询语言”中的工作方式不同——您需要 ` 个字符环绕 JSON 个文字值。此外,您的回复中的 data:[] 不是 隐含的 ,因此如果您打算使用它,则需要 .json.data | json_query 或更改您的 JMESPath 以添加data[?display...部分

  - name: Assign Monitor Group ID
    set_fact:
      monitorGroupID: "{{ monitor_groups.json | json_query( monitorGroupID_query ) }}"
    vars:
      monitorGroupID_query: 'data[?display_name==`"{{hostname.stdout}}"`].group_id'

给定 hostname.stdout=server2 收益率

ok: [localhost] => {"ansible_facts": {"monitorGroupID": ["319283000004701003"]}, "changed": false}

例如,给定以下数据

    monitor_groups:
      json:
        code: 0
        data:
          - description: System Generated
            display_name: server1
            group_id: '319283000000505864'
            group_type: 1
            health_threshold_count: 1
            monitors:
              - '319283000000483017'
          - display_name: server2
            group_id: '319283000004701003'
            group_type: 3
            health_threshold_count: 1
            monitors:
              - '319283000003989345'
              - '319283000004061005'
        message: success

创建主机名及其 group_id 的字典。用它来评估变量 monitorGroupID

    name_id: "{{ monitor_groups.json.data|
                 items2dict(key_name='display_name', value_name='group_id') }}"
    monitorGroupID: "{{ name_id[hostname.stdout] }}"

给予

  name_id:
    server1: '319283000000505864'
    server2: '319283000004701003'

那么,下面的任务

    - debug:
        var: monitorGroupID

给予

TASK [debug] **********************************************************
ok: [server1] => 
  monitorGroupID: '319283000000505864'
ok: [server2] => 
  monitorGroupID: '319283000004701003'