如何使用字典在ansible中实现默认值回退?

sor*_*rin 1 ansible

我想改进下面的代码,这样它总是会回退到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 不存在则失败。

Kon*_*rov 6

AFAIK 仅通过“嵌套” default

{{ ((foo|default({})).bar|default({}))[12] | default(omit) }}
Run Code Online (Sandbox Code Playgroud)