包含具有指定变量的任务(Ansible)

时间:2015-02-26 17:40:50

标签: ansible ansible-playbook

我刚开始学习安莎。我已经设置了一个测试存储库here

我试图包含一项任务,并将一条消息作为一个应该打印的变量传递给我们。通过任务。我的playbook(site.yml)的结构如下:

- name:  default playbook
  tasks:
    - { include: tasks/timestamp.yml, themsg='starting tasks' }

tasks/timestamp.yml是:

---
- debug: msg="{{ themsg }} ' @ ' {{ ansible_date_time['time'] }}."

当我使用ansible-playbook site.yml运行时,我收到错误:

TASK: [debug msg="{{ themsg }} 'current time:' {{     ansible_date_time['time'] }}."] *** fatal: [localhost] => One or more undefined variables: 'themsg' is undefined

我一直在使用official documentation作为参考,看起来这应该可行。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

你正在做一些略微不同寻常的事情(包括任务,而不是使用角色,来自剧本),并且被一些常见的语法错误所困扰。

您找到了正确的文档;它包含一个任务文件:

- include: wordpress.yml wp_user=timmy
- { include: wordpress.yml, wp_user: timmy, ssh_keys: [ 'keys/one.txt', 'keys/two.txt' ] }

不幸的是,你将这些结合起来:

- { include: tasks/timestamp.yml, themsg='starting tasks' }

这是如何摆脱果酱。这些都可以起作用:

- { include: tasks/timestamp.yml, themsg: 'starting tasks' }
- include: tasks/timestamp.yml themsg='starting tasks'

在第一个例子中,我将=更改为冒号。在第二个例子中,我删除了大括号并删除了逗号。

将“任务包含”升级为“角色包含”非常容易。将tasks/timestamp.yml移至roles/timestamp/tasks/main.yml,然后将其作为角色而不是任务包含在内:

roles:
  - { role: timestamp, themsg: "starting tasks" }

你获得了一些理智 - 一些可移植性,与先决条件和处理程序挂钩等。