如何使用 gce_pd Ansible 模块将磁盘添加到 Google Compute Engine 实例?

How to add a disk to a Google Compute Engine instance with the gce_pd Ansible module?

我是 Ansible 的新手,但我有一个可以创建 Google Compute Engine 实例的工作手册。我按照 Compute Engine Management with Puppet, Chef, Salt, and Ansible - Appendix 的 Ansible 部分来实现这一点。

现在我想扩展 playbook 以将额外的永久性磁盘添加到我创建的实例中,所以我正在关注 gce_pd module documentation。然而,我遇到的问题是文档中给出的 yaml 片段超出了更广泛的剧本的上下文:

# Simple attachment action to an existing instance
- local_action:
    module: gce_pd
    instance_name: notlocalhost
    size_gb: 5
    name: pd

因此,当我尝试将此代码段包含在我的剧本中时,出现语法错误:

The offending line appears to be:

    - local_action:
        module: gce_pd
^ here

上次我遇到类似的语法错误是因为我没有按照下面的行初始化子模块。但是 gce_pd 是一个核心模块,不是吗,所以应该已经可用了吧?

git submodule update --init lib/ansible/modules/core

这是我正在尝试的剧本 运行:

- name: Create Compute Engine instances
  hosts: local
  gather_facts: no
  vars:
    names: www1,www2,www3
    machine_type: n1-standard-1
    image: debian-7
    zone: europe-west1-d
    pid: <PID>
    email: <EMAIL>
    pem: <PEM>
  tasks:
    - name: Launch instances
      local_action: gce instance_names="{{ names }}"
                    machine_type="{{ machine_type }}"
                    image="{{ image }}" zone="{{ zone }}"
                    project_id="{{ pid }}" pem_file="{{ pem }}"
                    service_account_email="{{ email }}"
      register: gce
    - name: Wait for SSH to come up
      local_action: wait_for host="{{ item.public_ip }}" port=22 delay=10
                    timeout=60 state=started
      with_items: gce.instance_data
      - local_action:
            module: gce_pd
            instance_name: www2
            size_gb: 20
            name: www2-pd

我也试过将相关部分改成这个:

- name: Add a persistent disk to www2
  local_action:
        module: gce_pd
        instance_name: www2
        size_gb: 20
        name: www2-pd

谁能告诉我我做错了什么?

好的,我发现问题是由 yaml 中的选项卡引起的。

当我将 gce_pd 部分中缩进最多的行之前的制表符替换为空格时,错误不再发生。

我想,出于解释 yaml 的目的,制表符算作一个空白字符。这意味着它实际上相对于 local_action 行缩进不够(编辑:实际上在 yaml 中禁止制表符)。

github 上的 ansible-modules-core 项目中的这个问题也有助于确定语法应该是什么:https://github.com/ansible/ansible-modules-core/issues/977

我的剧本现在看起来像这样......而且有效:

- name: Create Compute Engine instances
  hosts: local
  gather_facts: no
  vars:
    names: www1,www2,www3
    machine_type: n1-standard-1
    image: debian-7
    zone: europe-west1-d
    pid: <PID>
    email: <EMAIL>
    pem: <PEM>
  tasks:
    - name: Launch instances
      local_action: gce instance_names="{{ names }}"
                    machine_type="{{ machine_type }}"
                    image="{{ image }}" zone="{{ zone }}"
                    project_id="{{ pid }}" pem_file="{{ pem }}"
                    service_account_email="{{ email }}"
      register: gce
    - name: Wait for SSH to come up
      local_action: wait_for host="{{ item.public_ip }}" port=22 delay=10
                    timeout=60 state=started
      with_items: gce.instance_data

    - local_action:
        module: gce_pd
        instance_name: "{{ item.name }}"
        project_id: "{{ pid }}"
        pem_file: "{{ pem }}"
        service_account_email: "{{ email }}"
        zone: "{{ zone }}"
        size_gb: 20
        mode: READ_WRITE
        name: "{{ item.name }}-disk"
      with_items: gce.instance_data