我刚接触过Ansible,但是我有一个工作的剧本,可以创建实例。为了实现这一点,我遵循了用木偶、主厨、盐和可耐药计算发动机管理-附录的Ansible部分。
现在,我想扩展剧本,将额外的持久磁盘添加到我创建的实例中,因此我遵循pd模块文档。然而,我遇到的问题是,文档中给出的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有人能告诉我我在这里做错了什么吗?
发布于 2015-06-16 13:10:15
好的,我发现这个问题是由yaml中的标签引起的。
当我用空格替换gce_pd部分中缩进最多的行之前存在的制表符时,错误不再发生。
我想,为了解释yaml,一个制表符算作一个空格字符。这意味着local_action行实际上缩进不够(yaml中禁止编辑:yaml选项卡)。
另外,github上的ansible模块核心项目中的这个问题也有助于确定语法应该是什么: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_datahttps://stackoverflow.com/questions/30864440
复制相似问题