Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use templated loop_var/index_var when looping include_* #58866

Merged
merged 2 commits into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- Use templated loop_var/index_var when looping include_* (https://github.com/ansible/ansible/issues/58820)
1 change: 1 addition & 0 deletions lib/ansible/executor/task_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ def _run_loop(self, items):
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']

Expand Down
7 changes: 2 additions & 5 deletions lib/ansible/playbook/included_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,8 @@ def process_include_results(results, iterator, loader, variable_manager):

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:
Expand Down
10 changes: 10 additions & 0 deletions test/integration/targets/loops/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- name: Validate that the correct value was used
assert:
that:
- templated_loop_var_name == 'value'