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