How to run python scripts on remote machines in ansible?

How to run python scripts on remote machines in ansible?

In this page, We are going to see how to run any python scripts on remote machines in ansible playbook.

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

Using command ansible module, running python scripts on remote machines and reading generated output content and displaying in ansible tasks.

---
- name: Run python scripts in remote machine
  hosts: 121.68.1.51
  tasks:
    - name: "creating directory"
      file:
    	state: directory
    	path: /home/user1/test_python
    	owner: user1
    	group: user1
    	mode: 0750

    - name: copy python scripts
      synchronize:
    	mode: push
    	dest: /home/user1/test_python//get_host_name.py
    	src: /home/stack/ansible/get_host_name.py
      delegate_to: "localhost"

    - name: run get_host_name.py
      command: python /home/user1/test_python/get_host_name.py

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

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

Above ansible playbook, creates directory and copy's python scripts under created directory '/home/user1/test_python' on remote machines.

Once python scripts are executed on remote machines, the output file is generated under '/home/user1/test_file' on the remote 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/user1/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 invoke-python-scripts.yml

PLAY [Run python scripts in remote machine] *********************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************
[DEPRECATION WARNING]: Distribution centos 8.4 on host 121.68.1.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: [121.68.1.51]

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

TASK [copy python scripts] **************************************************************************************************************************************************
changed: [121.68.1.51 -> localhost]

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

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

TASK [Print the msg] ********************************************************************************************************************************************************
ok: [121.68.1.51] => {
	"msg": {
    	"hostname": "b'machine-0.localdomain\\n'"
	}
}

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

Ansible playbook worked as expected on a remote 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
^