mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Introduce exception AnsibleFilterError and use it in Jinja filters. Ignore this exception when trying to find the name of a task for display (callback) purpose.
This commit is contained in:
parent
b2d881a899
commit
2d88c70cd5
4 changed files with 43 additions and 34 deletions
|
@ -35,3 +35,6 @@ class AnsibleYAMLValidationFailed(AnsibleError):
|
||||||
|
|
||||||
class AnsibleUndefinedVariable(AnsibleError):
|
class AnsibleUndefinedVariable(AnsibleError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class AnsibleFilterError(AnsibleError):
|
||||||
|
pass
|
||||||
|
|
|
@ -326,7 +326,7 @@ class PlayBook(object):
|
||||||
ansible.callbacks.set_task(self.callbacks, task)
|
ansible.callbacks.set_task(self.callbacks, task)
|
||||||
ansible.callbacks.set_task(self.runner_callbacks, task)
|
ansible.callbacks.set_task(self.runner_callbacks, task)
|
||||||
|
|
||||||
self.callbacks.on_task_start(template(play.basedir, task.name, task.module_vars, lookup_fatal=False), is_handler)
|
self.callbacks.on_task_start(template(play.basedir, task.name, task.module_vars, lookup_fatal=False, filter_fatal=False), is_handler)
|
||||||
if hasattr(self.callbacks, 'skip_task') and self.callbacks.skip_task:
|
if hasattr(self.callbacks, 'skip_task') and self.callbacks.skip_task:
|
||||||
ansible.callbacks.set_task(self.callbacks, None)
|
ansible.callbacks.set_task(self.callbacks, None)
|
||||||
ansible.callbacks.set_task(self.runner_callbacks, None)
|
ansible.callbacks.set_task(self.runner_callbacks, None)
|
||||||
|
|
|
@ -33,7 +33,7 @@ def to_nice_json(*a, **kw):
|
||||||
def failed(*a, **kw):
|
def failed(*a, **kw):
|
||||||
item = a[0]
|
item = a[0]
|
||||||
if type(item) != dict:
|
if type(item) != dict:
|
||||||
raise errors.AnsibleError("|failed expects a dictionary")
|
raise errors.AnsibleFilterError("|failed expects a dictionary")
|
||||||
rc = item.get('rc',0)
|
rc = item.get('rc',0)
|
||||||
failed = item.get('failed',False)
|
failed = item.get('failed',False)
|
||||||
if rc != 0 or failed:
|
if rc != 0 or failed:
|
||||||
|
|
|
@ -295,42 +295,48 @@ def legacy_varReplace(basedir, raw, vars, lookup_fatal=True, depth=0, expand_lis
|
||||||
|
|
||||||
# TODO: varname is misnamed here
|
# TODO: varname is misnamed here
|
||||||
|
|
||||||
def template(basedir, varname, vars, lookup_fatal=True, depth=0, expand_lists=True, convert_bare=False, fail_on_undefined=False):
|
def template(basedir, varname, vars, lookup_fatal=True, depth=0, expand_lists=True, convert_bare=False, fail_on_undefined=False, filter_fatal=True):
|
||||||
''' templates a data structure by traversing it and substituting for other data structures '''
|
''' templates a data structure by traversing it and substituting for other data structures '''
|
||||||
|
|
||||||
if convert_bare and isinstance(varname, basestring):
|
try:
|
||||||
first_part = varname.split(".")[0].split("[")[0]
|
if convert_bare and isinstance(varname, basestring):
|
||||||
if first_part in vars and '{{' not in varname and '$' not in varname:
|
first_part = varname.split(".")[0].split("[")[0]
|
||||||
varname = "{{%s}}" % varname
|
if first_part in vars and '{{' not in varname and '$' not in varname:
|
||||||
|
varname = "{{%s}}" % varname
|
||||||
|
|
||||||
if isinstance(varname, basestring):
|
if isinstance(varname, basestring):
|
||||||
if '{{' in varname or '{%' in varname:
|
if '{{' in varname or '{%' in varname:
|
||||||
varname = template_from_string(basedir, varname, vars, fail_on_undefined)
|
varname = template_from_string(basedir, varname, vars, fail_on_undefined)
|
||||||
if not '$' in varname:
|
if not '$' in varname:
|
||||||
return varname
|
|
||||||
|
|
||||||
m = _legacy_varFind(basedir, varname, vars, lookup_fatal, depth, expand_lists)
|
|
||||||
if not m:
|
|
||||||
return varname
|
|
||||||
if m['start'] == 0 and m['end'] == len(varname):
|
|
||||||
if m['replacement'] is not None:
|
|
||||||
Flags.LEGACY_TEMPLATE_WARNING = True
|
|
||||||
return template(basedir, m['replacement'], vars, lookup_fatal, depth, expand_lists)
|
|
||||||
else:
|
|
||||||
return varname
|
return varname
|
||||||
else:
|
|
||||||
Flags.LEGACY_TEMPLATE_WARNING = True
|
|
||||||
return legacy_varReplace(basedir, varname, vars, lookup_fatal, depth, expand_lists)
|
|
||||||
|
|
||||||
elif isinstance(varname, (list, tuple)):
|
m = _legacy_varFind(basedir, varname, vars, lookup_fatal, depth, expand_lists)
|
||||||
return [template(basedir, v, vars, lookup_fatal, depth, expand_lists) for v in varname]
|
if not m:
|
||||||
elif isinstance(varname, dict):
|
return varname
|
||||||
d = {}
|
if m['start'] == 0 and m['end'] == len(varname):
|
||||||
for (k, v) in varname.iteritems():
|
if m['replacement'] is not None:
|
||||||
d[k] = template(basedir, v, vars, lookup_fatal, depth, expand_lists)
|
Flags.LEGACY_TEMPLATE_WARNING = True
|
||||||
return d
|
return template(basedir, m['replacement'], vars, lookup_fatal, depth, expand_lists)
|
||||||
else:
|
else:
|
||||||
return varname
|
return varname
|
||||||
|
else:
|
||||||
|
Flags.LEGACY_TEMPLATE_WARNING = True
|
||||||
|
return legacy_varReplace(basedir, varname, vars, lookup_fatal, depth, expand_lists)
|
||||||
|
|
||||||
|
elif isinstance(varname, (list, tuple)):
|
||||||
|
return [template(basedir, v, vars, lookup_fatal, depth, expand_lists) for v in varname]
|
||||||
|
elif isinstance(varname, dict):
|
||||||
|
d = {}
|
||||||
|
for (k, v) in varname.iteritems():
|
||||||
|
d[k] = template(basedir, v, vars, lookup_fatal, depth, expand_lists)
|
||||||
|
return d
|
||||||
|
else:
|
||||||
|
return varname
|
||||||
|
except errors.AnsibleFilterError:
|
||||||
|
if filter_fatal:
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
return varname
|
||||||
|
|
||||||
|
|
||||||
class _jinja2_vars(object):
|
class _jinja2_vars(object):
|
||||||
|
|
Loading…
Reference in a new issue