且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ansible:检查变量列表的正确方法是否已设置?

更新时间:2023-11-25 23:44:28

使用 vars 结构:

- name: validate some variables
  fail:
    msg: "Required variable {{item}} has not been provided"
  when: vars[item] is undefined
  loop:
    - v1
    - v2

或者,在 Ansible 2.5 中,使用新的 vars 查找插件:

Or, in Ansible 2.5, with the new vars lookup plugin:

- name: validate some variables
  debug:
  when: lookup('vars', item) is undefined
  loop:
    - v1
    - v2

虽然不是您指定的错误消息,而是查找插件的默认错误消息.

Although not with the error message you specified, but a default error message for a lookup plugin.

模块甚至不会被执行,所以你可以使用任何我在上面的例子中用 debug 替换 fail 的任何东西.

The module won't even be executed, so you can use whatever ー I replaced fail with debug in the example above.