Fix extended loop_control with includes (#61231)

* Fix extended loop_control with includes

* Use assert for testing extended loop_control

* fix typo

(cherry picked from commit a213b9160c)
This commit is contained in:
Strahinja Kustudic 2019-08-27 19:09:56 +02:00 committed by Toshio Kuratomi
parent 59280a933e
commit e7fcb37e6c
4 changed files with 24 additions and 0 deletions

View file

@ -95,6 +95,8 @@ class IncludedFile:
task_vars[index_var] = special_vars[index_var] = include_result[index_var]
if '_ansible_item_label' in include_result:
task_vars['_ansible_item_label'] = special_vars['_ansible_item_label'] = include_result['_ansible_item_label']
if 'ansible_loop' in include_result:
task_vars['ansible_loop'] = special_vars['ansible_loop'] = include_result['ansible_loop']
if original_task.no_log and '_ansible_no_log' not in include_args:
task_vars['_ansible_no_log'] = special_vars['_ansible_no_log'] = original_task.no_log

View file

@ -0,0 +1,12 @@
- name: loop_control/extended/include https://github.com/ansible/ansible/issues/61218
hosts: localhost
gather_facts: false
tasks:
- name: loop on an include
include_tasks: inner.yml
loop:
- first
- second
- third
loop_control:
extended: yes

View file

@ -0,0 +1,9 @@
- name: assert ansible_loop variables in include_tasks
assert:
that:
- ansible_loop.index == ansible_loop.index0 + 1
- ansible_loop.revindex == ansible_loop.revindex0 + 1
- ansible_loop.first == {{ ansible_loop.index == 1 }}
- ansible_loop.last == {{ ansible_loop.index == ansible_loop.length }}
- ansible_loop.length == 3
- ansible_loop.allitems|join(',') == 'first,second,third'

View file

@ -9,3 +9,4 @@ MATCH='foo_label
bar_label'
[ "$(ansible-playbook label.yml "$@" |grep 'item='|sed -e 's/^.*(item=looped_var \(.*\)).*$/\1/')" == "${MATCH}" ]
ansible-playbook extended.yml "$@"