有没有解释为什么两个逻辑推理debug
下面分别输出在Ansible剧本任务"NONE"
,并"FALSE"
和不同时"NONE"
?
- hosts: 'all'
tasks:
- debug:
msg: '{{ foo | default( None ) | ternary( "TRUE", "FALSE", "NONE" ) }}'
- debug:
msg: '{{ bar | ternary( "TRUE", "FALSE", "NONE" ) }}'
vars:
bar: '{{ foo | default( None ) }}'
我用[email protected]
btw观察到了
[edit @ 2019-10-16]
从@VladimirBotka的答案可以推断,一个人可以说真正的“问题”是None
在替代方面不是可传递的。我对的使用default(...)
不必要地使事情复杂化,并且示例可以简化为:
- hosts: 'all'
tasks:
- debug:
msg: '{{ None | ternary( "TRUE", "FALSE", "NONE" ) }}'
- debug:
msg: '{{ bar | ternary( "TRUE", "FALSE", "NONE" ) }}'
vars:
bar: '{{ None }}'
...并仍然产生完全相同的输出。
foo|default(None)
评估为 null
{{ foo|default(None) }}
计算为空字符串 ""
任务
- debug:
msg: bar is empty string
when: bar|length == 0
给
"msg": "bar is empty string"
但是任务
- debug:
msg: foo is empty string
when: foo|default(None)|length == 0
失败并显示以下错误:
"... object of type 'NoneType' has no len() ... "
作为三元运算的结果,一个空字符串将选择FALSE,而null将选择NONE。
笔记
1)请参见省略参数中的注释
2)如果省略了第三个参数,则null
空字符串都将在三进制中""
选择FALSE。任务
- debug:
msg: "{{ bar|ternary('TRUE', 'FALSE') }}"
- debug:
msg: "{{ foo|default(None)|ternary('TRUE', 'FALSE') }}"
给
"msg": "FALSE"
"msg": "FALSE"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句