Use templated loop_var/index_var when looping include_* (#58866)

Fixes #58820

(cherry picked from commit 7346b699ee)
This commit is contained in:
Martin Krizek 2019-07-10 13:49:24 +02:00 committed by Toshio Kuratomi
parent 044923e4ae
commit d3afa17930
5 changed files with 19 additions and 5 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- Use templated loop_var/index_var when looping include_* (https://github.com/ansible/ansible/issues/58820)

View file

@ -381,6 +381,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']

View file

@ -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:

View file

@ -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

View file

@ -0,0 +1,4 @@
- name: Validate that the correct value was used
assert:
that:
- templated_loop_var_name == 'value'