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

Fixes #58820
pull/58920/head
Martin Krizek 5 years ago committed by GitHub
parent ad49057330
commit 7346b699ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

@ -382,6 +382,7 @@ class TaskExecutor:
res['ansible_loop_var'] = loop_var res['ansible_loop_var'] = loop_var
if index_var: if index_var:
res[index_var] = item_index res[index_var] = item_index
res['ansible_index_var'] = index_var
if extended: if extended:
res['ansible_loop'] = task_vars['ansible_loop'] res['ansible_loop'] = task_vars['ansible_loop']

@ -87,11 +87,8 @@ class IncludedFile:
include_args = include_result.get('include_args', dict()) include_args = include_result.get('include_args', dict())
special_vars = {} special_vars = {}
loop_var = 'item' loop_var = include_result.get('ansible_loop_var', 'item')
index_var = None index_var = include_result.get('ansible_index_var')
if original_task.loop_control:
loop_var = original_task.loop_control.loop_var
index_var = original_task.loop_control.index_var
if loop_var in include_result: if loop_var in include_result:
task_vars[loop_var] = special_vars[loop_var] = include_result[loop_var] task_vars[loop_var] = special_vars[loop_var] = include_result[loop_var]
if index_var and index_var in include_result: if index_var and index_var in include_result:

@ -334,3 +334,13 @@
- 1 - 1
loop_control: loop_control:
loop_var: alvin 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

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