使用ansible和gcp创建外部和内部IP

时间:2019-12-21 20:31:17

标签: google-cloud-platform ansible

我正在使用Ansible 2.9.1使用GCP创建一些计算实例。我想为每个实例创建一个外部IP,我已经使用以下代码设法做到了:

  - name: create an instance
    gcp_compute_instance:
      name: "{{ compute_inst }}"
      machine_type: "{{ machine }}"
      disks:
      - auto_delete: 'true'
        boot: 'true'
        source: "{{ disk }}"
      network_interfaces:
        - access_configs:
          - name: External NAT
            nat_ip: "{{ address }}"
            type: ONE_TO_ONE_NAT
      zone: "{{ zone }}"
      project: "{{ gcp_project }}"
      auth_kind: serviceaccount
      service_account_file: "{{ gcp_cred_file }}"
      state: present
    register: instance

其中地址是外部gcp_compute_address。但是,我也想设置一个静态内部IP。根据旧的gcp_compute_instance documentation,您应该可以在 network_interfaces 列表中设置 network_ip

  

要分配给该网络接口的实例的IPv4内部网络地址。如果用户未指定,则系统会分配一个未使用的内部IP。

我已经使用以下代码尝试过此操作:

  - name: create an instance
    gcp_compute_instance:
      name: "{{ compute_inst }}"
      machine_type: "{{ machine }}"
      disks:
      - auto_delete: 'true'
        boot: 'true'
        source: "{{ disk }}"
      network_interfaces:
        - access_configs:
          - name: External NAT
            nat_ip: "{{ address }}"
            type: ONE_TO_ONE_NAT
        - network_ip: "{{ internal_ip }}" 
      zone: "{{ zone }}"
      project: "{{ gcp_project }}"
      auth_kind: serviceaccount
      service_account_file: "{{ gcp_cred_file }}"
      state: present
    register: instance

其中internal_ip是包含内部IP地址的字符串。当我运行剧本时,出现以下错误:

Networks must be distinct for NICs attached to a VM.

因此,我再次通读了文档,并发现了 network_interfaces 的以下内容:

  

此接口的配置数组。这指定了如何配置此接口以与其他网络服务交互,例如连接到Internet。每个实例仅支持一个网络接口。

我能够使用GCP控制台创建一个实例,在该实例中,我既可以获取外部IP,也可以指定内部IP。是否有人对我在ansible中做错什么有任何想法,或者有什么建议可以尝试?

0 个答案:

没有答案