简体   繁体   English

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

[英]how to get json_query display property name in ansible playbook

I have this json block as an example:我以这个 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
}

I'm trying to figure out how I can get this output with property name and status to look like something like this.我试图弄清楚如何让这个 output 的属性名称和状态看起来像这样。

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

I can print everything except the IP part with this:我可以打印除 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}"

I wouldn't use JMESPath for this, the reason is, although it is pretty good at querying JSON, it is not really good at displaying JSON keys.我不会为此使用 JMESPath,原因是,虽然它非常擅长查询 JSON,但它并不擅长显示 JSON 键。

The keys() function is the closest you can find there, but it will yield you an array and since you cannot come back to a parent node you cannot do something like: keys() function 是您可以在那里找到的最接近的,但它会为您生成一个数组,并且由于您无法返回父节点,因此您不能执行以下操作:

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

Although this is a pretty frequently requested feature: https://github.com/jmespath/jmespath.js/issues/22虽然这是一个经常被请求的功能: https://github.com/jmespath/jmespath.js/issues/22


Now to solve your use case, you can use thekeys() function, but the one of Python.现在要解决您的用例,您可以使用keys() function,但 Python 之一。

You also have a issue in the fact that all your values of data.json are not dictionaries: in "capacity": 1080 , the value is a simple int .您还有一个问题,即data.json的所有值都不是字典:在"capacity": 1080中,值是一个简单的int

You can go work around this odd data structure using a when test and verify if you value is indeed a mapping (or in other words a dictionary).您可以 go 使用when测试和验证您的值是否确实是一个mapping (或者换句话说是一个字典)来解决这个奇怪的数据结构。

Given the playbook:鉴于剧本:

- 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

This yields the recap:这产生了回顾:

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   

This said, it is also a perfect use case for the dict2items filter:也就是说,它也是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

Would yield the same recap.将产生相同的回顾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM