我想改进下面的代码,这样它总是会回退到omit找不到值时。
#!/usr/bin/env ansible-playbook
---
- hosts: localhost
gather_facts: no
vars:
ansible_connection: local
foo:
bar:
12: 'xxx'
tasks:
- debug:
msg: "component_config={{ foo.bar[12] | default(omit) }}"
Run Code Online (Sandbox Code Playgroud)
当前代码仅当foo.bar是字典时才按预期工作,但如果 bar 甚至 foo 不存在则失败。
AFAIK 仅通过“嵌套” default:
{{ ((foo|default({})).bar|default({}))[12] | default(omit) }}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1921 次 |
| 最近记录: |