Issue
I want to execute following command that contains double quotes:
- name: Initializing Kubernetes cluster
shell: kubeadm init --control-plane-endpoint "hostname:port"
Don't know if it will work or not as if I check the command as follows:
- debug:
msg: kubeadm init --control-plane-endpoint "hostname:port"
It gives kubeadm init --control-plane-endpoint \"hostname:port\"
. Why output contains extra backslash between quotes? Will the command execute properly as it is or I have to add something due to double quote? (I couldn't test by executing it as it is a production server)
Solution
Q: "Why output contains extra backslash between quotes?"
A: Ansible escapes the double quotes in output to tell you the double quotes are part of the string. For example,
- shell: echo "\"This is a double-quoted string.\""
register: out
gives
msg: |-
. 123456789012345678901234567890123
out.stdout: "This is a double-quoted string."
length: 33
out.cmd: echo "\"This is a double-quoted string.\""
- It is up to you to include the double quotes. For example,
- shell: echo "This is NOT a double-quoted string."
register: out
gives
msg: |-
out.stdout: This is NOT a double-quoted string.
out.cmd: echo "This is NOT a double-quoted string."
You don't have to escape the double quotes if you use Single-Quoted Style
- shell: echo '"This is a double-quoted string."'
register: out
gives
msg: |-
. 123456789012345678901234567890123
out.stdout: "This is a double-quoted string."
length: 33
out.cmd: echo '"This is a double-quoted string."'
Example of a complete playbook for testing
- hosts: localhost
tasks:
- shell: echo "This is NOT a double-quoted string."
register: out
- debug:
msg: |
out.stdout: {{ out.stdout }}
out.cmd: {{ out.cmd }}
- shell: echo "\"This is a double-quoted string.\""
register: out
- debug:
msg: |
. 123456789012345678901234567890123
out.stdout: {{ out.stdout }}
length: {{ out.stdout|length }}
out.cmd: {{ out.cmd }}
- shell: echo '"This is a double-quoted string."'
register: out
- debug:
msg: |
. 123456789012345678901234567890123
out.stdout: {{ out.stdout }}
length: {{ out.stdout|length }}
out.cmd: {{ out.cmd }}
Q: "Do I have to add something due to double-quote (a string)?"
In the below command, the double quotes will be interpreted by the shell
- shell: kubeadm init --control-plane-endpoint "hostname:port"
If you want the double quotes to be part of the string you have either escape it
- shell: kubeadm init --control-plane-endpoint "\"hostname:port\""
, or close it in single quotes
- shell: kubeadm init --control-plane-endpoint '"hostname:port"'
Q: "Double quotes will be interpreted by the shell. Right?"
A: Right. The quotes around a script's parameters serve the only purpose to allow whitespaces inside a parameter. For example,
shell> cat test.sh
#!/usr/bin/sh
printf "$1\n"
printf "$2\n"
gives
shell> ./test.sh aaa bbb ccc
aaa
bbb
shell> ./test.sh aa a bbb ccc
aa
a
shell> ./test.sh "aa a" bbb ccc
aa a
bbb
shell> ./test.sh "aa a" "bbb" ccc
aa a
bbb
shell> ./test.sh "aaa" "\"bbb\"" ccc
aaa
"bbb"
shell> ./test.sh "aaa" '"bbb"' ccc
aaa
"bbb"
Answered By - Vladimir Botka Answer Checked By - Pedro (WPSolving Volunteer)