Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure we only cache the loop when the task had a loop #44901

Merged
merged 2 commits into from
Aug 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/loop-cache-fix.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- loop - Ensure we only cache the loop when the task had a loop and delegate_to was templated (https://github.com/ansible/ansible/issues/44874)
4 changes: 3 additions & 1 deletion lib/ansible/vars/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ def _get_delegated_vars(self, play, task, existing_variables):
templar = Templar(loader=self._loader, variables=vars_copy)

items = []
has_loop = True
if task.loop_with is not None:
if task.loop_with in lookup_loader:
try:
Expand All @@ -509,6 +510,7 @@ def _get_delegated_vars(self, play, task, existing_variables):
elif task.loop is not None:
items = templar.template(task.loop)
else:
has_loop = False
items = [None]

delegated_host_vars = dict()
Expand Down Expand Up @@ -583,7 +585,7 @@ def _get_delegated_vars(self, play, task, existing_variables):
include_hostvars=False,
)

if cache_items:
if has_loop and cache_items:
# delegate_to templating produced a change, update task.loop with templated items,
# this ensures that delegate_to+loop doesn't produce different results than TaskExecutor
# which may reprocess the loop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
add_host:
name: "foo{{item}}"
groups: foo
ansible_connection: local
loop: "{{ range(10)|list }}"

# We expect all of the next 3 runs to succeeed
Expand Down Expand Up @@ -56,3 +57,16 @@
- "{{ (result1.results|first) is successful }}"
- "{{ (result2.results|first) is successful }}"
- "{{ (result3.results|first) is successful }}"

- name: Set delegate
set_fact:
_delegate: '{{ groups.foo[0] }}'

- command: "true"
delegate_to: "{{ _delegate }}"
register: result

- assert:
that:
- result.stdout is defined
- result.results is undefined