From d78da403b64418a81035dfb2592d8fa24f533002 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Thu, 12 Apr 2018 09:35:00 -0400 Subject: [PATCH] allow user control of lookup error behaviour (#35932) * 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 --- lib/ansible/template/__init__.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index 215a191f23..4568a2b2f6 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -616,6 +616,7 @@ class Templar: 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) @@ -626,8 +627,14 @@ class Templar: 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: