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