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

时间:2015-06-16 10:02:05

标签: google-compute-engine ansible ansible-playbook

我是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

有谁能让我直截了当地说出我在这里做错了什么?

1 个答案:

答案 0 :(得分:3)

好的,我发现问题是由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