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

@ -267,3 +267,28 @@
that:
- "result.changed"
- name: test a with_items loop using a variable with a missing attribute
debug: var=item
with_items: foo.results
when: foo is defined and 'results' in foo
register: result
- name: assert the task was skipped
assert:
that:
- "'skipped' in result"
- result.skipped
- name: test a with_items loop skipping a single item
debug: var=item
with_items: items.results
when: item != 'b'
register: result
- debug: var=result
- name: assert only a single item was skipped
assert:
that:
- result.results|length == 3
- result.results[1].skipped

View file

@ -0,0 +1,13 @@
---
# foo is a dictionary that will be used to check that
# a conditional passes a with_items loop on a variable
# with a missing attribute (ie. foo.results)
foo:
bar: a
items:
results:
- a
- b
- c