如何做Ansible条件include_vars?

时间:2016-03-22 14:23:29

标签: automation ansible ansible-2.x

我正在尝试使用基于include_vars条件的when,如下所示:

- include_vars: clouderamanager.yml
  when: '{{ inventory_hostname }}' in groups['hadoop-clouderamanager']

- include_vars: hadoop-namenode.yml
  when: '{{ inventory_hostname }}' in groups['hadoop-namenode']

- include_vars: resourcemanager.yml
  when: '{{ inventory_hostname }}' in groups['hadoop-resourcemanager']

- include_vars: spark-history-server.yml
  when: '{{ inventory_hostname }}' in groups['spark-history-server']

我正在ERROR! Syntax Error while loading YAML.

The error appears to have been in '~/ansible/roles/hadoop-master/tasks/main.yml': line 4, column 36, but may be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- include_vars: clouderamanager.yml
  when: '{{ inventory_hostname }}' in groups['hadoop-clouderamanager']
                                   ^ here

2 个答案:

答案 0 :(得分:1)

当条件有效时改变

when: inventory_hostname in groups['hadoop-clouderamanager']

答案 1 :(得分:0)

如果一个值开始引用,则必须引用整个值直到结束或YAML解析器发生癫痫发作。

这意味着你必须定义这样的条件:

when: "'{{ inventory_hostname }}' in groups['hadoop-clouderamanager']"

不幸的是,Ansible 2中的错误消息并不像Ansible 1那样清晰,它实际上已经指出了这一点。

只是为了确保...你知道 group_vars ?您可以在group_vars/hadoop-clouderamanager中定义变量,而不需要自己包含变量,因为Ansible会自动加载所有匹配组的所有变量。