【发布时间】:2020-12-27 06:47:04
【问题描述】:
我正在尝试生成一个脚本,该脚本将在带有ssh 的ansible 期望/响应块中复制到远程主机。我想避免编写中间文件。
我有两个问题:
- Ansible 似乎在
command中进行了一些处理,这会弄乱多行bash脚本,但我可以使用/bin/bash -c 'my \ multi \ line \ command'解决它(在实际多行上) - 我似乎无法在
bash调用中正确在 中为heredoc 引用EOF 标记(见下文)
- 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
(请注意,在此示例中,我显然没有在单引号中使用单引号,但其他变体也都失败了。)
我试图以不同的方式转义第一个 EOF 周围的引号,但我总是收到警告:
"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: "]
并且myscript.sh 的内容要么没有正确地保留(即所有$... 扩展),要么包含最后一个EOF(因为它不被识别为分隔符并且只是读取到命令的末尾阻止,因此发出警告。
处理这个问题的正确方法是什么?
(请注意,我委托给localhost,因为我不想依赖目标主机上的python,这些是只有ssh 的最小系统。
【问题讨论】:
-
确保
EOF行的缩进只有TAB 字符,而不是空格。 -
那很快 ;) 不幸的是,这给了:
Syntax Error while loading YAML. found a tab character where an indentation space is expected -
如果 YAML 不允许使用制表符,则不能使用缩进的
EOF令牌。
标签: bash ansible escaping heredoc