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

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:

tasks:

  • debug: msg=“This should only run on test002.example.com
    when: <somehow lookup {{ ansible_fqdn }} in licensed_hosts>

Thanks!

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:
  - debug: msg="This should only run on test002.example.com"
    when: "{{ ansible_fqdn in licensed_hosts}}"

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!