Question:
How can I evaluate (extra-)variables inside an ad-hoc command?
Is there a way to make arrays work inside ad-hoc commands?
Background: I want to ensure that multiple containers on a host are running (in my example this is "test01" and "test02")
I already found out (please correct me if I'm wrong) that I can't use an array or with_items
in adhoc commands, so that doesn't work:
ansible all -i ,localhost -m docker_container -c local -a 'name=["test01","test02"] state=started command="sleep infinity"
Now I tried, but also didn't work:
ansible all -i ,localhost -m docker_container -c local -e MYVAR='[test01,test02]' -a 'name=MYVAR state=started command="sleep infinity"
... or even ...
ansible all -i ,localhost -m shell -c local -e MYVAR='[test01,test02]' -a 'echo MYVAR'
Or is the -e
(--extra-vars
) parameter wrong?
(Environment: ansible 2.9.10 on linux (manjaro) )
The same as in any playbook or variable file, using curly braces:
Though I don't get the point of doing this, you can just use the value of the variable where you need it directly ...
This will also not help you in looping over lists, it depends entirely on the module if it supports multiple values in a parameter or not, if not you will have to implement the loop in a shell script.