0

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)

bobs
  • 55
  • 8
  • Does this answer your question? [Ansible set\_fact with double quotes and back slashes](https://stackoverflow.com/questions/68559796/ansible-set-fact-with-double-quotes-and-back-slashes) – β.εηοιτ.βε Aug 21 '23 at 15:36

1 Answers1

1

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"
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • I wnt execute ```kubeadm init --control-plane-endpoint "hostname:port"```. As you said "double quotes will be interpreted by the shell", that means ```- shell: kubeadm init --control-plane-endpoint "hostname:port"``` will do the same, right? – bobs Aug 21 '23 at 16:28
  • 1
    Right. I added examples. – Vladimir Botka Aug 21 '23 at 18:02
  • But as you said if "double quotes to be part of the string then I have to use \ or '. It confusing me. If ```"hostname:port"``` interpreted as ```hostname:port``` then answer of above comment should be "no", as double quotes are part of the command. – bobs Aug 21 '23 at 18:25