Shasha99
Shasha99

Reputation: 1916

Which one is better ? Executing script directly from control machine or copy and execute locally?

There are following two ways to run a script on the target machine:

1.  - name: run the script from the control machine directly.
      script: "{{path_to_scripts}}/script.sh"

2.  - name: Copying the script from target machine.
      copy: src="{{path_to_scripts}}/script.sh" dest="{{path_to_scripts}}/script.sh" mode=0777

    - name: Execute script locally.
      command: /bin/sh {{path_to_scripts}}/script.sh

As i am running the playbook against more than 30 target machines. I would like to know which one will be a better choice ?

Also what is the performance penalty if i prefer one over other ?

Upvotes: 1

Views: 602

Answers (2)

Ganesan Srinivasan
Ganesan Srinivasan

Reputation: 337

If you are executing the script from the ansible machine, the ansible server will copy the script to temp location in the remote machine to execute.

So, the better choice is "run the script from the control machine directly" because of below reasons

  1. you dont need to ssh to all 30 machines for copy the scripts
  2. you can have a single line of code to do the same that needs 2 steps(copy and execute)
  3. no performance difference as both methods is doing the same operations

Upvotes: 2

shaps
shaps

Reputation: 1179

If the script has to do something on the remote machine, would be better if you copy it and execute directly on the remote. I don't think you would see any noticeable performance decrease in any of the two cases.

The only thing is that in case 1. you will have to ssh to the remote and execute the commands you need, thing that ansible already does for you.

Upvotes: 0

Related Questions