# "when" attribute in a play

**URL:** <https://forum.ansible.com/t/when-attribute-in-a-play/34357>\
**Category:** Ansible Project\
**Created:** [December 18, 2020, 9:01pm UTC](https://forum.ansible.com/t/when-attribute-in-a-play/34357 "2020-12-18T21:01:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alan\_Evangelista](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@Alan\_Evangelista](https://forum.ansible.com/u/Alan_Evangelista)\
**Post date:** [December 18, 2020, 9:01pm UTC](https://forum.ansible.com/t/when-attribute-in-a-play/34357/1 "2020-12-18T21:01:52Z")

</div>

Today I needed to add a condition to a play in my playbook. If it is true, the tasks are executed.

I’d expect this intuitive code to work:

- hosts:all  
become:yes  
when:   
tasks:  
  
…  

Unfortunately, “when” is not a valid attribute of the play and I’m required to do it in a less intuitive way: e.g. replicate the when attribute for all tasks (worst solution), move all tasks to a single block of tasks and assign a when attribute to it or moving the play to a separate yaml and conditionally include it. All these solutions seem more cumbersome than just setting the when attribute to a play. Is there a reason why it is not supported?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Joseph\_Alexander](https://avatars.discourse-cdn.com/v4/letter/j/6a8cbe/32.png) [@Joseph\_Alexander](https://forum.ansible.com/u/Joseph_Alexander)\
**Post date:** [December 18, 2020, 9:11pm UTC](https://forum.ansible.com/t/when-attribute-in-a-play/34357/2 "2020-12-18T21:11:56Z")

</div>

That’s a really hard question to answer. My question to you would be why can’t you just block it?

Regards,

Joe.

---

<div class="post-metadata">

**Author:** ![Michael\_M1](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@Michael\_M1](https://forum.ansible.com/u/Michael_M1)\
**Post date:** [December 18, 2020, 9:41pm UTC](https://forum.ansible.com/t/when-attribute-in-a-play/34357/3 "2020-12-18T21:41:19Z")

</div>

you can always create a main.yml in your role, then just conditionally import entire playbooks (like ‘some\_playbook.yml’ in this case) such as:

$ cat main.yml

- import\_tasks: some\_playbook.yml

when: inventory\_hostname\_short|regex\_search(‘host1|otherhost2’)

Or any other conditional that works with ‘when’

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 18, 2020, 11:32pm UTC](https://forum.ansible.com/t/when-attribute-in-a-play/34357/4 "2020-12-18T23:32:47Z")

</div>

Test the condition in the first task. For example

&nbsp;&nbsp;- hosts: all  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- meta: end\_play  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: not condition|default('false')|bool  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: Start play
