Provide more information in AnsibleUndefinedVariable (#71666) (#71876)

* Provide more information in AnsibleUndefinedVariable

Fixes #55152

(cherry picked from commit 00b22ab55e)
This commit is contained in:
Martin Krizek 2020-09-28 07:34:38 +02:00 committed by GitHub
parent c4e3552c8d
commit 4df129c6ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 2 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- Provide more information in AnsibleUndefinedVariable (https://github.com/ansible/ansible/issues/55152)

View file

@ -104,8 +104,8 @@ class AnsibleJ2Vars(Mapping):
value = None value = None
try: try:
value = self._templar.template(variable) value = self._templar.template(variable)
except AnsibleUndefinedVariable: except AnsibleUndefinedVariable as e:
raise raise AnsibleUndefinedVariable("%s: %s" % (to_native(variable), e.message))
except Exception as e: except Exception as e:
msg = getattr(e, 'message', None) or to_native(e) msg = getattr(e, 'message', None) or to_native(e)
raise AnsibleError("An unhandled exception occurred while templating '%s'. " raise AnsibleError("An unhandled exception occurred while templating '%s'. "

View file

@ -22,3 +22,6 @@ ansible-playbook filter_plugins.yml -v "$@"
# https://github.com/ansible/ansible/issues/68699 # https://github.com/ansible/ansible/issues/68699
ansible-playbook unused_vars_include.yml -v "$@" ansible-playbook unused_vars_include.yml -v "$@"
# https://github.com/ansible/ansible/issues/55152
ansible-playbook undefined_var_info.yml -v "$@"

View file

@ -0,0 +1,15 @@
- hosts: localhost
gather_facts: no
vars:
foo: []
bar: "{{ foo[0] }}"
tasks:
- debug:
msg: "{{ bar }}"
register: result
ignore_errors: yes
- assert:
that:
- '"foo[0]" in result.msg'
- '"object has no element 0" in result.msg'