mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 06:12:51 +00:00
Custom jinja Undefined class for handling nested undefined attributes (#51768)
This commit creates a custom Jinja2 Undefined class that returns Undefined for any further accesses, rather than raising an exception
This commit is contained in:
committed by
Sam Doran
parent
4e0e09d2de
commit
9c35f18dd6
@@ -642,5 +642,30 @@
|
||||
register: encoding_1252_diff_result
|
||||
loop: '{{ template_encoding_1252_encodings }}'
|
||||
|
||||
- name: Check that nested undefined values return Undefined
|
||||
vars:
|
||||
dict_var:
|
||||
bar: {}
|
||||
list_var:
|
||||
- foo: {}
|
||||
assert:
|
||||
that:
|
||||
- dict_var is defined
|
||||
- dict_var.bar is defined
|
||||
- dict_var.bar.baz is not defined
|
||||
- dict_var.bar.baz | default('DEFAULT') == 'DEFAULT'
|
||||
- dict_var.bar.baz.abc is not defined
|
||||
- dict_var.bar.baz.abc | default('DEFAULT') == 'DEFAULT'
|
||||
- dict_var.baz is not defined
|
||||
- dict_var.baz.abc is not defined
|
||||
- dict_var.baz.abc | default('DEFAULT') == 'DEFAULT'
|
||||
- list_var.0 is defined
|
||||
- list_var.1 is not defined
|
||||
- list_var.0.foo is defined
|
||||
- list_var.0.foo.bar is not defined
|
||||
- list_var.0.foo.bar | default('DEFAULT') == 'DEFAULT'
|
||||
- list_var.1.foo is not defined
|
||||
- list_var.1.foo | default('DEFAULT') == 'DEFAULT'
|
||||
|
||||
# aliases file requires root for template tests so this should be safe
|
||||
- include: backup_test.yml
|
||||
|
||||
Reference in New Issue
Block a user