How to download shell scripts from remote machine and run shell scripts on local machine in ansible?

How to download shell scripts from remote machine and run shell scripts on local machine in ansible?

How to download shell scripts from remote machine and run shell scripts on local machine in ansible?

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

synchronize ansible module will be used to download the shell scripts from remote machine to local machine (controller machine) using pull mode.

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

---
- name: Create directory on local machine
  hosts: localhost
  tasks:
    - name: "creating directory"
      file:
    	state: directory
    	path: /home/user2/ansible/test_shell
    	mode: 0750

- name: Download shell scripts from remote machine
  hosts: 128.23.4.51
  tasks:
    - name: download shell scripts
      synchronize:
    	mode: pull
    	src: /home/user1/test_shell/get_host_name.sh
    	dest: /home/user2/ansible/test_shell/get_host_name.sh

- name: Run shell scripts on local machine
  hosts: localhost
  tasks:
    - name: run get_host_name.sh
      shell: ". /home/user2/ansible/test_shell/get_host_name.sh"

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

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



Above ansible playbook, creates directory and download shell scripts under created directory '/home/user2/ansible/test_shell' on local machine.

Once shell scripts are executed on local machine, output file is generated under '/home/user2/ansible/test_file' on local machine.

Lets see the shell scripts to get host name in output file,

#!/bin/bash

echo $(hostname -f) > shell_file


Lets run the ansible playbook 'download-shell-scripts-from-remote.yml' contains above code,

Output:

$ ansible-playbook download-shell-scripts-from-remote.yml

PLAY [Create directory on local machine] ************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************
ok: [localhost]

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

PLAY [Download shell scripts from 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 [download shell scripts] ***********************************************************************************************************************************************
changed: [128.23.4.51]

PLAY [Run shell scripts on local machine] ***********************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************
ok: [localhost]

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

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

TASK [Print the msg] ********************************************************************************************************************************************************
ok: [localhost] => {
	"msg": "mymachine-1.local"
}

PLAY RECAP ******************************************************************************************************************************************************************
128.23.4.51          	: ok=2	changed=1	unreachable=0	failed=0	skipped=0	rescued=0	ignored=0   
localhost              	: ok=6	changed=3	unreachable=0	failed=0	skipped=0	rescued=0	ignored=0  

Ansible playbook worked as expected like downloaded shell scripts and executed shell scripts on a local 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
^