Automating tasks across multiple servers can be efficiently handled using Ansible. One of the powerful features Ansible offers is the ability to execute shell scripts on remote servers using the ansible.builtin.script module. It allows you to run custom scripts on remote machines as part of your automation tasks. This is useful when you have specific commands or scripts that can’t be easily handled by Ansible’s built-in modules.
This guide will explain how to use Ansible script module to execute a shell script on a remote server with practical examples.
Table of Contents
Basic Syntax
The ansible.builtin.script module allows you to execute local scripts on remote nodes. The script is first transferred to the remote node and then executed there.
Here’s the basic syntax for using the ansible.builtin.script module:
- name: Run a local script on the remote host
ansible.builtin.script:
cmd: /path/to/local/script.sh
Explanation.
- name: A description of the task.
- ansible.builtin.script: The module used to execute a script.
- cmd: The path to the script you want to run on the remote host. This should be a path to a script file on the local machine where Ansible is running.
Example 1: Executing a Simple Shell Script
Let’s start with a simple example where we execute a basic shell script on a remote server.
Create a simple shell script named hello_world.sh:
#!/bin/bash
echo "Hello, World!"
Make the script executable:
# chmod +x hello_world.sh
Create an Ansible playbook named execute_script.yml:
---
- name: Execute a shell script on a remote server
hosts: all
become: yes
tasks:
- name: Run hello_world.sh script
ansible.builtin.script:
cmd: ./hello_world.sh
register: script_output
- name: Print script output
debug:
var: script_output.stdout
Execute the playbook with the following command:
# ansible-playbook execute_script.yml
After running the playbook, you should see the output from the script:
Example 2: Passing Arguments to the Script
Sometimes, you may need to pass arguments to your script. Here’s how you can do that.
1. Create a script named greet_user.sh:
#!/bin/bash
echo "Hello, $1!"
2. Make the script executable:
# chmod +x greet_user.sh
3. Create an Ansible playbook named execute_script_with_args.yml:
---
- name: Execute a shell script with arguments on a remote server
hosts: all
become: yes
tasks:
- name: Run greet_user.sh script with argument
ansible.builtin.script:
cmd: ./greet_user.sh "Ansible User"
register: script_output
- name: Print script output
debug:
var: script_output.stdout
4. Execute the playbook with the following command:
# ansible-playbook execute_script_with_args.yml
After running the playbook, you should see the output from the script:
TASK [Print script output] ******************************************************************
ok: [remote_server] => {
"script_output.stdout": "Hello, Ansible User!"
}
Example 3: Conditional Execution Based on Script Output
You can use the script module to run the script and then use the when statement to perform actions based on the script’s result.
1. Create a script named check_service.sh:
#!/bin/bash
service_name=$1
if systemctl is-active --quiet $service_name; then
echo "$service_name is running"
else
echo "$service_name is not running"
exit 1
fi
2. Use the chmod command to make the script executable:
# chmod +x check_service.sh
3. Create an Ansible playbook named execute_script_with_conditional.yml:
---
- name: Execute a shell script with conditional actions on a remote server
hosts: all
become: yes
tasks:
- name: Check if a service is running
ansible.builtin.script:
cmd: ./check_service.sh sshd
register: script_output
ignore_errors: yes
- name: Print script output
debug:
var: script_output.stdout
- name: Handle service not running
ansible.builtin.command:
cmd: systemctl start sshd
when: script_output.rc != 0
4. Execute the playbook with the following command:
# ansible-playbook execute_script_with_conditional.yml
After running the playbook, you should see the output from the script, and if the service is not running, Ansible will attempt to start it:
TASK [Print script output] ******************************************************************
ok: [remote_server] => {
"script_output.stdout": "sshd is not running"
}
TASK [Handle service not running] ************************************************************
changed: [remote_server]
Conclusion
In this guide, we covered how to use the Ansible script module to execute shell scripts on remote servers. We explored basic execution, passing arguments. By leveraging this module, you can automate the execution of complex scripts across multiple servers.
FAQs
1. Do I need to copy the script to the remote server before using the script module?
No, the script module automatically copies the script to the remote server and executes it, making the process seamless.
2. How do I ensure that a script executes with elevated privileges using the script module?
Use the become: yes parameter to execute the script with elevated privileges, such as sudo.
3. What is the Ansible script module used for?
The Ansible script module is used to execute local scripts on remote servers.