From 7346b699eec99d7279da4175b99f07e08f0de283 Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Wed, 10 Jul 2019 13:49:24 +0200 Subject: [PATCH] Use templated loop_var/index_var when looping include_* (#58866) Fixes #58820 --- .../58820-use-templated-loop_var-in-include_tasks.yaml | 2 ++ lib/ansible/executor/task_executor.py | 1 + lib/ansible/playbook/included_file.py | 7 ++----- test/integration/targets/loops/tasks/main.yml | 10 ++++++++++ .../targets/loops/tasks/templated_loop_var_tasks.yml | 4 ++++ 5 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/58820-use-templated-loop_var-in-include_tasks.yaml create mode 100644 test/integration/targets/loops/tasks/templated_loop_var_tasks.yml diff --git a/changelogs/fragments/58820-use-templated-loop_var-in-include_tasks.yaml b/changelogs/fragments/58820-use-templated-loop_var-in-include_tasks.yaml new file mode 100644 index 00000000000..ba0440736a7 --- /dev/null +++ b/changelogs/fragments/58820-use-templated-loop_var-in-include_tasks.yaml @@ -0,0 +1,2 @@ +bugfixes: + - Use templated loop_var/index_var when looping include_* (https://github.com/ansible/ansible/issues/58820) diff --git a/lib/ansible/executor/task_executor.py b/lib/ansible/executor/task_executor.py index fe10d0a5617..0865a3a50bc 100644 --- a/lib/ansible/executor/task_executor.py +++ b/lib/ansible/executor/task_executor.py @@ -382,6 +382,7 @@ class TaskExecutor: res['ansible_loop_var'] = loop_var if index_var: res[index_var] = item_index + res['ansible_index_var'] = index_var if extended: res['ansible_loop'] = task_vars['ansible_loop'] diff --git a/lib/ansible/playbook/included_file.py b/lib/ansible/playbook/included_file.py index 5500b7f00bd..c7750cb0a06 100644 --- a/lib/ansible/playbook/included_file.py +++ b/lib/ansible/playbook/included_file.py @@ -87,11 +87,8 @@ class IncludedFile: include_args = include_result.get('include_args', dict()) special_vars = {} - loop_var = 'item' - index_var = None - if original_task.loop_control: - loop_var = original_task.loop_control.loop_var - index_var = original_task.loop_control.index_var + loop_var = include_result.get('ansible_loop_var', 'item') + index_var = include_result.get('ansible_index_var') if loop_var in include_result: task_vars[loop_var] = special_vars[loop_var] = include_result[loop_var] if index_var and index_var in include_result: diff --git a/test/integration/targets/loops/tasks/main.yml b/test/integration/targets/loops/tasks/main.yml index 85fb5ce4b38..0ce32ab589e 100644 --- a/test/integration/targets/loops/tasks/main.yml +++ b/test/integration/targets/loops/tasks/main.yml @@ -334,3 +334,13 @@ - 1 loop_control: loop_var: alvin + +# https://github.com/ansible/ansible/issues/58820 +- name: Test using templated loop_var inside include_tasks + include_tasks: templated_loop_var_tasks.yml + loop: + - value + loop_control: + loop_var: "{{ loop_var_name }}" + vars: + loop_var_name: templated_loop_var_name diff --git a/test/integration/targets/loops/tasks/templated_loop_var_tasks.yml b/test/integration/targets/loops/tasks/templated_loop_var_tasks.yml new file mode 100644 index 00000000000..1f8f96979a1 --- /dev/null +++ b/test/integration/targets/loops/tasks/templated_loop_var_tasks.yml @@ -0,0 +1,4 @@ +- name: Validate that the correct value was used + assert: + that: + - templated_loop_var_name == 'value'