使用Ansible将启动磁盘附加到Gcloud实例(如果存在)

时间:2018-09-21 13:38:38

标签: ansible gcloud

我正在使用Ansible在Google Cloud中创建实例,但是当我要将现有磁盘附加到新的计算引擎时,我无法将其附加或添加到实例中。

- name: Launch instances
  gce:
    instance_names: mongo
    machine_type: "n1-standard-1"
    image: "debian-9"
    service_account_email: "xxxx@xxxx.iam.gserviceaccount.com"
    credentials_file: "gcp-credentials.json"
    project_id: "learning"
    disk_size: 10
    disk_auto_delete: false
    preemptible: true
    tags: "mongo-server"
  register: gce

- name: Wait for SSH for instances 
  wait_for:
    delay: 1
    host: "{{ item.public_ip }}"
    port: 22
    state: started
    timeout: 30
  with_items: "{{ gce.instance_data }}"

我的错误是:

  

错误是:libcloud.common.google.ResourceExistsError:{'domain':'global','message':“资源'projects / xxx-xxx / zones / us-central1-a / disks / mongo'已经存在”,“原因”:“ alreadyExists”}

可以通过任何形式使用Ansible配置此选项吗?为此,我正在使用外部脚本。

1 个答案:

答案 0 :(得分:1)

可以在“ disks”属性下以列表形式提供现有磁盘,第一个条目需要是Boot dik

https://docs.ansible.com/ansible/2.6/modules/gce_module.html

  - gce:
  instance_names: my-test-instance
  zone: us-central1-a
  machine_type: n1-standard-1
  state: present
  metadata: '{"db":"postgres", "group":"qa", "id":500}'
  tags:
    - http-server
    - my-other-tag
  disks:
    - name: disk-2
      mode: READ_WRITE
    - name: disk-3
      mode: READ_ONLY