How to download python scripts from remote machine and run python scripts in ansible?

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

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

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

Using command ansible module, running python 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_python
    	mode: 0750

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

- name: Run python scripts on local machine
  hosts: localhost
  tasks:
    - name: run get_host_name.py
       command: python3 /home/user2/ansible/test_python/get_host_name.py

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

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


Above ansible playbook, creates directory and downloads python scripts under created directory '/home/user2/ansible/test_python' on local machine.

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

Lets see the python scripts to get host name in json format file,

#!/usr/bin/env python

import json
import subprocess

if __name__ == "__main__":
    machine_details = {}
    cmd = "hostname -f"
    output = subprocess.check_output(cmd,shell=True)
    machine_details['hostname'] = str(output)
    file_path = "/home/user2/ansible/test_file"
    with open(file_path, 'w') as test_file:
    	json.dump(machine_details, test_file)


Lets run the ansible playbook 'invoke-python-scripts.yml' contains above code,

Output:

$ ansible-playbook run-python-scripts.yml

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

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

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

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

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

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

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

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

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

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 is worked as expected like donwloaded python scripts and executed python scripts on local machine and received 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
^