I'm trying to generate a script to be copied in an ansible expect/response block with ssh to the remote host. I'd like to avoid writing intermediate files.
I have two issues:
- Ansible seems to do some processing in
commandwhich messes up multilinebashscripts, but I can get around it with/bin/bash -c 'my \ multi \ line \ command'(on actual multiple lines) - I can't seem to quote the EOF mark for the heredoc correctly within the
bashinvocation (see below)
- name: Generate script on the fly
expect:
command: |
/bin/bash -c 'ssh -o PubkeyAuthentication=no -p {{ ansible_ssh_port }} {{ ansible_user }}@{{ ansible_host }} "cat - > {{ tgtdir }}/myscript.sh" <<-'EOF'
#! /bin/env sh
a="/$0"; a=${a%/*}; a=${a#/}; a=${a:-.}; THIS=$(cd "$a"; pwd)
echo "Script dir == ${THIS}"
echo "{{ someansiblevar }}"
EOF
'
responses:
(.*)password: "{{ ansible_ssh_pass }}"
delegate_to: localhost
(Note that in this example I'm apparently getting away with using single quotes within single quotes, but other variations all fail as well.)
I have tried to escape the quotes around the first EOF in different ways, but I always get the warning:
"stdout_lines": ["/bin/bash: line 10: warning: here-document at line 0 delimited by end-of-file (wanted `EOF')", "", "admin@192.168.1.111's password: "]
And the contents of myscript.sh are either not correctly left alone (i.e. all $... expanded), or contain the last EOF (since it is not recognized as delimiter and is just reading to the end of the command block, hence the warning.
What is the correct way of handling this?
(Note that I delegate to localhost because I don't want to rely on python on the target host, these are minimal systems with only ssh).