[stable-2.6] Ensure loop with delegate_to can short circuit the same as without delegate_to. Fixes #45189 (#45231).

(cherry picked from commit 2ac647def8)

Co-authored-by: Matt Martz <matt@sivel.net>
This commit is contained in:
Matt Martz 2018-09-07 10:16:03 -05:00 committed by Matt Clay
parent daac0514ef
commit e0e266d070
3 changed files with 29 additions and 1 deletions

View file

@ -0,0 +1,4 @@
bugfixes:
- loop - Ensure that a loop with a when condition that evaluates to false and delegate_to, will short circuit if the
loop references an undefined variable. This matches the behavior in the same scenario without delegate_to
(https://github.com/ansible/ansible/issues/45189)

View file

@ -500,7 +500,12 @@ class VariableManager:
else: else:
raise AnsibleError("Failed to find the lookup named '%s' in the available lookup plugins" % task.loop_with) raise AnsibleError("Failed to find the lookup named '%s' in the available lookup plugins" % task.loop_with)
elif task.loop is not None: elif task.loop is not None:
items = templar.template(task.loop) try:
items = templar.template(task.loop)
except AnsibleUndefinedVariable:
# This task will be skipped later due to this, so we just setup
# a dummy array for the later code so it doesn't fail
items = [None]
else: else:
items = [None] items = [None]

View file

@ -223,3 +223,22 @@
- assert: - assert:
that: that:
- with_list_passed_a_dict is failed - with_list_passed_a_dict is failed
# https://github.com/ansible/ansible/issues/45189
- name: with_X conditional delegate_to shortcircuit on templating error
debug:
msg: "loop"
when: false
delegate_to: localhost
with_list: "{{ fake_var }}"
register: result
failed_when: result is not skipped
- name: loop conditional delegate_to shortcircuit on templating error
debug:
msg: "loop"
when: false
delegate_to: localhost
loop: "{{ fake_var }}"
register: result
failed_when: result is not skipped