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
    - name: "creating directory"
    	state: directory
    	path: /home/user1/test_python
    	owner: user1
    	group: user1
    	mode: 0750

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

    - name: run
      command: python /home/user1/test_python/

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

    - name: Print the msg
    	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,


$ ansible-playbook invoke-python-scripts.yml

PLAY [Run python scripts in 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 [copy python scripts] **************************************************************************************************************************************************
changed: [ -> localhost]

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

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

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

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