Collecting key/value from json with ansible

60 views
Skip to first unread message

Rodrigues

unread,
Jun 21, 2021, 7:57:14 AM6/21/21
to Ansible Project
Hi all,

I have the below json data, however i'm having a hard time collecting the two keys i'm interested. which is parent_path and id

I can get to a single value using this, however i'd like to loop through all of items that start with "nsx-"

    - name: Show data2
debug:
  msg: "{{ tresult.results[4].json.results[0].id }} {{ tresult.results[4].json.results[0].parent_path }}"
delegate_to: localhost

{
    "msg": {
        "ansible_loop_var": "item",
        "cache_control": "no-cache, no-store, max-age=0, must-revalidate",
        "changed": false,
        "connection": "close",
        "content": "{\n  \"results\" : [ {\n    \"mac_discovery_profile_path\" : \"/infra/mac-discovery-profiles/Allow-MacDiscoveryProfile\",\n    \"ip_discovery_profile_path\" : \"/infra/ip-discovery-profiles/default-ip-discovery-profile\",\n    \"resource_type\" : \"SegmentDiscoveryProfileBindingMap\",\n    \"id\" : \"e361a6f8-88a7-47f0-be00-a7fb412e8f56\",\n    \"display_name\" : \"e361a6f8-88a7-47f0-be00-a7fb412e8f56\",\n    \"path\" : \"/infra/segments/nsx-Management---10.26.170.0_24/segment-discovery-profile-binding-maps/e361a6f8-88a7-47f0-be00-a7fb412e8f56\",\n    \"relative_path\" : \"e361a6f8-88a7-47f0-be00-a7fb412e8f56\",\n    \"parent_path\" : \"/infra/segments/nsx-Management---10.26.170.0_24\",\n    \"unique_id\" : \"ce6235a7-d960-4407-8d2c-cf110197ca4a\",\n    \"marked_for_delete\" : false,\n    \"overridden\" : false,\n    \"_create_user\" : \"admin\",\n    \"_create_time\" : 1623945358415,\n    \"_last_modified_user\" : \"admin\",\n    \"_last_modified_time\" : 1623947366813,\n    \"_system_owned\" : false,\n    \"_protection\" : \"NOT_PROTECTED\",\n    \"_revision\" : 1\n  } ],\n  \"result_count\" : 1,\n  \"sort_by\" : \"display_name\",\n  \"sort_ascending\" : true\n}",
        "content_type": "application/json",
        "cookies": {
            "JSESSIONID": "7D0E069A624336C3E6782E112A5A8B73"
        },
        "cookies_string": "JSESSIONID=7D0E069A624336C3E6782E112A5A8B73",
        "date": "Mon, 21 Jun 2021 11:28:20 GMT",
        "elapsed": 0,
        "expires": "0",
        "failed": false,
        "invocation": {
            "module_args": {
                "attributes": null,
                "body": null,
                "body_format": "json",
                "client_cert": null,
                "client_key": null,
                "creates": null,
                "dest": null,
                "follow_redirects": "none",
                "force": false,
                "force_basic_auth": true,
                "group": null,
                "headers": {
                    "Content-Type": "application/json"
                },
                "http_agent": "ansible-httpget",
                "method": "GET",
                "mode": null,
                "owner": null,
                "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                "remote_src": false,
                "removes": null,
                "return_content": true,
                "selevel": null,
                "serole": null,
                "setype": null,
                "seuser": null,
                "src": null,
                "status_code": [
                    "200"
                ],
                "timeout": 30,
                "unix_socket": null,
                "unsafe_writes": false,
                "url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                "url_username": "admin",
                "use_proxy": true,
                "user": "admin",
                "validate_certs": false
            }
        },
        "item": {
            "description": null,
            "dvswitch_name": "DVS-Dev-Ork2",
            "key": "dvportgroup-13",
            "network_policy": {
                "forged_transmits": false,
                "mac_changes": false,
                "promiscuous": false
            },
            "num_ports": 15,
            "port_policy": {
                "block_override": true,
                "ipfix_override": false,
                "live_port_move": false,
                "network_rp_override": false,
                "port_config_reset_at_disconnect": true,
                "security_override": false,
                "shaping_override": false,
                "traffic_filter_override": false,
                "uplink_teaming_override": false,
                "vendor_config_override": false,
                "vlan_override": false
            },
            "portgroup_name": "Management - 10.26.170.0_24",
            "teaming_policy": {
                "inbound_policy": true,
                "notify_switches": true,
                "policy": "loadbalance_ip",
                "rolling_order": false
            },
            "type": "earlyBinding",
            "vlan_info": {
                "pvlan": false,
                "trunk": false,
                "vlan_id": "0"
            }
        },
        "json": {
            "result_count": 1,
            "results": [
                {
                    "_create_time": 1623945358415,
                    "_create_user": "admin",
                    "_last_modified_time": 1623947366813,
                    "_last_modified_user": "admin",
                    "_protection": "NOT_PROTECTED",
                    "_revision": 1,
                    "_system_owned": false,
                    "display_name": "e361a6f8-88a7-47f0-be00-a7fb412e8f56",
                    "id": "e361a6f8-88a7-47f0-be00-a7fb412e8f56",
                    "ip_discovery_profile_path": "/infra/ip-discovery-profiles/default-ip-discovery-profile",
                    "mac_discovery_profile_path": "/infra/mac-discovery-profiles/Allow-MacDiscoveryProfile",
                    "marked_for_delete": false,
                    "overridden": false,
                    "parent_path": "/infra/segments/nsx-Management---10.26.170.0_24",
                    "path": "/infra/segments/nsx-Management---10.26.170.0_24/segment-discovery-profile-binding-maps/e361a6f8-88a7-47f0-be00-a7fb412e8f56",
                    "relative_path": "e361a6f8-88a7-47f0-be00-a7fb412e8f56",
                    "resource_type": "SegmentDiscoveryProfileBindingMap",
                    "unique_id": "ce6235a7-d960-4407-8d2c-cf110197ca4a"
                }
            ],
            "sort_ascending": true,
            "sort_by": "display_name"
        },
        "msg": "OK (unknown bytes)",
        "pragma": "no-cache",
        "redirected": false,
        "server": "NSX",
        "set_cookie": "JSESSIONID=7D0E069A624336C3E6782E112A5A8B73; Path=/; Secure; HttpOnly",
        "status": 200,
        "strict_transport_security": "max-age=31536000 ; includeSubDomains",
        "transfer_encoding": "chunked",
        "vary": "accept-encoding",
        "x_content_type_options": "nosniff",
        "x_frame_options": "SAMEORIGIN",
        "x_nsx_requestid": "db6a3c47-a75d-43f3-a8d0-e9b91acebc63",
        "x_xss_protection": "1; mode=block"
    }
}
Reply all
Reply to author
Forward
0 new messages