mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-25 14:20:22 -07: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:
parent
4e0e09d2de
commit
9c35f18dd6
4 changed files with 60 additions and 2 deletions
|
@ -37,6 +37,24 @@ using an import, a task can notify any of the named tasks within the imported fi
|
|||
|
||||
To achieve the results of notifying a single name but running mulitple handlers, utilize ``include_tasks``, or ``listen`` :ref:`handlers`.
|
||||
|
||||
Jinja Undefined values
|
||||
----------------------
|
||||
|
||||
Beginning in version 2.8, attempting to access an attribute of an Undefined value in Jinja will return another Undefined value, rather than throwing an error immediately. This means that you can now simply use
|
||||
a default with a value in a nested data structure when you don't know if the intermediate values are defined.
|
||||
|
||||
In Ansible 2.8::
|
||||
|
||||
{{ foo.bar.baz | default('DEFAULT') }}
|
||||
|
||||
In Ansible 2.7 and older::
|
||||
|
||||
{{ ((foo | default({})).bar | default({})).baz | default('DEFAULT') }}
|
||||
|
||||
or
|
||||
|
||||
{{ foo.bar.baz if (foo is defined and foo.bar is defined and foo.bar.baz is defined) else 'DEFAULT' }}
|
||||
|
||||
Command Line
|
||||
============
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue