I need to run a script using Ansible on multiple nodes. I need to provide the script from command line and it will be changing. The script will also have special characters.
I tried to copy the content in a file using --extra-vars
. But as there is a single quote '
in the script, the script is failing.
The content of the script:
---
- hosts: all
become: true
tasks:
- name: copy extra variables to a file
copy:
content: |
{{ command }}
dest: /tmp/commands.sh
delegate_to: localhost
- name: copy commands.sh to nodes
copy:
src: /tmp/commands.sh
dest: /tmp/
mode: 0755
- name: run the commands in the nodes
shell: /bin/bash /tmp/commands.sh
register: command_output
- name: print output
debug:
msg: "{{ command_output.stdout_lines }}"
Lets say I am giving the command as below
ansible-playbook -i hosts.ini store-in-file-and-run-in-nodes.yaml -e "command='
date
ps -ef | grep httpd
echo "That's all folks"
'
"
Here due to single quote in echo "That's all folks", the command is failing. It works fine if I escape the singe quote. It looks easy to escape a single '
as per this script. But my original script has multiple '
also special chars.
The commands I provide in command line should be stored as it is ...
Please help me to find a solution.
Thanks