Ansible, copy script, then execute in remote machine
Ansible, copy script, then execute in remote machine
https://stackoverflow.com/questions/57810779/ansible-copy-script-then-execute-in-remote-machine
Asked 6 years, 3 months ago Modified 3 months ago Viewed 13k times 3
I want to copy a script to a remote server and then execute it. I can copy it to the directory /home/user/scripts, but when I run ansible script, it returns Could not find or access ‘/home/user/scripts/servicios.sh’
The full error output is:
fatal: [192.168.1.142]: FAILED! => {“changed”: false, “msg”: “Could not find or access ‘/home/user/scripts/servicios.sh’”} Here is the ansible playbook
-
name: correr script hosts: all tasks: - name: crear carpeta de scripts file: path: /home/user/scripts state: directory
- name: copiar el script copy: src: /home/local/servicios.sh dest: /home/user/scripts/servicios.sh - name: ejecutar script como sudo become: yes script: /home/user/scripts/servicios.sh
bashansible Share Improve this question Follow edited Sep 5, 2019 at 19:02 Shubham Vaishnav’s user avatar Shubham Vaishnav 1,74088 silver badges1818 bronze badges asked Sep 5, 2019 at 17:54 Juan.’s user avatar Juan. 79255 gold badges1212 silver badges2424 bronze badges Add a comment 2 Answers Sorted by:
Highest score (default) 5
You don’t need to create a directory and copy the script to target (remote node), the script module does that for you. It takes the script name followed by a list of space-delimited arguments. The local script at path will be transferred to the remote node and then executed. The script will be processed through the shell environment on the remote node. You were getting the error because script module expects the path /home/user/scripts/servicios.sh on your Ansible controller (the node where you are running the playbook from). To make it work you can specify correct path (/home/local/servicios.sh) in script task instead of /home/user/scripts/servicios.sh which is the path on the remote node. So you can change the playbook like this: You can also register the result of that command as a variable if you would like to see that.
- name: correr script
hosts: all
become: yes
tasks:
-
name: ejecutar script como sudo script: /home/local/servicios.sh register: console
-
debug: msg=”{{ console.stdout }}”
-
debug: msg=”{{ console.stderr }}” What if don’t want to go for script module and you are interested in creating a directory and copy the script to target (remote node) explicitly, and run it? No worries, you can still use the command module like this:
-
- name: correr script
hosts: all
become: yes
tasks:
-
name: crear carpeta de scripts file: path: /home/user/scripts state: directory
-
name: copiar el script copy: src: /home/local/servicios.sh dest: /home/user/scripts/servicios.sh
-
name: ejecutar script como sudo command: bash /home/user/scripts/servicios.sh register: console
-
debug: msg=”{{ console.stdout }}”
-
debug: msg=”{{ console.stderr }}” But I strongly recommend to go for script module.
-
Share Improve this answer Follow edited Sep 5, 2019 at 23:35 answered Sep 5, 2019 at 23:22 1218985’s user avatar 1218985 8,11222 gold badges2929 silver badges3333 bronze badges Sign up to request clarification or add additional context in comments.
2 Comments
RicHincapie Over a year ago Definitely, script module is shorter and lees prone to error.
Coddy Over a year ago Which directory does it copy the script to on the remote server. Can we change it and run it from a specific directory? 1
The script tag itself transfers the script from the local machine to remote machine and executes it there.
So, the path specified in the script module is of the local machine and not the remote machine i.e., /home/local/servicios.sh instead of /home/user/scripts/servicios.sh
As you have specified the path which is supposed to be on the remote machine, ansible is unable to find that script on local machine at the given path which results in the given error.
Hence, update the path in the task to local path as shown below,
- name: Run script via sudo become: yes script: /home/local/servicios.sh So scripts cant be executed inside the remote server and should be executed via local machine to the remote?
@thrash3d No, it is not like that. When you use script tag the script is transferred to the remote machine and then it is executed there. If there is a script which you don’t want to put on your remote machine and just want to execute it then you can use script tag.
If you want that script on your remote machine then you can first copy your script on remote machine and then execute it there.
Both ways are correct and it is up to you which case suits you better.
Share Improve this answer Follow edited Sep 4 at 18:50 slm’s user avatar slm 16.6k1313 gold badges119119 silver badges131131 bronze badges answered Sep 5, 2019 at 19:13 Shubham Vaishnav’s user avatar Shubham Vaishnav 1,74088 silver badges1818 bronze badges 2 Comments
Juan. Over a year ago So scripts cant be executed inside the remote server and should be executed via local machine to the remote?
Shubham Vaishnav Over a year ago @thrash3d I have updated the answer according to your query.