mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
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:
parent
b4f84c5d9a
commit
2eda9a3a47
3 changed files with 56 additions and 3 deletions
|
@ -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
|
||||
|
|
13
test/integration/roles/test_conditionals/vars/main.yml
Normal file
13
test/integration/roles/test_conditionals/vars/main.yml
Normal 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
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue