# How to test for an item in a list?

**URL:** <https://forum.ansible.com/t/how-to-test-for-an-item-in-a-list/14731>\
**Category:** Ansible Project\
**Created:** [March 8, 2014, 12:54pm UTC](https://forum.ansible.com/t/how-to-test-for-an-item-in-a-list/14731 "2014-03-08T12:54:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Adams](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@David\_Adams](https://forum.ansible.com/u/David_Adams)\
**Post date:** [March 8, 2014, 12:54pm UTC](https://forum.ansible.com/t/how-to-test-for-an-item-in-a-list/14731/1 "2014-03-08T12:54:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Christian\_Thiemann](https://avatars.discourse-cdn.com/v4/letter/c/f04885/32.png) [@Christian\_Thiemann](https://forum.ansible.com/u/Christian_Thiemann)\
**Post date:** [March 8, 2014, 1:23pm UTC](https://forum.ansible.com/t/how-to-test-for-an-item-in-a-list/14731/2 "2014-03-08T13:23:09Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![David\_Adams](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@David\_Adams](https://forum.ansible.com/u/David_Adams)\
**Post date:** [March 8, 2014, 1:43pm UTC](https://forum.ansible.com/t/how-to-test-for-an-item-in-a-list/14731/3 "2014-03-08T13:43:46Z")

</div>

I knew I was missing something obvious. I totally blanked that you can use straight Python in when. Duh. Thanks!

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [March 8, 2014, 3:03pm UTC](https://forum.ansible.com/t/how-to-test-for-an-item-in-a-list/14731/4 "2014-03-08T15:03:16Z")

</div>

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