# conditional roles

**URL:** <https://forum.ansible.com/t/conditional-roles/12801>\
**Category:** Ansible Project\
**Created:** [May 13, 2013, 4:23pm UTC](https://forum.ansible.com/t/conditional-roles/12801 "2013-05-13T16:23:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [May 13, 2013, 4:23pm UTC](https://forum.ansible.com/t/conditional-roles/12801/1 "2013-05-13T16:23:24Z")

</div>

Is this normal/expected?

I am trying to use conditional roles, and once it hits a negative condition, it skips every role afterwards:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- hosts: all  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- common  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- role: AppDynamics  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: appdynamics  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- java

"appdynamics" is loaded as True/False from a group\_vars, and is different depending on the environment loaded (different inventories, group\_vars etc... for PROD vs. TEST vs. DEV).

With the playbook above, the java role is skipped when appdynamic is False. If I move the java role above the AppDynamics role, then the java role never gets skipped.

Should the condition affect roles after it?

Thanks.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 13, 2013, 5:26pm UTC](https://forum.ansible.com/t/conditional-roles/12801/2 "2013-05-13T17:26:03Z")

</div>

I’m not sure you’re seeing what behavior you think you are seeing.

When just applies the conditional to each task.

It’s also a confusing (and noisy output) way to organize things, which is why we don’t encourage it.

it’s much better to do this:

- hosts: AppDynamics  
roles:
- AppDynamics

---

<div class="post-metadata">

**Author:** ![David\_Reagan](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@David\_Reagan](https://forum.ansible.com/u/David_Reagan)\
**Post date:** [March 15, 2014, 2:32am UTC](https://forum.ansible.com/t/conditional-roles/12801/3 "2014-03-15T02:32:25Z")

</div>

I was just looking to do the same thing. Michael’s post clued me into what I was missing.

Specifically, I hadn’t realized you can list your hosts more than once in the hosts file. That makes a site.yml file similar to:

- hosts: roleA  
roles:

- roleA

- hosts: roleB  
roles:

- roleB

Work to give you the same effect as we wanted with conditional roles. Just list the host under all the roles you want it to have in the groups file.

Thanks Michael!
