0

我正在运行一个以 JSON 格式输出信息的 ansible 剧本。该数组有一个循环,该循环具有多个不同的键,例如“ /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"
                }

          }
       }
    }
}
4

1 回答 1

0

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) %}如果使用命令式代码更容易

于 2019-10-04T03:26:48.703 回答