# Search key in Dictionaries and compare with variable

**URL:** <https://forum.ansible.com/t/search-key-in-dictionaries-and-compare-with-variable/37432>\
**Category:** Ansible Project\
**Created:** [December 6, 2022, 4:37pm UTC](https://forum.ansible.com/t/search-key-in-dictionaries-and-compare-with-variable/37432 "2022-12-06T16:37:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [December 6, 2022, 4:37pm UTC](https://forum.ansible.com/t/search-key-in-dictionaries-and-compare-with-variable/37432/1 "2022-12-06T16:37:34Z")

</div>

Hi, I need your help, this is my stage.

Good morning I need your help, I want to run a playbook using a console variable and compare it with a dictionary, example:

ansible-playbook --extra-vars “SERVER=new” [example.](https://example.com/)yaml

If in the console variable I type new I want to print the value of the url key belonging to new and if I type old I type the url value in old.

```auto
---
- name: "Instalando Elastic Logger"
  hosts: localhost
  gather_facts: no
  vars:
   servers:
      new: 
        url: "url-new"
      old: 
        url: "url-old"
  tasks:
    - name: "Debug"
      shell: echo '{{ url-new }} '
      when: new in dict

    - name: "Debug"
      shell: echo '{{ url-old }} '
      when: old in dict

```

any helps?

Regards,

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [December 6, 2022, 6:07pm UTC](https://forum.ansible.com/t/search-key-in-dictionaries-and-compare-with-variable/37432/2 "2022-12-06T18:07:05Z")

</div>

The expression you want is

echo ‘{{ servers[SERVERS].url }}’

But beware: Ansible variable names are supposed to be all lower-case. The above should work with “SERVERS” for now, but it’s frowned upon.

---

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [December 7, 2022, 2:13pm UTC](https://forum.ansible.com/t/search-key-in-dictionaries-and-compare-with-variable/37432/3 "2022-12-07T14:13:22Z")

</div>

Hello Todd, understand, using variables in minuscules.

i need to automate the installation of a docker plugin using ansible.

- name: “install plugin”  
hosts: localhost  
gather\_facts: no  
vars:  
new:  
url: newurl  
user: user  
pass: pass  
old:  
url: oldurl  
tasks:
- debug:  
msg: docker install pluginame HOST=‘{% if server==“new” %}{{ new.url }}{% elif server==“old” %}{{ old.url }}{% endif %}’

I don’t know if it’s the best way to handle it because the user variable and password are exposed in the playbook.

I cannot understand how to compare the “server” console variable with the url key within each array and I also need to add a conditional in each task to run it if the url key matches the value of the “server” variable.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [December 7, 2022, 5:21pm UTC](https://forum.ansible.com/t/search-key-in-dictionaries-and-compare-with-variable/37432/4 "2022-12-07T17:21:17Z")

</div>

It’s “docker plugin install …” but never mind that. You’re changing the goal on us!

First, let’s address your original question, but using “my\_selector” instead of “SERVER” as the variable name. It seems you misunderstood my answer, so I’ll spell it out completely. Then we’ll stick another task on the end that plays with the docker plugin install.
