简体   繁体   中英

Specifying a subnetwork for Ansible google.cloud.compute_instance

I have tried every combination I can conceive of to specify to deploy a google compute instance into a particular subnet ( subnetX ) in network ( networkY ).

- name: create a instance
  google.cloud.gcp_compute_instance:
    name: test_object
    machine_type: n1-standard-1
    disks:
    - auto_delete: 'true'
      boot: 'true'
      source: "{{ disk }}"
    - auto_delete: 'true'
      interface: NVME
      type: SCRATCH
      initialize_params:
        disk_type: local-ssd
    labels:
      environment: production
    network_interfaces:   # <<< does not work. API request is made without a network_interface
    - network:
       selfLink: "https://blah/blah/blah/networkY"
      subnetwork:
       selfLink: "https://blah/blah/blah/subnetworkX"
    zone: us-central1-a
    project: test_project
    auth_kind: serviceaccount
    service_account_file: "/tmp/auth.pem"
    state: present

When using subnetwork you should not specify a network .

To be more precise this is the workaround for this problem.

---
- name: create a network
  gcp_compute_network:
    name: ansible-network
    auto_create_subnetworks: yes
    project: "{{ lookup('env','GCP_PROJECT') }}"
    state: present
  register: network

- name: Get Network URL
  set_fact:
    network_url: "{{ network | json_query(jmesquery) }}"
  vars:
    jmesquery: "{selfLink: selfLink}"

- name: create a firewall
  gcp_compute_firewall:
    name: ansible-firewall
    network: "{{ network_url }}"
    allowed:
    - ip_protocol: tcp
      ports: ['80','22']
    target_tags:
      - apache-http-server
    source_ranges: ['0.0.0.0/0']
    project: "{{ lookup('env','GCP_PROJECT') }}"
    state: present
  register: firewall

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM