Skip to content

Commit

Permalink
allow user control of lookup error behaviour (ansible#35932)
Browse files Browse the repository at this point in the history
* allow user control of lookup error behaviour

this does not affect undefined vars, only other exceptions raised by a lookup

i.e lookup('file' ..) not finding a file
  • Loading branch information
bcoca authored Apr 12, 2018
1 parent 9de9633 commit d78da40
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/ansible/template/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ def _lookup(self, name, *args, **kwargs):
if instance is not None:
wantlist = kwargs.pop('wantlist', False)
allow_unsafe = kwargs.pop('allow_unsafe', C.DEFAULT_ALLOW_UNSAFE_LOOKUPS)
errors = kwargs.pop('errors', 'strict')

from ansible.utils.listify import listify_lookup_plugin_terms
loop_terms = listify_lookup_plugin_terms(terms=args, templar=self, loader=self._loader, fail_on_undefined=True, convert_bare=False)
Expand All @@ -626,8 +627,14 @@ def _lookup(self, name, *args, **kwargs):
raise AnsibleUndefinedVariable(e)
except Exception as e:
if self._fail_on_lookup_errors:
raise AnsibleError("An unhandled exception occurred while running the lookup plugin '%s'. Error was a %s, "
"original message: %s" % (name, type(e), e))
msg = u"An unhandled exception occurred while running the lookup plugin '%s'. Error was a %s, original message: %s" % \
(name, type(e), to_text(e))
if errors == 'warn':
display.warning(msg)
elif errors == 'ignore':
display.log(msg)
else:
raise AnsibleError(to_native(msg))
ran = None

if ran and not allow_unsafe:
Expand Down

0 comments on commit d78da40

Please sign in to comment.