Search key in Dictionaries and compare with variable

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.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.

---
- 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,

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.

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.

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.