Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend jinja2 nested undefined support to keys/indices #55094

Merged
merged 1 commit into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/jinja2_nested_undefined_getitem.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- jinja2 - accesses to keys/indices on an undefined value now return further undefined values rather than throwing an exception
4 changes: 4 additions & 0 deletions lib/ansible/template/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ def __getattr__(self, name):
# Return original Undefined object to preserve the first failure context
return self

def __getitem__(self, key):
# Return original Undefined object to preserve the first failure context
return self

def __repr__(self):
return 'AnsibleUndefined'

Expand Down
18 changes: 18 additions & 0 deletions test/integration/targets/template/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,24 @@
- list_var.0.foo.bar | default('DEFAULT') == 'DEFAULT'
- list_var.1.foo is not defined
- list_var.1.foo | default('DEFAULT') == 'DEFAULT'
- 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'
- dict_var['bar'].baz is not defined
- dict_var['bar'].baz | default('DEFAULT') == 'DEFAULT'

- template:
src: template_destpath_test.j2
Expand Down