How to Add external IP to an existing GCP VM

**I want to add an external IP to the existing VM in GCP **.

  • I have a GCP VM with epharmal IP I want to attach a reserved external IP to the instance
  • I am using google.cloud collection
  • when I run a playbook with the desired IP playbook just ignores it.
    - name: get info on an address
      gcp_compute_address_info:
        region: us-east4
        filters:
        - name = ip-name
        project: gcp-project
        auth_kind: serviceaccount
        service_account_file: "/root/creds.json"
      register: address
    - name: update instance
      gcp_compute_instance:
        auth_kind: serviceaccount
        service_account_file: "{{ auth_file_name_path }}"
        project: lab-wifi-staging
        zone: us-east4-a
        name: "{{ server_name }}"
        network_interfaces:
        - subnetwork: {selfLink: "projects/lab-wifi-staging/regions/{{region}}/subnetworks/{{subnet}}"}
          access_configs:
          - name: 'External NAT'
            nat_ip: "{{ address }}"
            type: 'ONE_TO_ONE_NAT'
  • Set at least one tag (the experts follow the tags, so the right people will find you if you tag)

Ensure You Have the Reserved IP Address:
First, confirm that you have a reserved static external IP address. If not, you need to reserve one before proceeding:

- name: Reserve static external IP
  gcp_compute_address:
    name: "ip-name"
    region: "us-east4"
    project: "gcp-project"
    auth_kind: serviceaccount
    service_account_file: "/root/creds.json"
  register: reserved_ip

Fetch Details of the Reserved IP:
Your playbook seems to attempt this. However, make sure you’re registering the natIP from the address:

- name: Get info on an address
  gcp_compute_address_info:
    region: us-east4
    filters:
    - name = ip-name
    project: gcp-project
    auth_kind: serviceaccount
    service_account_file: "/root/creds.json"
  register: address_info

Update the VM Instance:
Here, you’re trying to update the VM with the new IP, but there’s an issue with how you’re referencing address. Instead of passing the entire address object, you should pass the natIP property of the address:

- name: Update instance with reserved IP
  gcp_compute_instance:
    auth_kind: serviceaccount
    service_account_file: "{{ auth_file_name_path }}"
    project: lab-wifi-staging
    zone: us-east4-a
    name: "{{ server_name }}"
    network_interfaces:
    - subnetwork: {selfLink: "projects/lab-wifi-staging/regions/{{region}}/subnetworks/{{subnet}}"}
      access_configs:
      - name: 'External NAT'
        nat_ip: "{{ address_info.resources[0].address }}"
        type: 'ONE_TO_ONE_NAT'
  register: updated_instance