简体   繁体   English

ansible gcp_compute_instance - 在创建 vm 实例时附加现有的外部 ip 地址

[英]ansible gcp_compute_instance - attach existing external ip address when creating vm instance

I want to attach an existing (created before) external ip address when creating vm instance:我想在创建 vm 实例时附加一个现有的(之前创建的)外部 ip 地址:

  • create address创建地址
- name: create address
  gcp_compute_address:
    name: my-external-ip
    region: europe-west1
    address_type: '{{ item.compute_address_type | default("EXTERNAL") }}'
    network_tier: "{{ item.compute_address_network_tier | default("PREMIUM") }}"
    project: "{{ gcp.project_id }}"
    auth_kind: serviceaccount
    service_account_file: "{{ gcp.credentials_file }}"
    state: present
  with_items: "{{ compute_address }}"
  register: address
  tags: create_address
  • create instance创建实例
- name: create compute instances
  gcp_compute_instance:
    name: my-instance
    zone: europe-west1-b
    machine_type: "{{ item.instance_type }}"
    metadata: "{{ item.instance_metadata | default(omit) }}"
    labels: "{{ item.instance_label | default(omit) }}"
    tags: "{{ item.instance_tags | default(omit) }}"
    scopes: '{{ item.instance_scopes | default("https://www.googleapis.com/auth/cloud-platform") }}'
    disks: "{{ item.disks }}"
    network_interfaces: "{{ item.network_interfaces }}"
    project: "{{ gcp.project_id }}"
    auth_kind: serviceaccount
    service_account_file: "{{ gcp.credentials_file }}"
    state: present
  with_items: "{{ instances }}"
  register: instance
  tags:
 - create_instance
  • vars for network_interfaces network_interfaces 的变量
instances:
  network_interfaces:
 - network:
      name: default
    access_configs:
    - name: External NAT
      nat_ip:
        address: projects/project_id/regions/europe-west1/addresses/my-external-ip
      type: ONE_TO_ONE_NAT
  • The address has been created:地址已创建:
$ gcloud compute addresses describe my-external-ip --region europe-west1
address: 35.X.Y.Z
addressType: EXTERNAL
creationTimestamp: '2020-07-24T03:10:44.048-07:00'
description: ''
id: '1804649404875345227'
kind: compute#address
name: my-external-ip
networkTier: PREMIUM
region: https://www.googleapis.com/compute/v1/projects/project_id/regions/europe-west1
selfLink: https://www.googleapis.com/compute/v1/projects/project_id/regions/europe-west1/addresses/my-external-ip
status: RESERVED

Then I run, there's an error:然后我运行,有一个错误:

'message': "Invalid value for field 'resource.networkInterfaces[0].accessConfigs[0].natIP':
'projects/neoevolution/regions/europe-west1/addresses/neoevolution-dev-eip-1'. 
The specified external IP address 
'projects/neoevolution/regions/europe-west1/addresses/neoevolution-dev-eip-1' 
was not found in region 'europe-west1'

THANKS FOR YOUR HELP, PEACE.谢谢你的帮助,和平。

Check if your external reserved IP looks good(belong the correct region etc.):检查您的外部保留 IP 是否看起来不错(属于正确的区域等):

gcloud compute addresses list
NAME            ADDRESS/RANGE   TYPE      PURPOSE  NETWORK  REGION        SUBNET  STATUS
my-external-ip  35.207.102.185  EXTERNAL                    europe-west3          RESERVED

Try to reference the IP by the address itself rather than the name. 尝试通过地址本身而不是名称来引用 IP。 I reproduced in a simple deployment and this worked for me: 我在一个简单的部署中进行了复制,这对我有用:
 "accessConfigs": [ { "kind": "compute#accessConfig", "name": "External NAT", "type": "ONE_TO_ONE_NAT", "natIP": "35.207.102.185", "networkTier": "STANDARD" } ]

And of course make sure you are referencing your network variables correctly.当然,请确保您正确引用了网络变量。 You may try to hard-code the values to narrow down the possible source of the problem.您可以尝试对这些值进行硬编码以缩小问题的可能来源。

Peace;和平; ;) ;)

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

相关问题 Ansible“gcp_compute_instance”模块不会将外部 IP 分配给 VM 实例 - Ansible "gcp_compute_instance" module won't assign External IP to VM instance 无法绑定到谷歌计算引擎虚拟机实例的外部 IP 地址 - Cannot bind to external IP address of google compute engine VM instance 如何使用 CLI 将 static 外部 IP 地址附加到 Google 云计算实例 - How to attach a static external IP address to a google cloud compute instance using the CLI Google Compute Engine:在创建新的VM实例时指定内部IP地址? - Google Compute Engine: Specify internal IP-address while creating new VM instance? 通过 Terraform 配置没有外部 IP 的 GCP VM 实例 - Provision a GCP VM instance with no external IP via Terraform 通过浏览器在 GCP VM 实例中通过 SSH 登录时,IP 地址是否相同? - Is the IP address the same when logging in via SSH in GCP VM instance through the browser? 无法从外部 IP 访问 GCP 虚拟机实例 - GCP VM instance cannot be accessed from external IP 创建实例组时不显示 VM Instance GCP - VM Instance GCP doesn't appear when creating an instance group GCP 获取从 google_compute_instance_template 创建的计算实例的公共 IP 地址 - GCP Fetch Public IP address for compute instance created from google_compute_instance_template 确定静态外部IP地址与哪个计算实例相关联 - Identify which compute instance a static external IP address is associated with
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM