强制Ansible抛出错误,当在yml缺less的variables?

是否有可能迫使Ansible当replace在未定义的YML文件中的变种,抛出一个错误,而不是默默地replace为空string?

是的,这是可能的。 在访问复杂的variables数据下检查联机文档。

提供一个例子来做到这一点:

tasks: - shell: echo "I've got '{{ foo }}' and am not afraid to use it!" when: foo is defined - fail: msg="Bailing out: this play requires 'bar'" when: bar is not defined 

将此行添加到ansible.cfg的[defaults]部分:

 error_on_undefined_vars = True 

如果一个variables未定义,您现在将得到一个错误消息。

在中定义你的variables

 roles/<role_name>/defaults/main.yml 

喜欢:

 SUPERVAR: VAR1:foo VAR2:bar 

然后做

 roles/<role_name>/tasks/main.yml 

喜欢:

 - fail: msg="{{ item }} is not defined" when: not {{ item }} with_items: - SUPERVAR.VAR1 - SUPERVAR.VAR2