# Conditional role execution problem

**URL:** <https://forum.ansible.com/t/conditional-role-execution-problem/14187>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [December 19, 2013, 10:59pm UTC](https://forum.ansible.com/t/conditional-role-execution-problem/14187 "2013-12-19T22:59:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Cleveland](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@Matt\_Cleveland](https://forum.ansible.com/u/Matt_Cleveland)\
**Post date:** [December 19, 2013, 10:59pm UTC](https://forum.ansible.com/t/conditional-role-execution-problem/14187/1 "2013-12-19T22:59:22Z")

</div>

Hello all,

I’m trying to execute roles in a playbook conditionally. The conditions are based on command line args as passed through “–extra-vars”. I’ve tried several things and none work. Here are a few examples:

- hosts: ec2hosts  
user: ubuntu  
roles:

- common

- { role: role\_1, when: “{{role\_1}} == ‘yes’” }

- { role: role\_2, when: “{{role\_2}} == ‘yes’” }

When I run the above with “role\_1=no” in extra-vars the role is still executed. Here is the debug output from the above:

TASK: [common | print 2] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
\<X.X.X.X\> ESTABLISH CONNECTION FOR USER: ubuntu  
ok: [X.X.X.X] =\> {  
“item”: “”,  
“msg”: “role\_1\_matt no”  
}

- hosts: ec2hosts  
user: ubuntu  
roles:
- common
- { role: role\_1, when: “{{role\_1}} is defined” }
- { role: role\_2, when: “{{role\_2}} is defined” }

When I run the above with “role\_1” omitted from extra-vars it is still run. The debug output from the latter scenario is this:

TASK: [common | print] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
\<X.X.X\>X\> ESTABLISH CONNECTION FOR USER: ubuntu  
ok: [X.X.X.X] =\> {  
“item”: “”,  
“msg”: “role\_1\_matt {{role\_1}}”  
}

Any help is greatly appreciated, thanks!

---

<div class="post-metadata">

**Author:** ![Matt\_Cleveland](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@Matt\_Cleveland](https://forum.ansible.com/u/Matt_Cleveland)\
**Post date:** [December 20, 2013, 9:50pm UTC](https://forum.ansible.com/t/conditional-role-execution-problem/14187/2 "2013-12-20T21:50:47Z")

</div>

For conditional roles you should just do this:

- { role: role\_1, when: role\_1 == ‘yes’ }
- { role: role\_2, when: role\_2 == ‘yes’ }

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [December 20, 2013, 10:27pm UTC](https://forum.ansible.com/t/conditional-role-execution-problem/14187/3 "2013-12-20T22:27:24Z")

</div>

To follow up here, since this was worked out in IRC. The solution to getting this working is to change the role statement to look something like the following:

- { role: role\_2, when: role\_2|default(’no’) == 'yes’ }

---

<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:** [December 20, 2013, 11:19pm UTC](https://forum.ansible.com/t/conditional-role-execution-problem/14187/4 "2013-12-20T23:19:37Z")

</div>

So sounds like role\_2 as a variable just wasn’t passed in and there was no problem with role\_1 ?
