Ansible 任务可以有when这样的子句:
- name: Conditional output
debug:
msg: This is a conditional output
when: some_var
Run Code Online (Sandbox Code Playgroud)
为了防止some_var未定义,可以使用
- name: Conditional output
debug:
msg: This is a conditional output
when: some_var is defined and some_var
Run Code Online (Sandbox Code Playgroud)
似乎还有这样的变体:
- name: Conditional output
debug:
msg: This is a conditional output
when: some_var | d() | bool
Run Code Online (Sandbox Code Playgroud)
这些变体相同吗?它们的优点/缺点是什么?
您的两个安全示例的严格等效项是:
when: some_var is defined and some_var | bool
Run Code Online (Sandbox Code Playgroud)
与
when: some_var | d() | bool
Run Code Online (Sandbox Code Playgroud)
过滤bool器确保 var 内容被解释为布尔值,并且根据我的经验,a 的字符串答案vars_prompt按预期工作("true"=> true)。
d是过滤器的别名,default并记录在jinja2 官方文档中
上述 2 种情况严格等效,并且在所有情况下都会产生相同的结果。但我确实更喜欢最紧凑的一个,出于文档原因,我会进一步增强它,如下所示:
when: some_var | d(false) | bool
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
545 次 |
| 最近记录: |