Search key in Dictionaries and compare with variable

78 views
Skip to first unread message

SysAdmin EM

unread,
Dec 6, 2022, 11:38:29 AM12/6/22
to ansible...@googlegroups.com

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,

Todd Lewis

unread,
Dec 6, 2022, 1:07:05 PM12/6/22
to Ansible Project
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.

SysAdmin EM

unread,
Dec 7, 2022, 9:14:18 AM12/7/22
to ansible...@googlegroups.com
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.


Todd Lewis

unread,
Dec 7, 2022, 12:21:17 PM12/7/22
to Ansible Project
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.
---
# File: my_selector.yml
# Invoke with: ansible-playbook my_selector.yml -v --extra-vars "my_selector=new"
# or           ansible-playbook my_selector.yml -v --extra-vars "my_selector=old"
# or           ansible-playbook my_selector.yml -v --extra-vars "my_selector=borken"
# or           ansible-playbook my_selector.yml -v"
- name: "Use 'my_selector' passed in via --extra-vars"

  hosts: localhost
  gather_facts: no
  vars:
   servers:
      new:
        url: "url-new"

        user: user
        pass: pass
      old:
        url: "url-old"
  tasks:
    - name: Check for a valid 'my_selector'
      ansible.builtin.fail:
        msg: 'We don''t have a server called "{{ my_selector | default("<none>")}}".'
      when: >-
         my_selector is not defined or
         servers[my_selector] is not defined

    - name: "Use 'my_selector' in a shell"
      ansible.builtin.shell: echo '{{ servers[my_selector].url }}'

    - name: "Install a docker plugin"
      ansible.builtin.debug:
        msg: docker plugin install pluginame HOST='{{ servers[my_selector].url }}'

I did not address your issue with credentials being exposed. It's a legitimate issue, but turning on no_log would make the point of the other tasks rather difficult to see. Here's typical output:
$ ansible-playbook my_selector.yml -v --extra-vars "my_selector=new"

PLAY [Use 'my_selector' passed in via --extra-vars] *******************************

TASK [Check for a valid 'my_selector'] ********************************************
skipping: [localhost] => {"changed": false, "skip_reason": "Conditional result was False"}

TASK [Use 'my_selector' in a shell] ***********************************************
changed: [localhost] => {"changed": true, "cmd": "echo 'url-new'", "delta": "0:00:00.003195", "end": "2022-12-07 12:18:51.555607", "msg": "", "rc": 0, "start": "2022-12-07 12:18:51.552412", "stderr": "", "stderr_lines": [], "stdout": "url-new", "stdout_lines": ["url-new"]}

TASK [Install a docker plugin] ****************************************************
ok: [localhost] => {
    "msg": "docker plugin install pluginame HOST='url-new'"
}

PLAY RECAP ************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0
Reply all
Reply to author
Forward
0 new messages