Add new AnsibleTemplateError to more easily catch templating issues (#50563)

* Add new AnsibleTemplateError to more easily catch templating issues. Fixes #50154

* Add changelog fragment
pull/50416/head
Matt Martz 6 years ago committed by GitHub
parent 96f7cf394f
commit 9abeecb6d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,3 @@
bugfixes:
- Add new ``AnsibleTemplateError`` that various templating related exceptions inherit from,
making it easier to catch them without enumerating. (https://github.com/ansible/ansible/issues/50154)

@ -223,22 +223,27 @@ class AnsibleConnectionFailure(AnsibleRuntimeError):
pass pass
class AnsibleFilterError(AnsibleRuntimeError): class AnsibleCallbackError(AnsibleRuntimeError):
''' a templating failure ''' ''' a callback failure '''
pass pass
class AnsibleLookupError(AnsibleRuntimeError): class AnsibleTemplateError(AnsibleRuntimeError):
''' a lookup failure ''' '''A template related errror'''
pass pass
class AnsibleCallbackError(AnsibleRuntimeError): class AnsibleFilterError(AnsibleTemplateError):
''' a callback failure ''' ''' a templating failure '''
pass
class AnsibleLookupError(AnsibleTemplateError):
''' a lookup failure '''
pass pass
class AnsibleUndefinedVariable(AnsibleRuntimeError): class AnsibleUndefinedVariable(AnsibleTemplateError):
''' a templating failure ''' ''' a templating failure '''
pass pass

@ -32,7 +32,7 @@ except ImportError:
from jinja2.exceptions import UndefinedError from jinja2.exceptions import UndefinedError
from ansible import constants as C from ansible import constants as C
from ansible.errors import AnsibleError, AnsibleParserError, AnsibleUndefinedVariable, AnsibleFileNotFound, AnsibleAssertionError from ansible.errors import AnsibleError, AnsibleParserError, AnsibleUndefinedVariable, AnsibleFileNotFound, AnsibleAssertionError, AnsibleTemplateError
from ansible.inventory.host import Host from ansible.inventory.host import Host
from ansible.inventory.helpers import sort_groups, get_group_vars from ansible.inventory.helpers import sort_groups, get_group_vars
from ansible.module_utils._text import to_native from ansible.module_utils._text import to_native
@ -519,7 +519,7 @@ class VariableManager:
loop_terms = listify_lookup_plugin_terms(terms=task.loop, templar=self._templar, loop_terms = listify_lookup_plugin_terms(terms=task.loop, templar=self._templar,
loader=self._loader, fail_on_undefined=True, convert_bare=False) loader=self._loader, fail_on_undefined=True, convert_bare=False)
items = lookup_loader.get(task.loop_with, loader=self._loader, templar=self._templar).run(terms=loop_terms, variables=vars_copy) items = lookup_loader.get(task.loop_with, loader=self._loader, templar=self._templar).run(terms=loop_terms, variables=vars_copy)
except AnsibleUndefinedVariable: except AnsibleTemplateError:
# This task will be skipped later due to this, so we just setup # This task will be skipped later due to this, so we just setup
# a dummy array for the later code so it doesn't fail # a dummy array for the later code so it doesn't fail
items = [None] items = [None]
@ -528,7 +528,7 @@ class VariableManager:
elif task.loop is not None: elif task.loop is not None:
try: try:
items = self._templar.template(task.loop) items = self._templar.template(task.loop)
except AnsibleUndefinedVariable: except AnsibleTemplateError:
# This task will be skipped later due to this, so we just setup # This task will be skipped later due to this, so we just setup
# a dummy array for the later code so it doesn't fail # a dummy array for the later code so it doesn't fail
items = [None] items = [None]

Loading…
Cancel
Save