Reputation: 401
I'm trying to write a bash script that will invoke ansible playbook with extra-vars. Some of this vars is an array of strings with spaces. So i am very confused in how to pass them correctly. I already tried many combinations of quotes and slashes. So I came here for help.
ansible-playbook -i inventory.yml playbook.yml --extra-vars \
'username="${login}" fullname="${username}" password="${password}" groups="['Users','Remote Desktop Users']"';
Upvotes: 1
Views: 5386
Reputation: 401
It seems that variable name 'groups' is reserved by ansible. I changed name, and script starts working. The answer of Andrew Vickers is also correct.
Upvotes: 1
Reputation: 2654
This is what you need to know about bash variables and quoting:
For the following examples, the variable ${text}
is set to Hello
:
"${text}"
=> Hello
'${text}'
=> ${text}
"'${text}'"
=> 'Hello'
and '"${text}"'
=> "${text}"
"\"${text}\""
=> "Hello"
and '\'${text}\''
=> '${text}'
With all that said, in your case, you want the variables to be expanded, so you should enclose the entire --extra-vars value in double quotes. According to the Ansible website, the value of each of these extra variables does not need to be quoted, unless it contains spaces. To be safe, you can quote the variables as you might not be able to control their values.
Try this. I have added extra line breaks to make the code easier to understand:
ansible-playbook -i inventory.yml playbook.yml --extra-vars \
"username='${login}' \
fullname='${username}' \
password='${password}' \
groups=['Users','Remote Desktop Users'] \
"
Upvotes: 4