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

Rya*_*yan 8 ansible

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

daw*_*wud 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
Run Code Online (Sandbox Code Playgroud)


Lor*_*ein 8

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

error_on_undefined_vars = True
Run Code Online (Sandbox Code Playgroud)

如果变量未定义,您现在将收到一条错误消息。