# Multi-conditional when statements

**URL:** <https://forum.ansible.com/t/multi-conditional-when-statements/38799>\
**Category:** Ansible Project\
**Created:** [May 30, 2024, 2:32pm UTC](https://forum.ansible.com/t/multi-conditional-when-statements/38799 "2024-05-30T14:32:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcpetro](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jcpetro](https://forum.ansible.com/u/jcpetro)\
**Post date:** [May 30, 2024, 2:32pm UTC](https://forum.ansible.com/t/multi-conditional-when-statements/38799/1 "2024-05-30T14:32:14Z")

</div>

Good Morning Everyone!!

running into a difference of opinion between some coworkers, and I was brought in to offer my opinion as SME.

Basically there are two different “when” statements that are being used… What I am wondering is, what’s the difference between them other than maybe readability/syntax… I can’t seem to find what I am looking for when I do a search…

Case 1:

when: \>  
(  
( condition 1 ) and  
( condition 2 )  
)

Case 2:

when:

- condition 1
- condition 2

Any guidance you have about when you would use one method over the other would definitely help me out. Thanks!!

–John

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 30, 2024, 4:28pm UTC](https://forum.ansible.com/t/multi-conditional-when-statements/38799/2 "2024-05-30T16:28:16Z")

</div>

First of all, both are valid ways of writing conditionals.

> From the execution standpoint, the main difference is that the list

version will be evaluated in order, one at a time by Ansible passing  
each item to Jinja. While the other one will be passed as one item  
into Jinja. This creates a minor change in efficiency depending on the  
amount of conditions and the likelyhood of failure, but for most cases  
(less than 100 conditionals) I would consider it negligible.

> From a practical standpoint, the 2nd form is easier to put into a

variable and compose 'ANDed' conditions by adding to a list, you only  
need to ensure each condition's correctness, not the aggregated whole.  
The first form on the other hand supports 'OR' conditions also.

In the end I would consider it a preference issue, though most Ansible  
users are used to the 2nd form and might get confused by the first,  
but that is only a consideration when/if sharing the content.

---

<div class="post-metadata">

**Author:** ![jcpetro](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jcpetro](https://forum.ansible.com/u/jcpetro)\
**Post date:** [May 30, 2024, 5:21pm UTC](https://forum.ansible.com/t/multi-conditional-when-statements/38799/3 "2024-05-30T17:21:49Z")

</div>

Thanks Brian! That totally makes sense, and I hadn’t even thought about it that way. Thanks for the help, it is very much appreciated.

–John
