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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With