Multi-conditional when statements

26 views
Skip to first unread message

John Petro

unread,
May 30, 2024, 10:32:49 AMMay 30
to ansible...@googlegroups.com
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

Brian Coca

unread,
May 30, 2024, 12:28:56 PMMay 30
to ansible...@googlegroups.com
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.

--
----------
Brian Coca (he/him/yo)

John Petro

unread,
May 30, 2024, 1:22:19 PMMay 30
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CACVha7fDShTB-g-68uo%3DnMAqZXOVM2Oq3i%2BWUsGgM-nGXSNiRg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages