Technically when statements use jinja2 template syntax, but since that is
pretty much straight python in most cases there is a lot of similarity.


On Sat, Mar 8, 2014 at 8:43 AM, David Adams <[email protected]> wrote:

> I knew I was missing something obvious. I totally blanked that you can use
> straight Python in when. Duh. Thanks!
>
>
> On Sat, Mar 8, 2014 at 7:23 AM, Christian Thiemann 
> <[email protected]>wrote:
>
>> Regular Python should work in this case:
>>
>>   when: myvariable in mylist
>>
>> You need to quote the whole condition if it starts with a literal string,
>> though:
>>
>>   when: "'literal string' in mylist"
>>
>>
>>
>> On Saturday, March 8, 2014 1:54:24 PM UTC+1, David Adams wrote:
>>>
>>> My apologies if there's an obvious solution to this, but I've been
>>> unable to find it.
>>>
>>> I have a list variable defined with some values, eg:
>>>
>>>     mylist:
>>>       - aaa
>>>       - bbb
>>>       - ddd
>>>
>>> I need to execute a task conditionally based on whether the value of a
>>> variable is a member of that list. In other words, if myvar is set to
>>> "aaa", "bbb", or "ddd" I want to execute my task, but if it's set to
>>> anything else the task should be skipped.
>>>
>>> When I ran into this, I expected to find a Jinja filter called something
>>> like "contains" or "includes" such that I could do this:
>>>
>>>     tasks:
>>>       - debug: msg="Found the value in mylist"
>>>         when: mylist|contains(myvariable)
>>>
>>> But I haven't been able to find any filters that do this. I did come up
>>> with this alternative solution:
>>>
>>>     tasks:
>>>       - debug: msg="Found the value in mylist"
>>>         when: mylist|intersect([myvariable])|count > 0
>>>
>>> But that feels really hacky and is hard to read at a glance. Is there a
>>> Jinja filter I'm overlooking, or some better way to do this?
>>>
>>> Thanks!
>>>
>>> -dave
>>>
>>  --
>> 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 [email protected].
>> To post to this group, send email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/ansible-project/1bd3752e-fac8-4263-8cca-b0f20fe64229%40googlegroups.com<https://groups.google.com/d/msgid/ansible-project/1bd3752e-fac8-4263-8cca-b0f20fe64229%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>  --
> 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 [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAN3s8zZOdYC7OLfshs2e%2B3D5biy0G8opm7gfV1nV_9841W_uTQ%40mail.gmail.com<https://groups.google.com/d/msgid/ansible-project/CAN3s8zZOdYC7OLfshs2e%2B3D5biy0G8opm7gfV1nV_9841W_uTQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAFg%2Bn8ShBbZZrxPYU1JoaogH6mRDYRPfcLZJnwp2G21EJg69qg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to