AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible script Module: Run Local Scripts on Remote Hosts Guide — Video Tutorial

How to run local scripts on remote hosts with Ansible script module. Execute Python, Bash, and custom scripts without copying them first. Examples included.

Watch on YouTube · Read the written article

Tutorial summary

What you'll learn

  • How to Run Python Script on Remote Machines after transferring it?
  • Run Python Script on Remote Machines
  • Parameters
  • Links
  • Demo
  • code
  • execution
  • verbosity two execution
  • Conclusion
  • Run a Python Script
How to Run Python Script on Remote Machines after transferring it? Run Python Script on Remote Machines - `ansible.builtin.script` - Runs a local script on a remote node after transferring it Let’s talk about the Ansible module `script`. The full name is `ansible.builtin.script`, which means that is part of the Ansible builtin modules included in ansible-core. The purpose of the module is to Runs a local script on a remote node after transferring it. Parameters - `cmd` string - script name or path - `executable` string - executable name or path Let me summarize the main parameters of the module `script`. This module doesn't have any required parameters bus some options become necessary in this use case. The `cmd` parameter specifies the script name or path. The `executable` parameter specifies the interpreter name or path. Links - [`ansible.builtin.script`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/script_module.html) Demo Let's jump into a real-life Ansible Playbook to Run Python Script on Remote Machines after transferring it. code - cars.py ```python #!/usr/bin/env python3 import json cars = { "manufacturers": [ "Acura", "Alfa-Romeo", "Aston-Martin", "Audi", "Bentley", "BMW", "Bugatti", "Buick", "Cadillac", "Chevrolet", "Chrysler", "Citroen", "Deus Automobiles", "Dodge", "Ferrari", "Fiat", "Ford", "Geely", "Genesis", "GMC", "Honda", "Hyundai", "Infiniti", "Jaguar", "Jeep", "Kia", "Koenigsegg", "Lamborghini", "Lancia", "Land Rover", "Lexus", "Lincoln", "Lotus", "Maserati", "Maybach", "Mazda", "McLaren", "Mercedes", "Mini", "Mitsubishi", "Nissan", "Opel", "Pagani", "Peugeot", "Pontiac", "Porsche", "Ram", "Renault", "Rolls-Royce", "Skoda", "Smart", "Subaru", "Suzuki", "Tesla", "Toyota", "Volkswagen", "Volvo" ] } print(json.dumps(cars, indent=4)) ``` - run_python_script.yml ```yaml --- - name: run Python script hosts: all tasks: - name: run cars.py script ansible.builtin.script: executable: python3 cmd: cars.py register: cars_raw_output - name: print cars_raw_output ansible.builtin.debug: var: cars_raw_output verbosity: 2 - name: convert output to JSON ansible.builtin.set_fact: cars_list: "{{ cars_raw_output.stdout | from_json }}" - name: print cars_list ansible.builtin.debug: var: cars_list ``` execution ```bash ansible-pilot $ ansible-playbook -i ../virtualmachines/demo/inventory run_python_script.yml PLAY [run Python script] ************************************************************************** TASK [Gathering Facts] **************************************************************************** ok: [demo.example.com] TASK [run cars.py script] ************************************************************************* changed: [demo.example.com] TASK [print cars_raw_output] *****************************************************

About this tutorial

  • Author: Luca Berton
  • Difficulty: Advanced
  • Read time: 8 min
  • Category: installation

Topics covered

Related video tutorials