通过ansible运行带参数的脚本

通过ansible运行带参数的脚本,ansible,ansible-playbook,ansible-2.x,Ansible,Ansible Playbook,Ansible 2.x,[Ansible版本==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_

[Ansible版本==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_项执行。尝试实现以下目标:

- name: Executing all files.
  command: sh "{{item.filename}}" "{{item.args}}"
  with_items: scripts_to_execute
我试图传递脚本名,后跟包含要传递到脚本中的所有参数的字符串。但它将该参数串视为单个参数

但它将该参数串视为单个参数

我认为这是有道理的,因为你用引号传递参数。你试过不加引号吗

- name: Executing all files.
  command: sh "{{item.filename}}" {{item.args}}
  with_items: scripts_to_execute
但它将该参数串视为单个参数

我认为这是有道理的,因为你用引号传递参数。你试过不加引号吗

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

你能发布准确的错误消息吗?@udondan,语法错误是由于其他问题,我更正了它。编辑了这个问题。我试图传递多个参数,但它将该字符串视为单个参数。请帮忙;你能发布准确的错误消息吗?@udondan,语法错误是由于其他问题,我更正了它。编辑了这个问题。我试图传递多个参数,但它将该字符串视为单个参数。请帮忙;