Ansible回复文件

Rin*_*ing 6 yaml ansible

我在这里遇到一些Ansible/YAML语法.如何将多行(附加模式)回显到文件中?我也不能使用复制模块(带内容arg),因为它必须附加.

这段代码:

- name: Write backup script for each app
  shell: echo | '
      line one
      line two
      line three
      ' >> /manager/backup.sh
Run Code Online (Sandbox Code Playgroud)

荒谬的错误:

"stderr": "/bin/sh:  line one line two line three : command not found"
Run Code Online (Sandbox Code Playgroud)

我正在使用管道,因为我认为你告诉Ansible你想要多行(保留格式),但也许它被用作shell管道.

tec*_*raf 14

你想要这样的东西:

- name: Write backup script for each app
  shell: |
    echo 'line one
    line two
    line three' >> /manager/backup.sh
Run Code Online (Sandbox Code Playgroud)

或明确指定换行符printf:

- name: Write backup script for each app
  shell: printf 'line one\n
    line two\n
    line three\n' >> /manager/backup.sh
Run Code Online (Sandbox Code Playgroud)

您获得的错误消息非常有意义:您尝试|echo命令的输出pipe()传递给line one line two line three命令.由于shell没有找到后者,因此报告命令不存在.如果直接在shell中执行以下操作,则相同:

echo | "line one line two line three" >> /manager/backup.sh
Run Code Online (Sandbox Code Playgroud)

YAML用于|表示多行值,但在键后直接使用时,不在值字段中的任何位置.