find element with dictionary in Ansible and return value

hi ,

i’m trying to value of one yaml file1 with another file2 which has list of dict and if element if file1 matched with key/value of file2 then need to use value of file1 need to use as a key of file2 and return value of it…

not sure how we can leverage “when” condition or might any other way around it…

file1.yaml:

site: CL1

file2.yaml:

site_int:

The last task should be -


- debug:
msg: "{{ sites[site] }}"
when: "site in sites.keys()"

First you conflate site as both a variable and a string, showing both
examples below to make the distinction clear.

You don't need key/keys() nor quotes if 'site' is a variable

     when: site in sites

You seem to use site as both var and string, if site is a string, you
need quotes

    when: "'site' in sites"

also you can do this

     msg: "{{ sites.get(site)}}" if variable or msg: "{{
sites.get('site')}}" if string

Avoids error and shows 'none' if missing.