ansible 如何从嵌套的 json 输出中获取多个值

ansible how to fetch multiple values from a nested json like output

我期待一种从嵌套变量(具有 dict 和列表值)中列出值的方法,即 registerresponse_find 我主要在 json 格式和 json 中的大多数值嵌套在 [] 列表中,如构造。

如果我从中获取单独的值,它会以某种方式工作,如下所示:

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

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

但是我在下面做的时候没有用:

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

下面是我期待从这个嵌套输出中获取的几个值:

"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"

嵌套Json输出:

{
    "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",
    }
}

我的玩法:

---
- 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']

结果:

"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"
    },

如果你想用你列出的那些数据做一个字典,你可以使用过滤器 json_query, which is using JMESPath 来解析和处理 JSON。

为了提取如下字典:

{
    "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"
}

您可以使用 JMESPath 来 filter multiselect hashes

这是一个会给出此结果的 JMESPath 查询:

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 
}

这是一个使用这个的示例剧本:

- 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"
          }
        }

回顾一下:

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