You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Ansible raw模块模拟带EOF的原始命令执行?

用Ansible raw模块模拟SSH heredoc执行命令的方案

你要模拟的SSH heredoc执行方式,用Ansible的raw模块直接就能实现,不需要额外找特性——raw模块本身就是通过SSH直接发送命令串,支持多行命令的写法。

比如你原来的SSH命令:

ssh user@host.com <<EOF
   command
   exit
EOF

对应的Ansible写法可以这样写:

- name: 远程执行多行命令
  hosts: host.com
  remote_user: user
  tasks:
    - name: 通过raw模块发送命令
      ansible.builtin.raw: |
        command

不需要手动加exit,命令执行完成后SSH会话会自动结束。如果要执行多条命令,直接在块标里换行写就行:

- name: 执行多条远程命令
  ansible.builtin.raw: |
    command1
    command2
    echo "执行完成"

要是你需要执行带逻辑的脚本,也可以把完整的shell脚本内容直接写进去:

- name: 执行复杂脚本逻辑
  ansible.builtin.raw: |
    #!/bin/bash
    for i in {1..3}; do
        echo "第$i次循环"
        sleep 1
    done

raw模块会把整个块标里的内容原封不动通过SSH发送到目标主机执行,和你用heredoc传递命令的效果完全一致。

内容的提问来源于stack exchange,提问作者soField

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 15:25:09