ansible raw multiple commands

时间:2015-05-21 13:33:25

标签: ssh ansible

我有一组LANCOM WiFi AP。 它们支持SSH,但它们有自定义接口(没有python)。

当我尝试运行Ansible的command shellscript模块时,我遇到了很长的错误。它适用于raw模块。

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

3 个答案:

答案 0 :(得分:1)

我的理解是raw模块基本上只对目标主机执行纯ssh调用,在ssh命令行上传递任何参数。所以没有办法将文本文件传递给ansible中的命令。但是,您可以通过将文件读入ansible变量然后将其传递给raw命令来执行非常类似的操作。这些方面的东西:

- 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 }}"

当我运行上面的命令时,它在testhost上创建了包含以下文本的文件/tmp/foo.txt:

one
two
three 

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

相关问题