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
  hosts: 128.23.4.51
  tasks:
    - name: "creating directory"
      file:
    	state: directory
    	path: /home/user1/test_shell
    	owner: user1
    	group: user1
    	mode: 0755

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

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

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

    - name: Print the msg
       debug:
    	 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 'get_host_name.sh' to get host name of the machine,

#!/bin/bash

echo $(hostname -f) > shell_file

Output:

$ 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 128.23.4.51 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
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html 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: [128.23.4.51]

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

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

TASK [run get_host_name.sh] *************************************************************************************************************************************************
changed: [128.23.4.51]

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

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

PLAY RECAP ******************************************************************************************************************************************************************
128.23.4.51          	: 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
^