1

[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

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

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

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.

2
  • 2
    Can you post the exact error message? Commented Feb 5, 2016 at 13:12
  • @udondan, The syntax error was due to some other issue, I corrected it. Edited the question. I am trying to pass multiple arguments but it is considering that string as single argument. Please help; Commented Feb 8, 2016 at 5:53

1 Answer 1

6

But it is considering that string of arguments as a single argument.

I think that makes sense since you pass the args in quotes. Did you try without quotes?

- name: Executing all files.
  command: sh "{{item.filename}}" {{item.args}}
  with_items: scripts_to_execute
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.