Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a shell script on a remote server using Ansible?

I am planning to execute a shell script on a remote server using Ansible playbook.

blank test.sh file:

touch test.sh 

Playbook:

--- - name: Transfer and execute a script.   hosts: server   user: test_user   sudo: yes   tasks:      - name: Transfer the script        copy: src=test.sh dest=/home/test_user mode=0777       - name: Execute the script        local_action: command sudo sh /home/test_user/test.sh 

When I run the playbook, the transfer successfully occurs but the script is not executed.

like image 640
Pattu Avatar asked Jan 16 '14 11:01

Pattu


People also ask

Can Ansible run Shell scripts?

Though Ansible Shell module can be used to execute Shell scripts. Ansible has a dedicated module named Script which can be used to copy the Shell script from the control machine to the remote server and to execute. Based on your requirement you can use either Script or Shell module to execute your scripts.

How do I use Ansible playbook on remote host?

It is possible to have ansible installed on the remote machine and run it there, not just from your local machine connecting to the remote machine. Your hosts file will need to use localhost , and whenever you run playbooks with ansible-playbook -i hosts playbook. yml , you will need to add -c local to your command.


1 Answers

you can use script module

Example

- name: Transfer and execute a script.   hosts: all   tasks:       - name: Copy and Execute the script         script: /home/user/userScript.sh 
like image 126
Kunwar Avatar answered Oct 24 '22 02:10

Kunwar