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!!
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.