Repository: ansible/ansible

Issue:
include_role variable name cannot be host specific

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
include_role

##### ANSIBLE VERSION
```
ansible 2.5.0
  config file = /export/home/orion-admin/ansible-boulder/ansible.cfg
  configured module search path = [u'/export/home/orion-admin/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible
  python version = 2.7.5 (default, Aug  2 2017, 11:05:32) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

```

##### CONFIGURATION
```
ANSIBLE_PIPELINING(/export/home/orion-admin/ansible-boulder/ansible.cfg) = True
ANSIBLE_SSH_ARGS(/export/home/orion-admin/ansible-boulder/ansible.cfg) = -C -o ControlMaster=auto -
DEFAULT_CALLBACK_WHITELIST(/export/home/orion-admin/ansible-boulder/ansible.cfg) = ['profile_tasks'
DEFAULT_FORKS(/export/home/orion-admin/ansible-boulder/ansible.cfg) = 25
DEFAULT_GATHERING(/export/home/orion-admin/ansible-boulder/ansible.cfg) = smart
DEFAULT_HOST_LIST(/export/home/orion-admin/ansible-boulder/ansible.cfg) = [u'/export/home/orion-adm
DEFAULT_REMOTE_USER(/export/home/orion-admin/ansible-boulder/ansible.cfg) = root
DEFAULT_ROLES_PATH(/export/home/orion-admin/ansible-boulder/ansible.cfg) = [u'/export/home/orion-ad
PERSISTENT_CONNECT_TIMEOUT(/export/home/orion-admin/ansible-boulder/ansible.cfg) = 30
RETRY_FILES_ENABLED(/export/home/orion-admin/ansible-boulder/ansible.cfg) = False
```
##### OS / ENVIRONMENT
Scientific Linux 7.4

##### SUMMARY
Perhaps unsurprisingly, using a variable that is host specific as the name for include_role does not result in a different role being included for the different hosts.  Instead, first one evaluated appears to win.  I'm not sure if this can be supported, and if not it should be better documented or perhaps an error thrown.

##### STEPS TO REPRODUCE

<!--- Paste example playbooks or commands between quotes below -->
```yaml
---
- hosts: host1, host2
  tasks:
   - debug: msg="incrole={{ incrole }}"
   - include_role: name="{{ incrole }}"
```
host_vars/host1:
```yaml
---
incrole: a
```
host_vars/host2:
```yaml
---
incrole: b
```
roles/a/tasks/main.yml:
```yaml
---
- debug: msg="In role a"
```
roles/b/tasks/main.yml:
```yaml
---
- debug: msg="In role b"
```
<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
```
ansible-playbook includebug.yml

PLAY [host1, host2] *****************************************************

TASK [Gathering Facts] ****************************************************************************
Monday 16 April 2018  13:03:33 -0600 (0:00:00.227)       0:00:00.227 **********
ok: [host1]
ok: [host2]

TASK [debug] **************************************************************************************
Monday 16 April 2018  13:03:36 -0600 (0:00:03.086)       0:00:03.314 **********
ok: [host1] => {
    "msg": "incrole=a"
}
ok: [host2] => {
    "msg": "incrole=b"
}

TASK [include_role] *******************************************************************************
Monday 16 April 2018  13:03:36 -0600 (0:00:00.167)       0:00:03.481 **********

TASK [a : debug] **********************************************************************************
Monday 16 April 2018  13:03:37 -0600 (0:00:00.198)       0:00:03.680 **********
ok: [host1] => {
    "msg": "In role a"
}
ok: [host2] => {
    "msg": "In role b"
```
##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
ansible-playbook includebug.yml

PLAY [host1, host2] *****************************************************

TASK [Gathering Facts] ****************************************************************************
Monday 16 April 2018  13:03:33 -0600 (0:00:00.227)       0:00:00.227 **********
ok: [host1]
ok: [host2]

TASK [debug] **************************************************************************************
Monday 16 April 2018  13:03:36 -0600 (0:00:03.086)       0:00:03.314 **********
ok: [host1] => {
    "msg": "incrole=a"
}
ok: [host2] => {
    "msg": "incrole=b"
}

TASK [include_role] *******************************************************************************
Monday 16 April 2018  13:03:36 -0600 (0:00:00.167)       0:00:03.481 **********

TASK [a : debug] **********************************************************************************
Monday 16 April 2018  13:03:37 -0600 (0:00:00.198)       0:00:03.680 **********
ok: [host1] => {
    "msg": "In role a"
}
ok: [host2] => {
    "msg": "In role a"

```



Discussion:
Files identified in the description:
* [lib/ansible/playbook/role/include.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/playbook/role/include.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/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
@@ -160,7 +160,7 @@ def process_include_results(results, iterator, loader, variable_manager):
                                 from_key = from_arg.replace('_from', '')
                                 new_task._from_files[from_key] = templar.template(include_variables[from_arg])
 
-                        inc_file = IncludedFile("role", include_variables, new_task, is_role=True)
+                        inc_file = IncludedFile(role_name, include_variables, new_task, is_role=True)
 
                     try:
                         pos = included_files.index(inc_file)


Test patch:
diff --git a/test/integration/inventory b/test/integration/inventory
--- a/test/integration/inventory
+++ b/test/integration/inventory
@@ -1,6 +1,6 @@
 [local]
 testhost ansible_ssh_host=127.0.0.1 ansible_connection=local host_var_role_name=role3
-testhost2 ansible_ssh_host=127.0.0.1 ansible_connection=local
+testhost2 ansible_ssh_host=127.0.0.1 ansible_connection=local host_var_role_name=role2
 # For testing delegate_to
 testhost3 ansible_ssh_host=127.0.0.3
 testhost4 ansible_ssh_host=127.0.0.4
diff --git a/test/integration/targets/include_import/role/test_include_role.yml b/test/integration/targets/include_import/role/test_include_role.yml
--- a/test/integration/targets/include_import/role/test_include_role.yml
+++ b/test/integration/targets/include_import/role/test_include_role.yml
@@ -1,5 +1,5 @@
 - name: Test include_role
-  hosts: testhost
+  hosts: testhost,testhost2
 
   vars:
     run_role: yes
@@ -85,10 +85,27 @@
       include_role:
         name: "{{ role_name }}"
 
+    - name: wipe role results
+      set_fact:
+        _role2_result: ~
+        _role3_result: ~
+
     - name: Test using a host variable for role name
       include_role:
         name: "{{ host_var_role_name }}"
 
+    - name: assert that host varible for role name calls 2 diff roles
+      assert:
+        that:
+          - _role2_result is not none
+      when: inventory_hostname == 'testhost2'
+
+    - name: assert that host varible for role name calls 2 diff roles
+      assert:
+        that:
+          - _role3_result is not none
+      when: inventory_hostname == 'testhost'
+
     - name: Pass variable to role
       include_role:
         name: role1