ansible 原始多命令

ansible raw multiple commands

我有一组LANCOM WiFi AP。 他们支持 SSH,但他们有自定义界面(没有 python)。

当我尝试 运行ning Ansible 的 command shellscript 模块时,我得到一个很长的错误。不过,它可以与 raw 模块一起使用。

我有一个很长的脚本,必须 运行 全部在一个 SSH 会话中。有没有办法让 ansible 将 .txt 文件通过 playbook 中的原始模块传输到一组路由器?

我的理解是原始模块基本上只是对目标主机执行纯 ssh 调用,在 ssh 命令行上传递任何参数。所以没有办法将文本文件通过管道传输到 ansible 中的命令。但是,您可以通过将文件读入一个 ansible 变量然后将其传递给原始命令来做非常相似的事情。沿着这些线的东西:

- hosts: waps
  vars:
      command_string: "{{ lookup('file', '/path/to/commands.txt') }}"
  tasks:
      raw: "{{ command_string }}"

可能需要一些努力才能使文本文件格式正确,但我看不出有什么原因不起作用。

编辑:这是我刚刚成功进行的测试 运行。首先,我创建了一个包含以下内容的 commands.txt 文件:

rm -f /tmp/foo.txt
echo one >> /tmp/foo.txt
echo two >> /tmp/foo.txt
if [ -f /tmp/foo.txt ] ; then
  echo three >> /tmp/foo.txt
fi

我的剧本与上图几乎完全相同:

- hosts: testhost
  user: test-user
  vars:
    command_string: "{{ lookup('file', '/tmp/commands.txt') }}"

  tasks:
    - debug: var=command_string

    - raw: "{{ command_string }}"

当我 运行 上面的内容时,它在包含以下文本的测试主机上创建了文件 /tmp/foo.txt:

one
two
three 

可能是因为 ansible 要求 python-simplejson 可用于执行除 rawscript 之外的所有模块。所有其他的都需要 python 安装以及 python-simplejson

只需在主机和任务之间添加 "gather_facts: False"。 (来自:https://groups.google.com/forum/#!topic/ansible-project/WP4XM_7PUz0