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
On Wednesday, December 7, 2022 at 9:14:18 AM UTC-5 [email protected] wrote:
> Hello Todd, understand, using variables in minuscules.
>
> <https://stackoverflow.com/posts/74707194/timeline>
>
> 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.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/3fc51741-503a-449e-8177-991d467c4419n%40googlegroups.com.