如何在 ansible 剧本中获取 json_query 显示 属性 名称

how to get json_query display property name in ansible playbook

我以这个 json 块为例:

"msg": {
        "10.10.28.10": {
            "core": 23,
            "cpuCoreUsage": 0.0,
            "cputhreshold": 80,
            "status": "healthy",
            "status_code": 0,
            "status_reason": "Checks passed",
            "timestamp": 1614281443,
            "total": 0
        },
        "10.10.28.5": {
            "core": 18,
            "cpuCoreUsage": 2.0,
            "cputhreshold": 80,
            "status": "healthy",
            "status_code": 0,
            "status_reason": "Checks passed",
            "timestamp": 1614281443,
            "total": 0
        },
        "capacity": 1080
}

我正在尝试弄清楚如何使用 属性 名称和状态使输出看起来像这样。

DESIRED OUTPUT:
IP: 10.10.28.5, status: healthy, status_code: 0
IP: 10.10.28.10, status: healthy, status_code: 0

我可以打印除 IP 部分以外的所有内容:

  - name: STATUS QUERY
    debug:
      msg: "code: {{ item }}"
    loop: "{{ data.json | json_query(status_code_query) | list }}"
    vars:
            status_code_query: "*.{statuscode: status_code, status: status}"

我不会为此使用 JMESPath,原因是,尽管它非常擅长查询 JSON,但它并不擅长显示 JSON 键。

keys() 函数是您可以在那里找到的最接近的函数,但它会为您生成一个数组,并且由于您无法返回到父节点,因此您不能执行类似以下操作:

*.{IP: keys($)[0], statuscode: status_code, status: status}

虽然这是一个经常被请求的功能:https://github.com/jmespath/jmespath.js/issues/22


现在要解决您的用例,您可以使用 keys() 函数,但是 Python.

你也有一个问题,因为你所有的 data.json 值都不是字典:在 "capacity": 1080 中,值是一个简单的 int.

您可以使用 when 测试来解决这个奇怪的数据结构,并验证您的值是否确实是 mapping(或者换句话说字典)。

鉴于剧本:

- hosts: all
  gather_facts: yes

  tasks:
    - debug: 
        msg: >-
          IP: {{ item }}, 
          status: {{ data.json[item].status }}, 
          status_code: {{ data.json[item].status_code }}
      loop: "{{ data.json.keys() }}"
      when: data.json[item] is mapping
      vars:
        data:
          json:
            10.10.28.10:
              core: 23
              cpuCoreUsage: 0
              cputhreshold: 80
              status: healthy
              status_code: 0
              status_reason: Checks passed
              timestamp: 1614281443
              total: 0
            10.10.28.5:
              core: 18
              cpuCoreUsage: 2
              cputhreshold: 80
              status: healthy-
              status_code: 0-
              status_reason: Checks passed
              timestamp: 1614281443
              total: 0
            capacity: 1080

这产生了回顾:

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

TASK [Gathering Facts] **********************************************************************************************
ok: [localhost]

TASK [debug] ********************************************************************************************************
ok: [localhost] => (item=10.10.28.10) => {
    "msg": "IP: 10.10.28.10,  status: healthy,  status_code: 0"
}
ok: [localhost] => (item=10.10.28.5) => {
    "msg": "IP: 10.10.28.5,  status: healthy-,  status_code: 0-"
}
skipping: [localhost] => (item=capacity) 

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

也就是说,它也是 dict2items 过滤器的完美用例:

- hosts: all
  gather_facts: yes

  tasks:
    - debug: 
        msg: >-
          IP: {{ item.key }}, 
          status: {{ item.value.status }}, 
          status_code: {{ item.value.status_code }}
      loop: "{{ data.json | dict2items }}"
      when: item.value is mapping
      vars:
        data:
          json:
            10.10.28.10:
              core: 23
              cpuCoreUsage: 0
              cputhreshold: 80
              status: healthy
              status_code: 0
              status_reason: Checks passed
              timestamp: 1614281443
              total: 0
            10.10.28.5:
              core: 18
              cpuCoreUsage: 2
              cputhreshold: 80
              status: healthy-
              status_code: 0-
              status_reason: Checks passed
              timestamp: 1614281443
              total: 0
            capacity: 1080

会产生相同的重述。