【问题标题】:How to check if multiple ansible variables are defined without warnings?如何检查是否定义了多个ansible变量而没有警告?
【发布时间】:2017-11-24 21:59:00
【问题描述】:

我在角色开始时有一个任务:

- name: check if pg_* variables are defined
  fail: msg="variable {{item}} is undefined"
  when: "{{item}} is undefined"
  with_items:
    - pg_dbname
    - pg_host
    - pg_port
    - pg_username
    - pg_password

但较新版本的 Ansible 会发出以下警告:

[警告]:when 语句不应包含 jinja2 模板分隔符,例如 {{ }} 或 {% %}。发现:{{item}} 未定义

删除 Jinja2 大括号 when: item is undefined 不起作用,因为最终评估基本上等同于:

"pg_dbname" is undefined` # False
"pg_host" is undefined` # False
.
.
"pg_password" is undefined` # False

这不是我想要的。

【问题讨论】:

    标签: ansible jinja2


    【解决方案1】:

    名为item 的变量总是with_items 循环内定义,并且您分配给它的所有五个字符串都应具有skipped 状态。


    您要检查实际变量是否存在:

    when: vars[item] is undefined
    

    【讨论】:

    • 我知道必须有一个全局字典,我可以使用item 中的字符串访问它。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2010-09-22
    • 2017-01-09
    • 1970-01-01
    • 2015-05-01
    • 2017-07-12
    • 2012-10-07
    • 2021-04-21
    • 2021-10-03
    相关资源
    最近更新 更多