Repository: ansible/ansible

Issue:
templated tag variable is undefined in include_tasks when used in a loop
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### ISSUE TYPE
<!--- Pick one below and delete the rest -->
 - Bug Report


##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
- include_tasks
- with_items

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.5.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/hinrikus/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.14 (default, Jan  5 2018, 10:41:29) [GCC 7.2.1 20171224]
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).-->
```
```

##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.-->
N/A

##### SUMMARY
<!--- Explain the problem briefly -->
In a loop, e.g. `with_items`, we are using `include_tasks`. 
In the included task, we want to use dynamic tags depending on `item`.
Ansible outputs the error:
```
ERROR! 'item' is undefined
```
That is unexpected behaviour because it works with "normal" variables. 

##### STEPS TO REPRODUCE
<!--- For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used. -->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
#file: roles/foo/tasks/main.yml
---
- include_tasks: app.yml
  with_items: 
    - item1
    - item2

#file: roles/foo/tasks/app.yml
---
- debug:
    msg: "item = {{ item }}"
  tags:
    - "{{ item }}"
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
We would expect to see the debug message.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
The mentioned error message above and nothing else.
<!--- Paste verbatim command output between quotes below -->
```
TASK [foo : include_tasks] ******************************************************************
task path: /tmp/ansible_test/roles/foo/tasks/main.yml:3
included: /tmp/ansible_test/roles/foo/tasks/app.yml for localhost
ERROR! 'item' is undefined
```



Discussion:
Files identified in the description:
* [lib/ansible/modules/utilities/logic/include_tasks.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/utilities/logic/include_tasks.py)
* [lib/ansible/playbook/loop_control.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/playbook/loop_control.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->

Resolving patch:
diff --git a/lib/ansible/plugins/strategy/linear.py b/lib/ansible/plugins/strategy/linear.py
--- a/lib/ansible/plugins/strategy/linear.py
+++ b/lib/ansible/plugins/strategy/linear.py
@@ -363,7 +363,7 @@ def run(self, iterator, play_context):
                             for new_block in new_blocks:
                                 task_vars = self._variable_manager.get_vars(
                                     play=iterator._play,
-                                    task=included_file._task,
+                                    task=new_block._parent
                                 )
                                 display.debug("filtering new block on tags")
                                 final_block = new_block.filter_tagged_tasks(play_context, task_vars)


Test patch: