Sim*_*eth 23 boolean ansible ansible-playbook ansible-2.x
我遇到了最愚蠢的问题.我无法弄清楚如何在Ansible 2.2任务文件中测试布尔值.
在vars/main.yml,我有:
destroy: false
Run Code Online (Sandbox Code Playgroud)
在剧本中,我有:
roles:
- {'role': 'vmdeploy','destroy': true}
Run Code Online (Sandbox Code Playgroud)
在任务文件中,我有以下内容:
- include: "create.yml"
when: "{{ destroy|bool }} == 'false'"
Run Code Online (Sandbox Code Playgroud)
我尝试了以下各种组合:
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
Run Code Online (Sandbox Code Playgroud)
在以上所有情况下,我仍然得到:
statically included: .../vmdeploy/tasks/create.yml
Run Code Online (Sandbox Code Playgroud)
调试输出:
- debug:
msg: "{{ destroy }}"
---
ok: [atlcicd009] => {
"msg": true
}
Run Code Online (Sandbox Code Playgroud)
期望的结果是它会跳过包含.
tec*_*raf 39
要在运行任务destroy是true:
---
- hosts: localhost
connection: local
vars:
destroy: true
tasks:
- debug:
when: destroy
Run Code Online (Sandbox Code Playgroud)
当destroy是false:
---
- hosts: localhost
connection: local
vars:
destroy: false
tasks:
- debug:
when: not destroy
Run Code Online (Sandbox Code Playgroud)
Hen*_*ica 13
当在hostvars下定义变量的值时,不需要使用bool Jinja过滤器.
要将值转换为某些类型,例如从vars_prompt输入字符串为"True"并且系统不知道它是布尔值.
这么简单
when: not destroy
Run Code Online (Sandbox Code Playgroud)
应该诀窍.
小智 7
我也一直在这个问题上挣扎。我的问题是,该变量没有在脚本内声明,而是作为额外参数给出。在这种情况下,您需要显式转换为 bool
when: destroy|bool
Run Code Online (Sandbox Code Playgroud)
或者
when: not destroy|bool
Run Code Online (Sandbox Code Playgroud)
Sim*_*eth -12
包含在该时间之前不断发生。
所以我只是将包含动态化。
---- defaults/main.yml
mode: "create"
---- tasks/main.yml
- include: "{{ mode + '.yml' }}"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53944 次 |
| 最近记录: |