Ansible - Run a script using the script module
by
Jeremy Canfield |
Updated: March 28 2023
| Ansible articles
If you are not familiar with modules, check out Ansible - Getting Started with Modules.
Typically, the shell, command, or script modules are used to invoke a command on a managed node (e.g. target systems).
Python (e.g. /usr/bin/python) needs to exist on the managed node to use these modules.
Python is not need to use these modules.
- script (this article)
- raw
One thing that's pretty cool about this module is that the script you want to run will be stored somewhere on your control node (that's your Ansible server) but the script will actually be executed on each target system.
---
- hosts: all
tasks:
- name: my script
script:
cmd: /path/to/your/script.sh
...
Optionally, you can include command line flags too.
---
- hosts: all
tasks:
- name: my script
script:
cmd: /path/to/your/script.sh --foo bar
...
If the script requires certain environment variables to be set, environment can be included.
---
- hosts: all
tasks:
- name: my script
script:
cmd: /path/to/your/script.sh --foo bar
environment:
LC_ALL: en_US.utf-8
...
Did you find this article helpful?
If so, consider buying me a coffee over at