# How to use conditions in Playbook itself

**URL:** <https://forum.ansible.com/t/how-to-use-conditions-in-playbook-itself/17342>\
**Category:** Ansible Project\
**Created:** [January 21, 2015, 4:48pm UTC](https://forum.ansible.com/t/how-to-use-conditions-in-playbook-itself/17342 "2015-01-21T16:48:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Suraj](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@Suraj](https://forum.ansible.com/u/Suraj)\
**Post date:** [January 21, 2015, 4:48pm UTC](https://forum.ansible.com/t/how-to-use-conditions-in-playbook-itself/17342/1 "2015-01-21T16:48:55Z")

</div>

Hi Folks,

I am trying to create single playbook for every service we have. So here we need to deploy multiple roles on different service instance.  
What I want is that, I want to just define variable in different group vars files and depending upon that variable, playbook should run the roles like:

if we have service A which has variable like:  
roles:

- common
- roleA

and service B has:  
roles:

- common
- roleB

now single playbook should run use these variables and depending upon value it should sun respective roles on respective service instances like:

playbook.yml

…

roles:

- { role : “{{ item }}” }  
with\_items:
- roles

But it is not working as I want, it give me error:  
“ **ERROR: with\_items is not a legal parameter at this level in an Ansible Playbook** ”

Any Ideas how can I achieve the same using conditions in playbook??

Best Wishes,  
Suraj

---

<div class="post-metadata">

**Author:** ![Jonathan\_Davila](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@Jonathan\_Davila](https://forum.ansible.com/u/Jonathan_Davila)\
**Post date:** [January 21, 2015, 5:05pm UTC](https://forum.ansible.com/t/how-to-use-conditions-in-playbook-itself/17342/2 "2015-01-21T17:05:28Z")

</div>

Seems to be sort of an anti-pattern to do it this way. Referencing: [https://groups.google.com/forum/#!msg/ansible-project/CPAE8\_0YXMg/Az3Etfz7mP8J](https://groups.google.com/forum/#!msg/ansible-project/CPAE8_0YXMg/Az3Etfz7mP8J)

It sounds like a similar issue at the end.

---

<div class="post-metadata">

**Author:** ![Suraj](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@Suraj](https://forum.ansible.com/u/Suraj)\
**Post date:** [January 21, 2015, 5:14pm UTC](https://forum.ansible.com/t/how-to-use-conditions-in-playbook-itself/17342/3 "2015-01-21T17:14:40Z")

</div>

I think with this I am still setting every configuration related to one service in single var file so this way I am maintaining state of the service at one place and every common thing for all the services will be written in single playbook.

---

<div class="post-metadata">

**Author:** ![Suraj](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@Suraj](https://forum.ansible.com/u/Suraj)\
**Post date:** [January 29, 2015, 9:18am UTC](https://forum.ansible.com/t/how-to-use-conditions-in-playbook-itself/17342/4 "2015-01-29T09:18:40Z")

</div>

Can somebody please help me on this one??

---

<div class="post-metadata">

**Author:** ![cselden](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/cselden/32/4405_2.png) [@cselden](https://forum.ansible.com/u/cselden)\
**Post date:** [February 5, 2015, 12:38am UTC](https://forum.ansible.com/t/how-to-use-conditions-in-playbook-itself/17342/5 "2015-02-05T00:38:01Z")

</div>

Suraj,

I’ve been trying to find a way to do this as well, with no luck so far.

Ideally, I’d be able to use a loop (either with\_items, or with\_dict) to run multiple and differing roles on a per-host basis. But, alas, this is not a feature and from what I can find from older posts, will not be a feature. Passing in a list variable to “roles:” is a good idea, but it doesn’t seem like Ansible can use variables (even extra-vars from the ansible-playbook command) to determine which roles are to be used.

I realize that this may not be considered “best practice”, but looping through roles and/or include statements would be a MUCH welcome feature.

Chip

---

<div class="post-metadata">

**Author:** ![Giovanni\_Tirloni](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Giovanni\_Tirloni](https://forum.ansible.com/u/Giovanni_Tirloni)\
**Post date:** [February 5, 2015, 10:31am UTC](https://forum.ansible.com/t/how-to-use-conditions-in-playbook-itself/17342/6 "2015-02-05T10:31:39Z")

</div>

If I understand it, there is a need to run a playbook and it would apply  
a different set of roles to different set of roles, right?

My suggestion is to define many plays per playbook. You'd not be  
defining a "roles" variable to be imported and, sure, there is more  
typing involved but it's clean and works:

---

<div class="post-metadata">

**Author:** ![cselden](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/cselden/32/4405_2.png) [@cselden](https://forum.ansible.com/u/cselden)\
**Post date:** [February 5, 2015, 3:56pm UTC](https://forum.ansible.com/t/how-to-use-conditions-in-playbook-itself/17342/7 "2015-02-05T15:56:55Z")

</div>

Giovanni,

Thanks for joining in.

Having multiple plays in a playbook is obviously the easiest solution. Especially when there’s only a handful of combinations/groups. However, what happens when you have 20 or 30 different combinations? And what happens when the combinations of roles for a group frequently change? It becomes very cumbersome to maintain the playbook and edit it on a per-run basis.

Dynamic inventory allows for this sort of flexibility with assigning variables to groups and/or hosts that define which roles are applicable. It’s difficult to account for these playbooks on the playbook side without looping through roles. I’d like to do something like this:
