I\'m very new to Ansible (2.x) and I am having trouble using the script module and passing parameters with double quotes and backslashes.
Assuming we have a set vari
Don't be confused by the fact that ansible-playbook prints debug messages in JSON encoded form, so some characters are escaped.
set_fact:
arg: \(-name "{{foo}}" \)
You have correct syntax. This will set arg
to \(-name "bar" \)
if foo
's value is bar
.
But the debug message in this case will look like this:
ok: [localhost] => {
"arg": "\\(-name \"bar\" \\)"
}
Note that special characters for JSON ("
and \
) are escaped.
But there may be some issues with passing this as parameter.
If you call your script like this
script: path/somescript.sh "{{arg}}"
Parameter string will look like this "\(-name "bar" \)"
which is actually 3 concatenated strings in bash: \(-name
+bar
+ \)
, so you will lose double quotes around the bar.
If you want to preserve those double quotes, use:
script: path/somescript.sh '{{arg}}'