0

The string parameter in jenkins dest_cmd works fine and gets passed and read by ansible as below:

Jenkins pipeline script:

ansible-playbook -i /web/runcmd/allmwhosts.hosts /web/runcmd/copyfiles.yml -e dest_user=$dest_user -e '{ dest_cmd: $dest_cmd }' --tags validate"

The above works fine for all dest_cmd parameters however, it fails when the user enters single quotes ' as you can see below:

[Pipeline] sh
+ ansible-playbook -i /web/runcmd/allmwhosts.hosts /web/runcmd/copyfiles.yml -e dest_user=wluser -e '{ dest_cmd: arp `hostname` | cut -d' ' -f4 }' --tags validate
usage: ansible-playbook [-h] [--version] [-v] [-k]
                        [--private-key PRIVATE_KEY_FILE] [-u REMOTE_USER]
                        [-c CONNECTION] [-T TIMEOUT]
                        [--ssh-common-args SSH_COMMON_ARGS]
                        [--sftp-extra-args SFTP_EXTRA_ARGS]
                        [--scp-extra-args SCP_EXTRA_ARGS]
                        [--ssh-extra-args SSH_EXTRA_ARGS] [--force-handlers]

Can you please suggest how to resolve this issue?

Ashar
  • 2,942
  • 10
  • 58
  • 122

1 Answers1

0

The problem is the use of quotes within the single-quoted string:

'{ dest_cmd: arp `hostname` | cut -d' ' -f4 }'
                     problem quotes ^ ^

Try using double quotes:

'{ dest_cmd: arp `hostname` | cut -d" " -f4 }'
JR.
  • 311
  • 1
  • 13
  • agreed but what if the user has to enter both single and or double quotes in the command? Any solution for such a scenario ? – Ashar Feb 10 '22 at 18:17
  • Single quotes and double quotes work very differently in shell scripting. Generally I recommend using single quotes only when absolutely necessary to avoid ambiguity. A pretty good explanation of the differences is here: https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash – JR. Feb 10 '22 at 18:26
  • I m asking about a situation where the single user command contains both single as well as double quotes? What would be the solution in that case? – Ashar Feb 15 '22 at 05:33
  • That's a different ask than the original question. I still recommend that it is best to rewrite your shell commands to avoid using both types of quotes. – JR. Feb 16 '22 at 17:36