Running script with arguments through ansible

Sha*_*a99 0 ansible ansible-playbook ansible-2.x

[Ansible version == 2.1.0]

In order to run a script which is present locally on the target server, we can use Ansible's "command" module. Following can be done easily:

- name: Executing getpkgs.sh to download packages.
  command: sh "/path/to /dir/scriptName.sh" arg1 arg2 arg3 arg4
Run Code Online (Sandbox Code Playgroud)

I have my script names and the arguments stored in ansible variables. For example, the following variable contains all the script names and the arguments to be passed to those scripts:

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"}
Run Code Online (Sandbox Code Playgroud)

And i want all these files which are already present on the target server, to be executed using with_items. Trying to achieve something like the following:

- name: Executing all files.
  command: sh "{{item.filename}}" "{{item.args}}"
  with_items: scripts_to_execute
Run Code Online (Sandbox Code Playgroud)

I am trying to pass the script name followed by the string containing all arguments that are to be passed into the script. But it is considering that string of arguments as a single argument.

udo*_*dan 5

但是,它正在考虑将该参数字符串视为单个参数。

我认为这很有意义,因为您在引号中传递了参数。您尝试不使用引号了吗?

- name: Executing all files.
  command: sh "{{item.filename}}" {{item.args}}
  with_items: scripts_to_execute
Run Code Online (Sandbox Code Playgroud)