我遇到了最愚蠢的问题。我无法弄清楚如何在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
}
期望的结果是它会跳过包含。
当destroy
是true
时运行任务:
---
- hosts: localhost
connection: local
vars:
destroy: true
tasks:
- debug:
when: destroy
当destroy
是false
时:
---
- hosts: localhost
connection: local
vars:
destroy: false
tasks:
- debug:
when: not destroy
当在hostvars下定义变量的值时,不需要使用bool
Jinja filter。
要将值转换为某些类型,例如从vars_prompt输入字符串为“True”并且系统不知道它是布尔值。
这么简单
when: not destroy
应该做的伎俩。
包含在发生之前一直发生。
所以我只是制作了包含动态。
---- defaults/main.yml
mode: "create"
---- tasks/main.yml
- include: "{{ mode + '.yml' }}"