在 Ansible When 子句中使用 True False

Using True False with Ansible When Clause

我运行遇到了最愚蠢的问题。 我不知道如何在 Ansible 2.2 任务文件中测试布尔值。

vars/main.yml中,我有:

destroy: false

在剧本中,我有:

roles: 
  - {'role': 'vmdeploy','destroy': true}

在任务文件中,我有以下内容:

- include: "create.yml"
  when: "{{ destroy|bool }} == 'false'"

我尝试了以下各种组合:

when: "{{ destroy|bool }} == false"
when: "{{ destroy|bool }} == 'false'"
when: "{{ destroy|bool  == false}}"
when: "{{ destroy  == false}}"
when: "{{ destroy  == 'false'}}"
when: destroy|bool  == false
when: destroy|bool  == 'false'
when: not destroy|bool

在上述所有情况下,我仍然得到:

statically included: .../vmdeploy/tasks/create.yml

调试输出:

- debug:
    msg: "{{ destroy }}"

---

ok: [atlcicd009] => {
"msg": true
}

期望的结果是它会跳过包含。

如果变量的值定义在hostvars.

下,则不需要使用boolJinja filter

To cast values as certain types, such as when you input a string as “True” from a vars_prompt and the system doesn’t know it is a boolean value.

好简单

when: not destroy

应该可以解决问题。

destroytrue 时 运行 任务:

---
- hosts: localhost
  connection: local
  vars:
    destroy: true
  tasks:
    - debug:
      when: destroy

并且当 destroyfalse 时:

---
- hosts: localhost
  connection: local
  vars:
    destroy: false
  tasks:
    - debug:
      when: not destroy

包含在什么时候之前一直发生。

所以我只是使包含动态化。

---- defaults/main.yml
mode: "create"

---- tasks/main.yml
- include: "{{ mode + '.yml' }}"