通过ansible运行带有参数的脚本

时间:2016-02-05 13:04:38

标签: ansible ansible-playbook ansible-2.x

[Ansible version == 2.1.0]

为了运行目标服务器上本地存在的脚本,我们可以使用Ansible的“命令”模块。以下可以轻松完成:

- name: Executing getpkgs.sh to download packages.
  command: sh "/path/to /dir/scriptName.sh" arg1 arg2 arg3 arg4

我的脚本名称和参数存储在ansible变量中。例如,以下变量包含所有脚本名称和要传递给这些脚本的参数:

scripts_to_execute:
  - { filename: "/path/to/file/file1.sh", args: "arg11 arg12 arg13"}
  - { filename: "/path/to/file/file2.sh", args: "arg21 arg22"}
  - { filename: "/path/to/file/file3.sh", args: "arg31 arg32 arg33 arg34"}

我希望使用with_items执行目标服务器上已存在的所有这些文件。试图实现以下内容:

- name: Executing all files.
  command: sh "{{item.filename}}" "{{item.args}}"
  with_items: scripts_to_execute

我正在尝试传递脚本名称,后跟包含要传递给脚本的所有参数的字符串。但它正在考虑将一串参数作为一个参数。

1 个答案:

答案 0 :(得分:4)

  

但它正在考虑将一串参数作为单一参数。

我认为这是有道理的,因为你在引号中传递了args。你没有引号尝试过吗?

- name: Executing all files.
  command: sh "{{item.filename}}" {{item.args}}
  with_items: scripts_to_execute
相关问题