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

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 Video

Watch "Ansible script Module: Run Local Scripts on Remote Hosts Guide" on YouTube

What You'll Learn

Full Tutorial Content

How to Run Python Script on Remote Machines after transferring it? I'm going to show you a live Playbook with some simple Ansible code. I'm Luca Berton and welcome to today's episode of Ansible Pilot. 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. I'm going to show you how to create a `cars.py` custom Python script that output a JSON file, transfers it to a remote machine, and executes it using `python3` interpreter. 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] ***

About This Tutorial

Read the full written article: Ansible script Module: Run Local Scripts on Remote Hosts Guide

Topics Covered

Related Video Tutorials