简体   繁体   English

Ansible“gcp_compute_instance”模块不会将外部 IP 分配给 VM 实例

[英]Ansible "gcp_compute_instance" module won't assign External IP to VM instance

- name: Create Disk
  gcp_compute_disk:
    name: "{{app_name}}-disk"
    size_gb: 50
    source_image: "{{source_image}}"
    zone: "{{ gcp_zone }}"
    project: web-project
    auth_kind: "{{ gcp_cred_kind }}"
    state: present
  register: disk

- name: Reserve IP Address
  gcp_compute_address:
    name: "{{app_name}}-address"
    region: "{{region}}"
    project: vpc-project
    auth_kind: "{{ gcp_cred_kind }}"
    state: present
  register: address

- name: Create VM Instance
  gcp_compute_instance:
    name: "{{app_name}}-vm"
    machine_type: "{{instance_type}}"
    disks:
    - auto_delete: true
      boot: true
      source: "{{disk}}"
    network_interfaces:
    - subnetwork: 
        selfLink: "{{subnetwork}}"
        access_configs:
        - name: External NAT
          nat_ip: "{{ address }}"
          type: ONE_TO_ONE_NAT
    zone: "{{ gcp_zone }}"
    project: web-project
    auth_kind: "{{ gcp_cred_kind }}"
    state: present
  register: instance

The VPC exists in the "vpc-project" and is shared with "web-project", which has no.network of its own. VPC 存在于“vpc-project”中,并与“web-project”共享,后者没有自己的网络。

Region: us-west1 Zone: us-west1-b地区:us-west1 地区:us-west1-b

When I run the code, it successfully creates the VM using the other projects VPC and assigns an internal IP to it.当我运行代码时,它使用其他项目 VPC 成功创建了 VM,并为其分配了一个内部 IP。

However, it does NOT assign the external IP to it.但是,它不会将外部 IP 分配给它。 I have confirmed that the address was created and in the correct region, but it's just sitting there unused.我已确认地址已创建且位于正确的区域,但它只是闲置在那里。

Just for testing, I had it create an external address and a VM in the projects local.network but it still didn't assign the public address to it.只是为了测试,我让它在项目 local.network 中创建了一个外部地址和一个 VM,但它仍然没有为其分配公共地址。

Am I missing something?我错过了什么吗?

John and Ferregina were correct - an external IP from one project cannot be assigned to a resource in another project. John 和 Ferregina 是正确的 - 来自一个项目的外部 IP 不能分配给另一个项目中的资源。 I changed the playbook accordingly.我相应地改变了剧本。

However that was not the cause of my pain.然而,这不是我痛苦的原因。 It was a playbook-related issue.这是一个与剧本相关的问题。 Using the "selfLink" in the play was causing it to completely ignore the entire part about "access_configs" for some reason.由于某种原因,在剧中使用“selfLink”导致它完全忽略了关于“access_configs”的整个部分。

I changed it from this:我改变了它:

network_interfaces:
- subnetwork: 
    selfLink: "{{subnetwork}}"
    access_configs:
    - name: External NAT
      nat_ip: "{{ address }}"
      type: ONE_TO_ONE_NAT

To this:对此:

network_interfaces:
- subnetwork: "{{subnetwork_info.resources[0]}}"
  access_configs:
  - name: External NAT
    nat_ip: "{{ external_address }}"
    type: ONE_TO_ONE_NAT

And it worked.它奏效了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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