Search code examples
jsonansibleyamlf5big-ip

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


I am running an ansible playbook which outputs the informations in JSON. This array has a loop that has multiple not identical keys like "/VRF1/192.168.10.10" with different values. Please see below, I am interested in the "address", "name", "monitor_status", "availability_status", "status_description" and "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"
                }

          }
       }
    }
}

Solution

  • The dict2items filter is the way you work around that:

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

    Substituting your own map() in there, or {% for kv in (BigIP_facts.ansible_facts.node | dict2items) %} if it's easier to work with imperative code