繁体   English   中英

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

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

我想在创建 vm 实例时附加一个现有的(之前创建的)外部 ip 地址:

  • 创建地址
- 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
  • 创建实例
- 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
  • 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
  • 地址已创建:
$ 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

然后我运行,有一个错误:

'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'

谢谢你的帮助,和平。

检查您的外部保留 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

尝试通过地址本身而不是名称来引用 IP。 我在一个简单的部署中进行了复制,这对我有用:
 "accessConfigs": [ { "kind": "compute#accessConfig", "name": "External NAT", "type": "ONE_TO_ONE_NAT", "natIP": "35.207.102.185", "networkTier": "STANDARD" } ]

当然,请确保您正确引用了网络变量。 您可以尝试对这些值进行硬编码以缩小问题的可能来源。

和平; ;)

暂无
暂无

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

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