Repository: ansible/ansible

Issue:
include_role skips all tasks if a conditional role in the dependencies is not included due to the conditon
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
include_role

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.3.0.0
  config file = 
  configured module search path = Default w/o overrides
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]

```

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

##### OS / ENVIRONMENT
Ubuntu 16.04

<!--- Explain the problem briefly -->
I noticed that `include_role` skips all tasks when called in a role, that has a conditional dependency to another role and this condition resolves to False

##### 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.
-->
directory structure:
```
├── hosts
├── roles
│   ├── role1
│   │   ├── meta
│   │   │   └── main.yml
│   │   └── tasks
│   │       └── main.yml
│   ├── role2
│   │   └── tasks
│   │       └── main.yml
│   └── role3
│       └── tasks
│           └── main.yml
├── test.retry
└── test.yml

```
inventory (hosts):
```
# vi:set ft=yaml tabstop=2 shiftwidth=2 smarttab expandtab:

localhost gather_facts=no ansible_python_interpreter=python2 ansible_connection=local
```
playbook (test.yml):
````yaml
#

- hosts: all
  roles:
    - { role: role1 }

````
role1 meta (meta/main.yml):
````yaml
dependencies:
 - { role: role2, when: False }

````
role1 main task (tasks/main.yml):
````yaml
# role 1
---

- name: 'role 1: debug'
  debug:
    msg: 'role 1'

- name: 'role 1: include role3'
  include_role:
    name: 'role3'

````
role 2 main task (tasks/main.yml):
````yaml
# role 2
---

- name: 'role 2: debug'
  debug:
    msg: 'role 2'
````
role 3 main task (tasks/main.yml):
````yaml
# role 3
---

- name: 'role 3: debug'
  debug:
    msg: 'role 3'
````
Play:
````
 ansible-playbook -i hosts test.yml 
````

##### EXPECTED RESULTS
````
No config file found; using defaults

PLAYBOOK: test.yml *********************************************************************************************************************************************************************************************
1 plays in test.yml

PLAY [all] *****************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************************************************
Using module file /usr/local/lib/python2.7/dist-packages/ansible/modules/system/setup.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: xxx
<localhost> EXEC /bin/sh -c 'echo ~ && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/xxx/.ansible/tmp/ansible-tmp-1496074407.62-3930217730450 `" && echo ansible-tmp-1496074407.62-3930217730450="` echo /home/xxx/.ansible/tmp/ansible-tmp-1496074407.62-3930217730450 `" ) && sleep 0'
<localhost> PUT /tmp/tmpuQJ5pZ TO /home/xxx/.ansible/tmp/ansible-tmp-1496074407.62-3930217730450/setup.py
<localhost> EXEC /bin/sh -c 'chmod u+x /home/xxx/.ansible/tmp/ansible-tmp-1496074407.62-3930217730450/ /home/xxx/.ansible/tmp/ansible-tmp-1496074407.62-3930217730450/setup.py && sleep 0'
<localhost> EXEC /bin/sh -c 'python2 /home/xxx/.ansible/tmp/ansible-tmp-1496074407.62-3930217730450/setup.py; rm -rf "/home/xxx/.ansible/tmp/ansible-tmp-1496074407.62-3930217730450/" > /dev/null 2>&1 && sleep 0'
ok: [localhost]
META: ran handlers

TASK [role2 : role 2: debug] ***********************************************************************************************************************************************************************************
task path: /home/xxx/ansible-bug/roles/role2/tasks/main.yml:4
skipping: [localhost] => {
    "changed": false, 
    "skip_reason": "Conditional result was False", 
    "skipped": true
}

TASK [role1 : role 1: debug] ***********************************************************************************************************************************************************************************
task path: /home/xxx/ansible-bug/roles/role1/tasks/main.yml:4
ok: [localhost] => {
    "changed": false, 
    "msg": "role 1"
}

TASK [role3 : role 3: debug] ***********************************************************************************************************************************************************************************
task path: /home/xxx/ansible-bug/roles/role3/tasks/main.yml:4
ok: [localhost] => {
    "changed": false,
    "msg": "role 3"
}
META: ran handlers
META: ran handlers

PLAY RECAP *****************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0
````
##### ACTUAL RESULTS
```
No config file found; using defaults

PLAYBOOK: test.yml *********************************************************************************************************************************************************************************************
1 plays in test.yml

PLAY [all] *****************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************************************************
Using module file /usr/local/lib/python2.7/dist-packages/ansible/modules/system/setup.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: xxx
<localhost> EXEC /bin/sh -c 'echo ~ && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/xxx/.ansible/tmp/ansible-tmp-1496074407.62-3930217730450 `" && echo ansible-tmp-1496074407.62-3930217730450="` echo /home/xxx/.ansible/tmp/ansible-tmp-1496074407.62-3930217730450 `" ) && sleep 0'
<localhost> PUT /tmp/tmpuQJ5pZ TO /home/xxx/.ansible/tmp/ansible-tmp-1496074407.62-3930217730450/setup.py
<localhost> EXEC /bin/sh -c 'chmod u+x /home/xxx/.ansible/tmp/ansible-tmp-1496074407.62-3930217730450/ /home/xxx/.ansible/tmp/ansible-tmp-1496074407.62-3930217730450/setup.py && sleep 0'
<localhost> EXEC /bin/sh -c 'python2 /home/xxx/.ansible/tmp/ansible-tmp-1496074407.62-3930217730450/setup.py; rm -rf "/home/xxx/.ansible/tmp/ansible-tmp-1496074407.62-3930217730450/" > /dev/null 2>&1 && sleep 0'
ok: [localhost]
META: ran handlers

TASK [role2 : role 2: debug] ***********************************************************************************************************************************************************************************
task path: /home/xxx/ansible-bug/roles/role2/tasks/main.yml:4
skipping: [localhost] => {
    "changed": false, 
    "skip_reason": "Conditional result was False", 
    "skipped": true
}

TASK [role1 : role 1: debug] ***********************************************************************************************************************************************************************************
task path: /home/xxx/ansible-bug/roles/role1/tasks/main.yml:4
ok: [localhost] => {
    "changed": false, 
    "msg": "role 1"
}

TASK [role3 : role 3: debug] ***********************************************************************************************************************************************************************************
task path: /home/xxx/ansible-bug/roles/role3/tasks/main.yml:4
skipping: [localhost] => {
    "changed": false, 
    "skip_reason": "Conditional result was False", 
    "skipped": true
}
META: ran handlers
META: ran handlers

PLAY RECAP *****************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0   

```


Discussion:
seems to be the same issue as in #23733 and #23379
Hi,

Any feedback on this bug?

Resolving patch:
diff --git a/lib/ansible/playbook/helpers.py b/lib/ansible/playbook/helpers.py
--- a/lib/ansible/playbook/helpers.py
+++ b/lib/ansible/playbook/helpers.py
@@ -296,6 +296,7 @@ def load_list_of_tasks(ds, play, block=None, role=None, task_include=None, use_h
                                 (use_handlers and C.DEFAULT_HANDLER_INCLUDES_STATIC)  or \
                                 (not needs_templating and ir.all_parents_static() and not ir.loop)
                     display.debug('Determined that if include_role static is %s' % str(is_static))
+
                 if is_static:
                     # uses compiled list from object
                     t = task_list.extend(ir.get_block_list(variable_manager=variable_manager, loader=loader))
diff --git a/lib/ansible/playbook/role_include.py b/lib/ansible/playbook/role_include.py
--- a/lib/ansible/playbook/role_include.py
+++ b/lib/ansible/playbook/role_include.py
@@ -82,7 +82,6 @@ def get_block_list(self, play=None, variable_manager=None, loader=None):
             dep_chain = []
         else:
             dep_chain = list(self._parent_role._parents)
-            dep_chain.extend(self._parent_role.get_all_dependencies())
             dep_chain.append(self._parent_role)
 
         blocks = actual_role.compile(play=myplay, dep_chain=dep_chain)
@@ -117,7 +116,7 @@ def load(data, block=None, role=None, task_include=None, variable_manager=None,
             if option in ir.args:
                 setattr(ir, option, ir.args.get(option))
 
-        return ir.load_data(data, variable_manager=variable_manager, loader=loader)
+        return ir
 
     def copy(self, exclude_parent=False, exclude_tasks=False):
 


Test patch: