Fixing item loop when undefined variable errors occur because of missing attributes

Fixes a case where the variable 'foo' may exist, but the with_items loop
was used on something like 'foo.results', where 'results' was not a valid
attribute of 'foo'. Prior to this patch, conditionals were not evaluated
until later, meaning there was no opportunity to allow a test to skip the
task or item based on it being undefined.
This commit is contained in:
James Cammarata 2014-10-10 12:23:58 -05:00
commit 2eda9a3a47
3 changed files with 56 additions and 3 deletions

View file

@ -663,9 +663,24 @@ class Runner(object):
if os.path.exists(filesdir):
basedir = filesdir
items_terms = self.module_vars.get('items_lookup_terms', '')
items_terms = template.template(basedir, items_terms, inject)
items = utils.plugins.lookup_loader.get(items_plugin, runner=self, basedir=basedir).run(items_terms, inject=inject)
try:
items_terms = self.module_vars.get('items_lookup_terms', '')
items_terms = template.template(basedir, items_terms, inject)
items = utils.plugins.lookup_loader.get(items_plugin, runner=self, basedir=basedir).run(items_terms, inject=inject)
except errors.AnsibleUndefinedVariable, e:
if 'has no attribute' in str(e):
# the undefined variable was an attribute of a variable that does
# exist, so try and run this through the conditional check to see
# if the user wanted to skip something on being undefined
if utils.check_conditional(self.conditional, self.basedir, inject, fail_on_undefined=True):
# the conditional check passed, so we have to fail here
raise
else:
# the conditional failed, so we skip this task
result = utils.jsonify(dict(changed=False, skipped=True))
self.callbacks.on_skipped(host, None)
return ReturnData(host=host, result=result)
# strip out any jinja2 template syntax within
# the data returned by the lookup plugin
items = utils._clean_data_struct(items, from_remote=True)