Problems with gcp_compute_instance

466 views
Skip to first unread message

Thomas Kube

unread,
Mar 26, 2020, 7:26:12 AM3/26/20
to Ansible Project
Hello,

I'm branching to a problem with gcp_compute_instance. The server should be created inside a project that has a shared subnet in custom mode.

These are the individual steps to gather the informations for the network:

- name: "Gather network information for {{ vpc_project }}"
  gcp_compute_network_info:
    filters:
    - name = "{{ vpc_network }}"
    project: "{{ vpc_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file }}"
  register: vpcnetwork

- name: Create dictionary VPCselfLink
  set_fact:
    VPCselfLink: "{{ VPCselfLink|default({}) | combine( {'selfLink': vpcnetwork['items'][0].selfLink} ) }}"

- name: "Gather subnet information from project {{ vpc_project }} and subnetwork {{ vpc_subnetwork }}"
  gcp_compute_subnetwork_info:
    region: region
    project: myproject
    filters:
    - name = "{{ vpc_subnetwork }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file }}"
    scopes:
      - https://www.googleapis.com/auth/compute   
  register: vpcsubnetwork

- name: Create dictionary selfLink
  set_fact:
    selfLink: "{{ selfLink|default({}) | combine( {'selfLink': vpcsubnetwork['items'][0].selfLink} ) }}"

- name: "Create IP address for server {{ instancename }}"
  gcp_compute_address:
    name: "ip-{{ instancename }}"
    region: "{{ region }}"
    project: "{{ gcp_project }}"
    address_type: INTERNAL
    subnetwork: "{{ selfLink }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file }}"
    scopes:
      - https://www.googleapis.com/auth/compute   
    state: present
  register: ipaddress

When I make this call for the network:

       network_interfaces:
          - network: "{{ VPCselfLink }}"
          - network_ip: "{{ ipaddress.address }}"
          - subnetwork: "{{ selfLink }}" 

I get this error:

Fatal: [localhost]: FAILED! => {"changed": false, "msg": "GCP returned error: {'error': {'errors': [{'domain': 'global', 'reason': 'invalid', 'message': 'Invalid value for field \\'resource.networkInterfaces[0]\\': \\'{  \"network\": \"https://www.googleapis.com/compute/v1/projects/<project>/global/networks/<network>...\\'. Subnetwork should be specified for custom subnetmode network'}], 'code': 400, 'message': 'Invalid value for field \\'resource.networkInterfaces[0]\\': \\'{  \"network\": \"https://www.googleapis.com/compute/v1/projects/sap-shared-vpn/global/networks/vpc-ott...\\'. Subnetwork should be specified for custom subnetmode network'}}"}

When I make this call for the network:

    network_interfaces:
       - network_ip: "{{ ipaddress.address }}"
       - subnetwork: "{{ selfLink }}"

I get this error:

fatal: [localhost]: FAILED! => {"changed": false, "msg": "GCP returned error: {'error': {'errors': [{'domain': 'global', 'reason': 'invalid', 'message': \"Invalid value for field 'resource.networkInterfaces[0].network': ''. The referenced network resource cannot be found.\"}], 'code': 400, 'message': \"Invalid value for field 'resource.networkInterfaces[0].network': ''. The referenced network resource cannot be found.\"}}"}

What am I doing wrong? I am grateful for any help.

Thomas from Germany










Thomas Kube

unread,
Mar 26, 2020, 7:53:47 AM3/26/20
to Ansible Project
If I specify only the subnetwork value, then the server is assigned an IP address:

    network_interfaces:
       - subnetwork: "{{ selfLink }}"

Thomas

Thomas Kube

unread,
Mar 26, 2020, 9:44:57 AM3/26/20
to Ansible Project
Sorry, found it:

network_interfaces:
       - network_ip: "{{ ipaddress.address }}"
       - subnetwork: "{{ selfLink }}"

is wrong


network_interfaces:
       - subnetwork: "{{ selfLink }}"
         network_ip: "{{ ipaddress.address }}"

is right. My fault.

Thomas

Sanjay Sreeram

unread,
Oct 2, 2020, 11:38:26 AM10/2/20
to Ansible Project
Following resolved the issue for me,
 network_interfaces:
        - subnetwork:
            selfLink: "{{ subnet.resources[0].selfLink }}"
Note: i have created register:subnet to gcp_compute_subnetwork_info.

Reply all
Reply to author
Forward
0 new messages