# Using a sigle 'when' for multiple tasks

**URL:** <https://forum.ansible.com/t/using-a-sigle-when-for-multiple-tasks/14977>\
**Category:** Ansible Project\
**Created:** [April 9, 2014, 1:51pm UTC](https://forum.ansible.com/t/using-a-sigle-when-for-multiple-tasks/14977 "2014-04-09T13:51:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephan\_S](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@Stephan\_S](https://forum.ansible.com/u/Stephan_S)\
**Post date:** [April 9, 2014, 1:51pm UTC](https://forum.ansible.com/t/using-a-sigle-when-for-multiple-tasks/14977/1 "2014-04-09T13:51:02Z")

</div>

Hello,

i have a playbook which contains 10 tasks. I want to run those tasks only if a when clause if filled.  
My first approach was to put on every task the when. E.g.:

_ **- name: My command** _  
_ **command: sudo install…** _  
_ **when: a == b** _

The above example worked fine but was not ideal. The second approach was to use roles. Inside a playbook i declared a role. This role contained a task list  
which was executed only when the ‘when’ was true. That worked as well but i needed 3 files for something really simple.

My question is if i can have just one single playbook, including all 10 tasks and control all those tasks with one just when clause,  
avoid to have always the same when: a == b for every task.

Thank you

---

<div class="post-metadata">

**Author:** ![Paul\_Durivage](https://avatars.discourse-cdn.com/v4/letter/p/94ad74/32.png) [@Paul\_Durivage](https://forum.ansible.com/u/Paul_Durivage)\
**Post date:** [April 10, 2014, 4:09pm UTC](https://forum.ansible.com/t/using-a-sigle-when-for-multiple-tasks/14977/2 "2014-04-10T16:09:06Z")

</div>

Using a role is the way to go here. Yes, you’ll need to physically crate the role and use 3 files, but conditionally running the role with a “when” statement is the only way, I think, that you can avoid writing “when: a == b” 10 times.

Why do you need 3 files for your role? You said it was simple, so I think you’d only have one file in the role/tasks folder.

---

<div class="post-metadata">

**Author:** ![Stephan\_S](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@Stephan\_S](https://forum.ansible.com/u/Stephan_S)\
**Post date:** [April 15, 2014, 7:49am UTC](https://forum.ansible.com/t/using-a-sigle-when-for-multiple-tasks/14977/3 "2014-04-15T07:49:52Z")

</div>

This will require again an extra file for the role. I was hoping to do everything inside a single playbook.

Thank you very much for your reply.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 15, 2014, 9:31pm UTC](https://forum.ansible.com/t/using-a-sigle-when-for-multiple-tasks/14977/4 "2014-04-15T21:31:56Z")

</div>

Alternatively, the group\_by module is a great way to create a dynamic group of hosts where something is true, and then apply tasks only to that group of hosts.
