0

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

2

The same as in any playbook or variable file, using curly braces:

ansible all -i ,localhost -m shell -c local -e MYVAR='[test01,test02]' -a 'echo {{ MYVAR }}'

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.