From a213b9160c7902122d20f05b2fa27a15cd541f4a Mon Sep 17 00:00:00 2001 From: Strahinja Kustudic Date: Tue, 27 Aug 2019 19:09:56 +0200 Subject: [PATCH] Fix extended loop_control with includes (#61231) * Fix extended loop_control with includes * Use assert for testing extended loop_control * fix typo --- lib/ansible/playbook/included_file.py | 2 ++ test/integration/targets/loop_control/extended.yml | 12 ++++++++++++ test/integration/targets/loop_control/inner.yml | 9 +++++++++ test/integration/targets/loop_control/runme.sh | 1 + 4 files changed, 24 insertions(+) create mode 100644 test/integration/targets/loop_control/extended.yml create mode 100644 test/integration/targets/loop_control/inner.yml diff --git a/lib/ansible/playbook/included_file.py b/lib/ansible/playbook/included_file.py index c7750cb0a06..7870d83a6a2 100644 --- a/lib/ansible/playbook/included_file.py +++ b/lib/ansible/playbook/included_file.py @@ -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 diff --git a/test/integration/targets/loop_control/extended.yml b/test/integration/targets/loop_control/extended.yml new file mode 100644 index 00000000000..6ad9933a30e --- /dev/null +++ b/test/integration/targets/loop_control/extended.yml @@ -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 diff --git a/test/integration/targets/loop_control/inner.yml b/test/integration/targets/loop_control/inner.yml new file mode 100644 index 00000000000..1c286fa4607 --- /dev/null +++ b/test/integration/targets/loop_control/inner.yml @@ -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' diff --git a/test/integration/targets/loop_control/runme.sh b/test/integration/targets/loop_control/runme.sh index 01f178e9da5..af065ea0e22 100755 --- a/test/integration/targets/loop_control/runme.sh +++ b/test/integration/targets/loop_control/runme.sh @@ -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 "$@"