当yml中缺少vars时,强制Ansible抛出错误吗?


8

替换未定义的yml文件中的var时,是否可以强制Ansible,抛出错误而不是用空字符串静默替换?

Answers:


9

是的,有可能。在访问复杂变量数据下,查看在线文档。

提供了一个示例来做到这一点:

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


1

在以下位置定义变量

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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.