我如何使用 ansible 访问多级 json 文件中以随机密钥开头的某些值

How with ansible can i access to some value preceded by a random key in a multi level json file

我是运行一个ansible playbook,输出JSON中的信息。这个数组有一个循环,它有多个不相同的键,如“/VRF1/192.168.10.10”,具有不同的值。请看下面,我对 "address"、"name"、"monitor_status"、"availability_status"、"status_description" 和 "session_status" 感兴趣:

ok: [1.1.1.1] => {
    "BigIP_facts": {
        "ansible_facts": {
            "node": {
                "/VRF1/10.10.10.10": {
                            "address": "10.10.10.10",
                            "connection_limit": 0,
                            "description": "",
                            "dynamic_ratio": 1,
                            "monitor_rule": {
                                "monitor_templates": [
                                    "/VRF1/icmp-60s"
                                ],
                                "quorum": 0,
                                "type": "MONITOR_RULE_TYPE_SINGLE"
                            },
                            "monitor_status": "MONITOR_STATUS_DOWN",
                            "name": "10.10.10.10",
                            "object_status": {
                                "availability_status": "AVAILABILITY_STATUS_RED",
                                "enabled_status": "ENABLED_STATUS_ENABLED",
                                "status_description": "/VRF1/icmp-60s: No successful responses received before deadline. @2019/09/20 13:04:38. "
                            },
                            "rate_limit": 0,
                            "ratio": 1,
                            "session_status": "SESSION_STATUS_ENABLED"
                },
                "/VRF1/20.20.20.20": {
                            "address": "20.20.20.20",
                            "connection_limit": 0,
                            "description": "",
                            "dynamic_ratio": 1,
                            "monitor_rule": {
                                "monitor_templates": [
                                    "/Common/gateway_icmp"
                                ],
                                "quorum": 0,
                                "type": "MONITOR_RULE_TYPE_SINGLE"
                            },
                            "monitor_status": "MONITOR_STATUS_DOWN",
                            "name": "20.20.20.20",
                            "object_status": {
                                "availability_status": "AVAILABILITY_STATUS_RED",
                                "enabled_status": "ENABLED_STATUS_ENABLED",
                                "status_description": "/Common/gateway_icmp: No successful responses received before deadline. @2019/09/20 13:04:58. "
                            },
                            "rate_limit": 0,
                            "ratio": 1,
                            "session_status": "SESSION_STATUS_ENABLED"
                }

          }
       }
    }
}

dict2items 过滤器是解决此问题的方法:

- name: show the monitor statuses
  debug:
    msg: >-
     {{ BigIP_facts.ansible_facts.node
     | dict2items
     | map(attribute="value.monitor_status")
     | list }}

在其中替换您自己的 map(),或者 {% for kv in (BigIP_facts.ansible_facts.node | dict2items) %} 如果使用命令式代码更容易