# Lookup if a value is in a dictionary then perform a task (using when/conditional)

**URL:** <https://forum.ansible.com/t/lookup-if-a-value-is-in-a-dictionary-then-perform-a-task-using-when-conditional/17115>\
**Category:** Ansible Project\
**Created:** [December 19, 2014, 11:46pm UTC](https://forum.ansible.com/t/lookup-if-a-value-is-in-a-dictionary-then-perform-a-task-using-when-conditional/17115 "2014-12-19T23:46:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cody\_Robertson](https://avatars.discourse-cdn.com/v4/letter/c/ad7895/32.png) [@Cody\_Robertson](https://forum.ansible.com/u/Cody_Robertson)\
**Post date:** [December 19, 2014, 11:46pm UTC](https://forum.ansible.com/t/lookup-if-a-value-is-in-a-dictionary-then-perform-a-task-using-when-conditional/17115/1 "2014-12-19T23:46:35Z")

</div>

Hello,

Hopefully I’m not overlooking this but is there a way to lookup if a value is in a dictionary - if it is perform a task using the “when” keyword? For example I want to have a certain task only run if the {{ ansible\_fqdn }} is in the dictionary.

licensed\_hosts:

- [test001.example.com](http://test001.example.com)
- [test002.example.com](http://test002.example.com)

tasks:

- debug: msg=“This should only run on [test002.example.com](http://test002.example.com)”  
when: \<somehow lookup {{ ansible\_fqdn }} in licensed\_hosts\>

Thanks!

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 20, 2014, 12:35am UTC](https://forum.ansible.com/t/lookup-if-a-value-is-in-a-dictionary-then-perform-a-task-using-when-conditional/17115/2 "2014-12-20T00:35:40Z")

</div>

First, licensed\_hosts is actually a list, not a dictionary, the dash  
(-) signifies 'list item'

Second, you don't need to lookup ansible\_fqdn as that should be part  
of fact gathering which isnormally automatic, so this should "just  
work":

tasks:  
&nbsp;&nbsp;- debug: msg="This should only run on test002.example.com"  
&nbsp;&nbsp;&nbsp;&nbsp;when: "{{ ansible\_fqdn in licensed\_hosts}}"

---

<div class="post-metadata">

**Author:** ![Cody\_Robertson](https://avatars.discourse-cdn.com/v4/letter/c/ad7895/32.png) [@Cody\_Robertson](https://forum.ansible.com/u/Cody_Robertson)\
**Post date:** [December 20, 2014, 12:46am UTC](https://forum.ansible.com/t/lookup-if-a-value-is-in-a-dictionary-then-perform-a-task-using-when-conditional/17115/3 "2014-12-20T00:46:36Z")

</div>

Hi Brian,

My verbiage is pretty poor when it comes to YAML - thanks for clarifying!

Since ansible\_fqdn is automatically gathered I was referring to looking to see if that variable / fact was in a list. Your example worked perfectly! I’ll make sure to try to look into the Jinja2 expressions a bit further in the future.

Thanks a ton!
