Home > Back-end >  To fetch a instance name and IP from the gcp_compute_instance_info module
To fetch a instance name and IP from the gcp_compute_instance_info module

Time:05-10

I'm trying to get the list of all instance and its IP, I can able to fetch the instance name as info.resources.0.name, info.resources.1.name, info.resources.2.name etc. Similarly to fetch the network IP I tried info.resources.0.networkInterfaces.networkIP but that didn't work. I need help to rephrase the playbook to use with_items or loop to fetch the list of instances and it's IP. Below is the playbook task:

   - name: get info on an instance
     gcp_compute_instance_info:
         zone: "{{ zone }}"
         project: "{{ gcp_project }}"
         auth_kind: "{{ gcp_cred_kind }}"
         service_account_file: "{{ gcp_cred_file }}"
     register: info

   - debug:
       msg: "name - {{ info.resources.0.name }}"

Below is the detailed json output of single instance info:


PLAY [Create an instance] ******************************************************

TASK [get info on an instance] *************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": {
        "changed": false,
        "failed": false,
        "resources": [
            {
                "canIpForward": false,
                "confidentialInstanceConfig": {
                    "enableConfidentialCompute": false
                },
                "cpuPlatform": "AMD Rome",
                "creationTimestamp": "2022-05-04T23:36:13.606-07:00",
                "deletionProtection": false,
                "description": "",
                "disks": [
                    {
                        "autoDelete": true,
                        "boot": true,
                        "deviceName": "ansible-automation-platform",
                        "diskSizeGb": "50",
                        "guestOsFeatures": [
                            {
                                "type": "UEFI_COMPATIBLE"
                            },
                            {
                                "type": "VIRTIO_SCSI_MULTIQUEUE"
                            },
                            {
                                "type": "SEV_CAPABLE"
                            },
                            {
                                "type": "GVNIC"
                            }
                        ],
                        "index": 0,
                        "interface": "SCSI",
                        "kind": "compute#attachedDisk",
                        "licenses": [
                            "https://www.googleapis.com/compute/v1/projects/rhel-cloud/global/licenses/rhel-8-server"
                        ],
                        "mode": "READ_WRITE",
                        "source": "https://www.googleapis.com/compute/v1/projects/company_project_prod/zones/us-central1-a/disks/ansible-automation-platform",
                        "type": "PERSISTENT"
                    }
                ],
                "displayDevice": {
                    "enableDisplay": false
                },
                "fingerprint": "tRhWl0y3JhA=",
                "id": "3629531832562667187",
                "kind": "compute#instance",
                "labelFingerprint": "42WmSpB8rSM=",
                "lastStartTimestamp": "2022-05-05T22:09:57.920-07:00",
                "lastStopTimestamp": "2022-05-05T07:27:28.617-07:00",
                "machineType": "https://www.googleapis.com/compute/v1/projects/company_project_prod/zones/us-central1-a/machineTypes/e2-custom-4-8192",
                "metadata": {
                    "fingerprint": "3tGBZbor1YQ=",
                    "items": [
                        {
                            "key": "ssh-keys",
                            "value": "user_name:ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEYc1v1Vqgy1tiS1TIDtgNqd1M0Ycp/10q5WBYH8f1E9iXfdZYj6sRSVAzh4C4D0LD925m mH6MPyr5393vJNcA= google-ssh {\"userName\":\"[email protected]\",\"expireOn\":\"2022-05-09T06:13:35 0000\"}\nuser_name:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDHsNAKOXJCdLjgiYZrlYIQ0NS dQcBIyxlJXNirKi4zpS4cOTM42FEFyEfByT5s6oexnh/LWLmB8qKGF7hTI7Xup44Nh0W20m2VMHwhGalDELa KNtrVEg429KutUOC7VHNoSbthj/sVyJ7YiZuRLEPmWlh2VZPLqIvdVgIZ5DhOcAVpeoPEDEVwAMUgceci1d7iyqyVsFA6CcyFTcXqouUgXpjzm6a 6YL4wpSFcZntoVxCA8TFb35ZzXdsSoh1U86GGWRqsntpXHaI5H90z DpwDg/G8BWSjtZ5vBBgk48n08yg3kLSG5B3mJKtilJhCdmSWqRAqOxpRUvA6kkpf google-ssh {\"userName\":\"[email protected]\",\"expireOn\":\"2022-05-09T06:13:51 0000\"}"
                        }
                    ],
                    "kind": "compute#metadata"
                },
                "name": "ansible-automation-platform",
                "networkInterfaces": [
                    {
                        "fingerprint": "WE1S5NA9wnc=",
                        "kind": "compute#networkInterface",
                        "name": "nic0",
                        "network": "https://www.googleapis.com/compute/v1/projects/company_project_prod/global/networks/default",
                        "networkIP": "10.128.0.17",
                        "stackType": "IPV4_ONLY",
                        "subnetwork": "https://www.googleapis.com/compute/v1/projects/company_project_prod/regions/us-central1/subnetworks/default"
                    }
                ],
                "reservationAffinity": {
                    "consumeReservationType": "ANY_RESERVATION"
                },
                "scheduling": {
                    "automaticRestart": true,
                    "onHostMaintenance": "MIGRATE",
                    "preemptible": false,
                    "provisioningModel": "STANDARD"
                },
                "selfLink": "https://www.googleapis.com/compute/v1/projects/company_project_prod/zones/us-central1-a/instances/ansible-automation-platform",
                "serviceAccounts": [
                    {
                        "email": "[email protected]",
                        "scopes": [
                            "https://www.googleapis.com/auth/devstorage.read_only",
                            "https://www.googleapis.com/auth/logging.write",
                            "https://www.googleapis.com/auth/monitoring.write",
                            "https://www.googleapis.com/auth/servicecontrol",
                            "https://www.googleapis.com/auth/service.management.readonly",
                            "https://www.googleapis.com/auth/trace.append"
                        ]
                    }
                ],
                "shieldedInstanceConfig": {
                    "enableIntegrityMonitoring": true,
                    "enableSecureBoot": false,
                    "enableVtpm": true
                },
                "shieldedInstanceIntegrityPolicy": {
                    "updateAutoLearnPolicy": true
                },
                "startRestricted": false,
                "status": "RUNNING",
                "tags": {
                    "fingerprint": "42WmSpB8rSM="
                },
                "zone": "https://www.googleapis.com/compute/v1/projects/company_project_prod/zones/us-central1-a"
            },
                    ]
    }
}

CodePudding user response:

Given the simplified data

  info:
    changed: false
    failed: false
    resources:
    - canIpForward: false
      name: ansible-automation-platform
      networkInterfaces:
      - name: nic0
        networkIP: 10.128.0.17
        stackType: IPV4_ONLY
      - name: nic1
        networkIP: 10.128.0.18
        stackType: IPV4_ONLY
      - name: nic2
        networkIP: 10.128.0.19
        stackType: IPV4_ONLY

Iterate the selected names of the resources and combine a dictionary, e.g.

    - set_fact:
        name_ip: "{{ name_ip|d({})|
                     combine({item: dict(info.resources|json_query(_query))}) }}"
      loop:
        - ansible-automation-platform
      vars:
        _query: "[?name == '{{ item }}'].networkInterfaces[].[name,networkIP]"

gives

  name_ip:
    ansible-automation-platform:
      nic0: 10.128.0.17
      nic1: 10.128.0.18
      nic2: 10.128.0.19
  • Related