Repository: ansible/ansible

Issue:
loop_control.label with include_tasks
<!---
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. -->

##### SUMMARY
<!--- Explain the problem briefly -->
When `loop` module used with `include_tasks`, ansible print out full paths of included tasks only.
Assignment `loop_control.label` does not affect.

Labels listing may be very useful in some cases:
 - Deploy running in one-step-at-a-time mode and the contents of the list are unexpected.
 - Troubleshooting post deploy issues.
##### ISSUE TYPE
 - Feature Idea

##### 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-->
Unknown

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.6.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/me/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.15rc1 (default, Apr 15 2018, 21:51:34) [GCC 7.3.0]
```

##### 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).-->
```
ANSIBLE_PIPELINING(/home/me/deploy/ansible.cfg) = True
DEFAULT_HASH_BEHAVIOUR(/home/me/deploy/ansible.cfg) = merge
DEFAULT_HOST_LIST(/home/me/deploy/ansible.cfg) = [u'/home/me/deploy/env/dev']
DEFAULT_ROLES_PATH(/home/me/deploy/ansible.cfg) = [u'/home/me/deploy/roles/in-house', u'/home/me/deploy/roles/3rd-party']
DEFAULT_STDOUT_CALLBACK(/home/me/deploy/ansible.cfg) = debug
DIFF_ALWAYS(/home/me/deploy/ansible.cfg) = True
PERSISTENT_CONNECT_TIMEOUT(/home/me/deploy/ansible.cfg) = 30
RETRY_FILES_ENABLED(/home/me/deploy/ansible.cfg) = False
```
##### 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.-->
Ubuntu 18.04 LTS
##### 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
- hosts: test
  tasks: 
  - include_tasks: tasks.yml
    with_items: [1,2]
    loop_control: { label: "{{ 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? -->
```
$ ansible-playbook play.yml --step

PLAY [test] ***************************************************
Perform task: TASK: include_tasks (N)o/(y)es/(c)ontinue: y

Perform task: TASK: include_tasks (N)o/(y)es/(c)ontinue: ******

TASK [include_tasks] ******************************************
included: /ansible/playbooks/tasks.yml for test (item=1)
included: /ansible/playbooks/tasks.yml for test (item=2)
Perform task: TASK: debug (N)o/(y)es/(c)ontinue:
```
##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
```
$ ansible-playbook play.yml --step

PLAY [test] ***************************************************
Perform task: TASK: include_tasks (N)o/(y)es/(c)ontinue: y

Perform task: TASK: include_tasks (N)o/(y)es/(c)ontinue: ******

TASK [include_tasks] ******************************************
included: /ansible/playbooks/tasks.yml for test
included: /ansible/playbooks/tasks.yml for test
Perform task: TASK: debug (N)o/(y)es/(c)ontinue:
```
<!--- Paste verbatim command output between quotes below -->



Discussion:
Files identified in the description:
None

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/playbook/included_file.py b/lib/ansible/playbook/included_file.py
--- a/lib/ansible/playbook/included_file.py
+++ b/lib/ansible/playbook/included_file.py
@@ -93,6 +93,10 @@ def process_include_results(results, iterator, loader, variable_manager):
                         task_vars[loop_var] = include_variables[loop_var] = include_result[loop_var]
                     if index_var and index_var in include_result:
                         task_vars[index_var] = include_variables[index_var] = include_result[index_var]
+                    if '_ansible_item_label' in include_result:
+                        task_vars['_ansible_item_label'] = include_variables['_ansible_item_label'] = include_result['_ansible_item_label']
+                    if original_task.no_log and '_ansible_no_log' not in include_variables:
+                        task_vars['_ansible_no_log'] = include_variables['_ansible_no_log'] = original_task.no_log
 
                     if original_task.action in ('include', 'include_tasks'):
                         include_file = None
diff --git a/lib/ansible/plugins/callback/default.py b/lib/ansible/plugins/callback/default.py
--- a/lib/ansible/plugins/callback/default.py
+++ b/lib/ansible/plugins/callback/default.py
@@ -269,6 +269,8 @@ def v2_runner_item_on_skipped(self, result):
 
     def v2_playbook_on_include(self, included_file):
         msg = 'included: %s for %s' % (included_file._filename, ", ".join([h.name for h in included_file._hosts]))
+        if 'item' in included_file._args:
+            msg += " => (item=%s)" % (self._get_item_label(included_file._args),)
         self._display.display(msg, color=C.COLOR_SKIP)
 
     def v2_playbook_on_stats(self, stats):


Test patch:
diff --git a/test/integration/targets/include_import/runme.sh b/test/integration/targets/include_import/runme.sh
--- a/test/integration/targets/include_import/runme.sh
+++ b/test/integration/targets/include_import/runme.sh
@@ -76,6 +76,6 @@ fi
 
 # Test that duplicate items in loop are not deduped
 ANSIBLE_STRATEGY='linear' ansible-playbook tasks/test_include_dupe_loop.yml -i ../../inventory "$@" | tee test_include_dupe_loop.out
-test "$(grep -c 'item=foo' test_include_dupe_loop.out)" = 3
+test "$(grep -c '"item=foo"' test_include_dupe_loop.out)" = 3
 ANSIBLE_STRATEGY='free' ansible-playbook tasks/test_include_dupe_loop.yml -i ../../inventory "$@" | tee test_include_dupe_loop.out
-test "$(grep -c 'item=foo' test_include_dupe_loop.out)" = 3
\ No newline at end of file
+test "$(grep -c '"item=foo"' test_include_dupe_loop.out)" = 3
\ No newline at end of file