ansible gcp_compute_instance-创建虚拟机实例时附加现有的外部IP地址

时间:2020-07-24 10:31:10

标签: google-cloud-platform ansible devops

我想在创建虚拟机实例时附加一个现有的(之前创建的)外部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
  • 用于网络接口的变量
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'

感谢您的帮助,和平。

1 个答案:

答案 0 :(得分:0)

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

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

和平! ;)