How to run shell scripts on remote machine in ansible?

How to run shell scripts on remote machine in ansible?

In this page, We are going to see how to upload shell scripts to remote machine and run shell scripts on remote machine in ansible playbook.

synchronize ansible module will be used to upload the shell scripts to remote machine from local machine (controller machine) using push mode and delegate_to.

Using command ansible module, running shell scripts on remote machines and reading generated output file content and displaying via ansible tasks.

- name: Copy and Run shell scripts on remote machine
    - name: "creating directory"
    	state: directory
    	path: /home/user1/test_shell
    	owner: user1
    	group: user1
    	mode: 0755

    - name: upload shell scripts
    	mode: push
    	dest: /home/user1/test_shell//
    	src: /home/user2/ansible/test_shell/
       delegate_to: "localhost"

    - name: run
       shell: ". /home/user1/test_shell/"

    - name: read file content
      command: "cat /home/user1/shell_file"
       register: out

    - name: Print the msg
    	 msg: "{{ out.stdout }}"

Above ansible playbook, creates directory and upload shell scripts under directory '/home/user2/ansible/test_shell' on remote machine.

Once shells scripts are executed on remote machine, output file is generated under '/home/user1/shell_file' on remote machine.

Lets see the shell scripts '' to get host name of the machine,


echo $(hostname -f) > shell_file


$ ansible-playbook run-shell-scripts-on-remote.yml

PLAY [Copy and Run shell scripts on remote machine] *************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************
[DEPRECATION WARNING]: Distribution centos 8.4 on host should use /usr/libexec/platform-python, but is using /usr/bin/python for backward compatibility with
prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See for more information. This feature will be removed in version 2.12. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
ok: []

TASK [creating directory] ***************************************************************************************************************************************************
changed: []

TASK [upload shell scripts] **************************************************************************************************************************************************
changed: [ -> localhost]

TASK [run] *************************************************************************************************************************************************
changed: []

TASK [read file content] ****************************************************************************************************************************************************
changed: []

TASK [Print the msg] ********************************************************************************************************************************************************
ok: [] => {
	"msg": "mymachine-0.localdomain"

PLAY RECAP ******************************************************************************************************************************************************************          	: ok=6	changed=4	unreachable=0	failed=0	skipped=0	rescued=0	ignored=0   

Ansible playbook is worked as expected like uploaded shell scripts and executed shell scripts on remote machine and got the above mentioned output.

Python installation

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter