角色开始时我有一个任务:
- 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发出以下警告:
[警告]:当语句不应包含jinja2模板分隔符时,例如{{}}或{%%}。找到:{{item}}未定义
删除Jinja2括号when: item is undefined不起作用,因为最终评估结果基本上等同于:
"pg_dbname" is undefined` # False
"pg_host" is undefined` # False
.
.
"pg_password" is undefined` # False
这不是我想要的。
所谓变item则始终内部定义的with_items循环,你的任务应该skipped对所有五根弦的状态,你分配给它。
您要检查实际变量是否存在:
when: vars[item] is undefined