Search code examples
jsondictionaryansibleansible-2.xansible-facts

How to fetch multiple values from a nested JSON like output


I am looking forward a way to list the value from a nested variable (which has dict and list values) ie register which is response_find and I have that in mostly json format and most of the values inside the json are nested within [] list like construct.

It works somehow if I get individual values from it like below:

var=response_find['json']['results'][0]['content_facet_attributes']

or

var=response_find['json']['results'][0]['certname']

But did not work when I did below:

var=response_find['json']['results'][0]['content_view_id']

Below are few values which I'm looking forward to fetch from this nested output:

"architecture_name": "x86_64",
"name": "satcap.rest.example.com",
"url": "https://satcap.rest.example.com:9090"
"content_source_name": "satcap.rest.example.com",
"name": "ccv-azure-infra-rhel7"
"name": "Prod"
"id": 33485,
"ip": "192.168.88.88",
"name": "invwharn108.test.exampl.com",
"registered_through": "satcap.rest.example.com"

Nested Json output:

{
    "response_find": {
        "apipie_checksum": "7533ab625c45a3819647f4fb4c9394c2832c0180",
        "cookies": {
            "_session_id": "25872833d9e2723073797fbd8cfa2d63"
        },
        "cookies_string": "_session_id=25872833d9e2723073797fbd8cfa2d63",
        "foreman_version": "1.24.1.21",
        "json": {
            "page": 1,
            "per_page": 20,
            "results": [
                {
                    "architecture_id": 1,
                    "architecture_name": "x86_64",
                    "build": false,
                    "capabilities": [
                        "build"
                    ],
                    "certname": "invwharn108.test.exampl.com",
                    "comment": null,
                    "content_facet_attributes": {
                        "applicable_module_stream_count": 0,
                        "applicable_package_count": 0,
                        "content_source": {
                            "id": 17,
                            "name": "satcap.rest.example.com",
                            "url": "https://satcap.rest.example.com:9090"
                        },
                        "content_source_id": 17,
                        "content_source_name": "satcap.rest.example.com",
                        "content_view": {
                            "id": 67,
                            "name": "ccv-azure-infra-rhel7"
                        },
                        "content_view_id": 67,
                        "content_view_name": "ccv-azure-infra-rhel7",
                        "errata_counts": {
                            "bugfix": 0,
                            "enhancement": 0,
                            "security": 0,
                            "total": 0
                        },
                        "id": 32255,
                        "kickstart_repository": {
                            "id": 5772,
                            "name": "Red Hat Enterprise Linux 7 Server Kickstart x86_64 7.8"
                        },
                        "kickstart_repository_id": 5772,
                        "kickstart_repository_name": "5772",
                        "lifecycle_environment": {
                            "id": 5,
                            "name": "Prod"
                        },
                        "lifecycle_environment_id": 5,
                        "lifecycle_environment_name": "Prod",
                        "upgradable_module_stream_count": 0,
                        "upgradable_package_count": 0,
                        "uuid": "dab2d66b-1a73-490a-aa9d-3f036658980a"
                    },
                    "created_at": "2020-12-23 13:19:35 UTC",
                    "disk": null,
                    "domain_id": 25,
                    "domain_name": "test.example-aws.example.com",
                    "enabled": true,
                    "environment_id": null,
                    "environment_name": null,
                    "errata_status": 1,
                    "errata_status_label": "Could not calculate errata status, ensure host is registered and the katello-host-tools package is installed",
                    "global_status": 1,
                    "global_status_label": "Warning",
                    "hostgroup_id": 982,
                    "hostgroup_name": "infra",
                    "hostgroup_title": "NXDI-hg-aws/west-europe/infra",
                    "id": 33485,
                    "image_file": "",
                    "image_id": null,
                    "image_name": null,
                    "installed_at": null,
                    "ip": "192.168.88.88",
                    "ip6": null,
                    "last_compile": "2020-12-23 13:19:38 UTC",
                    "last_report": null,
                    "location_id": 37,
                    "location_name": "west-europe",
                    "mac": "02:78:43:60:ee:fb",
                    "managed": false,
                    "medium_id": null,
                    "medium_name": null,
                    "model_id": 25,
                    "model_name": "HVM domU",
                    "name": "invwharn108.test.exampl.com",
                    "openscap_proxy": null,
                    "openscap_proxy_id": null,
                    "openscap_proxy_name": null,
                    "operatingsystem_id": 21,
                    "operatingsystem_name": "RedHat 7.8",
                    "organization_id": 3,
                    "pxe_loader": "PXELinux BIOS",
                    "subnet_id": 65,
                    "subnet_name": "aws-west-europe-infra",
                    "subscription_facet_attributes": {
                        "autoheal": true,
                        "hypervisor": false,
                        "id": 33593,
                        "last_checkin": "2020-12-23 13:19:42 UTC",
                        "purpose_addons": [],
                        "purpose_role": "",
                        "purpose_usage": "",
                        "registered_at": "2020-12-23 13:19:35 UTC",
                        "registered_through": "satcap.rest.example.com",
                    },
                    "subscription_global_status": 1,
                    "subscription_status": 1,
                    "subscription_status_label": "Partially entitled",
                }
            ],
            "search": "name=invwharn108.test.exampl.com",
            "sort": {
                "by": null,
                "order": null
            },
            "subtotal": 1,
            "total": 13121
        },
        "msg": "OK (unknown bytes)",
        "x_request_id": "e02dd9b3-b2f8-4c39-8bf5-1ce1897d9548",
    }
}

My Play:

---
- hosts: localhost
  tasks:
    - include_vars: vaults/aws_secrets.yml
      no_log: true
    - include_vars: requirements.yml
      no_log: true

    - name: Find the ID of Cloud VM in Satellite
      uri:
        url: "https://{{ satserver }}/api/v2/hosts?search=name={{ aws_instance_name }}"
        method: GET
        user: "{{ aws_satuser }}"
        password: "{{ aws_satpw }}"
        force_basic_auth: yes
        validate_certs: no
      register: response_find
      delegate_to: localhost
      changed_when: false
    - debug:
        var=response_find['json']['results'][0]['content_facet_attributes']
        #var=response_find.json.results.0.organization_name
        #var=response_find['json']['results'][0]['certname']

Result:

"content_facet_attributes": {
    "applicable_module_stream_count": 0,
    "applicable_package_count": 0,
    "content_source": {
            "id": 17,
            "name": "satcap.rest.example.com",
            "url": "https://satcap.rest.example.com:9090"
        },
        "content_source_id": 17,
        "content_source_name": "satcap.rest.example.com",
        "content_view": {
            "id": 67,
            "name": "ccv-azure-infra-rhel7"
        },
        "content_view_id": 67,
        "content_view_name": "ccv-azure-infra-rhel7",
        "errata_counts": {
            "bugfix": 0,
            "enhancement": 0,
            "security": 0,
            "total": 0
        },
        "id": 32255,
        "kickstart_repository": {
            "id": 5772,
            "name": "Red Hat Enterprise Linux 7 Server Kickstart x86_64 7.8"
        },
        "kickstart_repository_id": 5772,
        "kickstart_repository_name": "5772",
        "lifecycle_environment": {
            "id": 5,
            "name": "Prod"
        },
        "lifecycle_environment_id": 5,
        "lifecycle_environment_name": "Prod",
        "upgradable_module_stream_count": 0,
        "upgradable_package_count": 0,
        "uuid": "dab2d66b-1a73-490a-aa9d-3f036658980a"
    },

Solution

  • If you want to do a dictionary out of those data you listed, you could use the filter json_query, which is using JMESPath to parse and process JSON.

    In order to extract a dictionary looking like:

    {
        "architecture_name": "x86_64",
        "content_source_name": "satcap.rest.example.com",
        "content_source_url": "https://satcap.rest.example.com:9090",
        "content_view_name": "ccv-azure-infra-rhel7",
        "egistered_through": "satcap.rest.example.com",
        "id": 33485,
        "ip": "192.168.88.88",
        "lifecycle_environment_name": "Prod",
        "name": "invwharn108.test.exampl.com"
    }
    

    You could use JMESPath to filter multiselect hashes.

    Here is a JMESPath query that would give this result:

    json.results[*].{
        "architecture_name": architecture_name,
        "content_source_name": content_facet_attributes.content_source.name, 
        "content_source_url": content_facet_attributes.content_source.url, 
        "content_view_name": content_facet_attributes.content_view.name, 
        "lifecycle_environment_name": content_facet_attributes.lifecycle_environment.name, 
        "id": id, 
        "ip": ip, 
        "name": name, 
        "egistered_through": subscription_facet_attributes.registered_through 
    }
    

    Here is an example playbook using this:

    - hosts: localhost
      gather_facts: no
    
      tasks:  
        - debug:
            msg: >-
              {{
                (
                  response_find | json_query('
                    json.results[*].{
                      "architecture_name": architecture_name,
                      "content_source_name": content_facet_attributes.content_source.name, 
                      "content_source_url": content_facet_attributes.content_source.url, 
                      "content_view_name": content_facet_attributes.content_view.name, 
                      "lifecycle_environment_name": content_facet_attributes.lifecycle_environment.name, 
                      "id": id, 
                      "ip": ip, 
                      "name": name, 
                      "egistered_through": subscription_facet_attributes.registered_through 
                    }
                  ')
                ).0
              }}
          vars:
            {
              "response_find": {
                "apipie_checksum": "7533ab625c45a3819647f4fb4c9394c2832c0180",
                "cookies": {
                  "_session_id": "25872833d9e2723073797fbd8cfa2d63"
                },
                "cookies_string": "_session_id=25872833d9e2723073797fbd8cfa2d63",
                "foreman_version": "1.24.1.21",
                "json": {
                  "page": 1,
                  "per_page": 20,
                  "results": [{
                    "architecture_id": 1,
                    "architecture_name": "x86_64",
                    "build": false,
                    "capabilities": [
                      "build"
                    ],
                    "certname": "invwharn108.test.exampl.com",
                    "comment": null,
                    "content_facet_attributes": {
                      "applicable_module_stream_count": 0,
                      "applicable_package_count": 0,
                      "content_source": {
                        "id": 17,
                        "name": "satcap.rest.example.com",
                        "url": "https://satcap.rest.example.com:9090"
                      },
                      "content_source_id": 17,
                      "content_source_name": "satcap.rest.example.com",
                      "content_view": {
                        "id": 67,
                        "name": "ccv-azure-infra-rhel7"
                      },
                      "content_view_id": 67,
                      "content_view_name": "ccv-azure-infra-rhel7",
                      "errata_counts": {
                        "bugfix": 0,
                        "enhancement": 0,
                        "security": 0,
                        "total": 0
                      },
                      "id": 32255,
                      "kickstart_repository": {
                        "id": 5772,
                        "name": "Red Hat Enterprise Linux 7 Server Kickstart x86_64 7.8"
                      },
                      "kickstart_repository_id": 5772,
                      "kickstart_repository_name": "5772",
                      "lifecycle_environment": {
                        "id": 5,
                        "name": "Prod"
                      },
                      "lifecycle_environment_id": 5,
                      "lifecycle_environment_name": "Prod",
                      "upgradable_module_stream_count": 0,
                      "upgradable_package_count": 0,
                      "uuid": "dab2d66b-1a73-490a-aa9d-3f036658980a"
                    },
                    "created_at": "2020-12-23 13:19:35 UTC",
                    "disk": null,
                    "domain_id": 25,
                    "domain_name": "test.example-aws.example.com",
                    "enabled": true,
                    "environment_id": null,
                    "environment_name": null,
                    "errata_status": 1,
                    "errata_status_label": "Could not calculate errata status, ensure host is registered and the katello-host-tools package is installed",
                    "global_status": 1,
                    "global_status_label": "Warning",
                    "hostgroup_id": 982,
                    "hostgroup_name": "infra",
                    "hostgroup_title": "NXDI-hg-aws/west-europe/infra",
                    "id": 33485,
                    "image_file": "",
                    "image_id": null,
                    "image_name": null,
                    "installed_at": null,
                    "ip": "192.168.88.88",
                    "ip6": null,
                    "last_compile": "2020-12-23 13:19:38 UTC",
                    "last_report": null,
                    "location_id": 37,
                    "location_name": "west-europe",
                    "mac": "02:78:43:60:ee:fb",
                    "managed": false,
                    "medium_id": null,
                    "medium_name": null,
                    "model_id": 25,
                    "model_name": "HVM domU",
                    "name": "invwharn108.test.exampl.com",
                    "openscap_proxy": null,
                    "openscap_proxy_id": null,
                    "openscap_proxy_name": null,
                    "operatingsystem_id": 21,
                    "operatingsystem_name": "RedHat 7.8",
                    "organization_id": 3,
                    "pxe_loader": "PXELinux BIOS",
                    "subnet_id": 65,
                    "subnet_name": "aws-west-europe-infra",
                    "subscription_facet_attributes": {
                      "autoheal": true,
                      "hypervisor": false,
                      "id": 33593,
                      "last_checkin": "2020-12-23 13:19:42 UTC",
                      "purpose_addons": [],
                      "purpose_role": "",
                      "purpose_usage": "",
                      "registered_at": "2020-12-23 13:19:35 UTC",
                      "registered_through": "satcap.rest.example.com"
                    },
                    "subscription_global_status": 1,
                    "subscription_status": 1,
                    "subscription_status_label": "Partially entitled"
                  }],
                  "search": "name=invwharn108.test.exampl.com",
                  "sort": {
                    "by": null,
                    "order": null
                  },
                  "subtotal": 1,
                  "total": 13121
                },
                "msg": "OK (unknown bytes)",
                "x_request_id": "e02dd9b3-b2f8-4c39-8bf5-1ce1897d9548"
              }
            }
    

    Which gives the recap:

    PLAY [localhost] *************************************************************************************************
    
    TASK [debug] *****************************************************************************************************
    ok: [localhost] => {
        "msg": {
            "architecture_name": "x86_64",
            "content_source_name": "satcap.rest.example.com",
            "content_source_url": "https://satcap.rest.example.com:9090",
            "content_view_name": "ccv-azure-infra-rhel7",
            "egistered_through": "satcap.rest.example.com",
            "id": 33485,
            "ip": "192.168.88.88",
            "lifecycle_environment_name": "Prod",
            "name": "invwharn108.test.exampl.com"
        }
    }
    
    PLAY RECAP *******************************************************************************************************
    localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0