Executing Commands on Remote Servers with the Ansible Shell Module
Ansible is a popular configuration management tool that simplifies managing multiple servers by automating tasks like server setup and application installation. It allows you to manage numerous remote servers and execute tasks sequentially from a central control node. Ansible leverages modules and integrates seamlessly over SSH, eliminating the need for extra software on target servers.
This tutorial will guide you through running commands on remote servers using the Ansible shell module. We’ll cover executing Ansible ad-hoc commands and creating playbooks to run different shell commands.
Prerequisites
Before you can use the Ansible shell module, you’ll need to ensure you have the following:
- This guide uses Ubuntu 22.04. Ansible must be installed and configured on your Ubuntu system.
- You should have a remote host configured to run commands on, using the Ansible shell module.
What Is the Ansible Shell Module?
The Ansible shell module allows you to execute commands directly on the target system’s shell. This makes it possible to run complex commands, including pipes and redirections, while maintaining the integrity of the command execution on the remote server.
When using the shell module, you provide the command name and any necessary parameters or arguments, separated by spaces. These commands are then executed on the remote host.
Difference: Ansible Shell Module vs. Command Module
The Ansible shell module and command module are similar in functionality, but there are key differences:
- The shell module supports special operators, environment variables, pipes, and redirections. The command module does not support these ( <, >, &, ;, | | ).
- The command module doesn’t allow direct interaction with the shell. The shell module provides more flexibility by allowing commands to be executed directly on the target host’s shell, using `/bin/sh` by default, though this can be configured.
- For security, the command module is often preferred. It is generally safer and offers more predictable options than the shell module. The command module doesn’t alter the user’s remote shell environment.
Getting Started with Ansible Shell Commands: Run Ad-hoc Commands
Ad-hoc commands are single-line commands, designed for immediate tasks and are not meant to be reusable. They are quick and convenient for one-off tasks. The basic syntax of ad-hoc commands is:
ansible [pattern] -m [ansible-module] -a {commands with options}
In this syntax, `pattern` is the host group. `-m` specifies the module, and `-a` provides the command arguments.
Here’s an example that displays the date and time of a remote host with the IP address “192.168.1.14,” connecting as the user “ansible”:
$ ansible 192.168.1.14 -u ansible -m shell -a "timedatectl"
This ad-hoc command connects to the specified remote host via SSH as the “ansible” user and executes the `timedatectl` command, displaying the system’s date and time settings.
$ ansible <ip-address> -u ansible -m shell -a "timedatectl | grep Time"
This command runs `timedatectl` and pipes the output to `grep Time`, filtering and displaying lines containing “Time.”
To display system uptime and load average, use the following command:
$ ansible <ip-address> -u ansible -m shell -a "uptime"
This command executes the `uptime` command after connecting to the remote host.
$ ansible <ip-address> -u ansible -m shell -a setup
This command collects system facts and information about the remote host.
How to Use the Ansible Shell Module?
Before using the shell module, you should understand the basic parameters you can use:
chdir – Changes the current directory before command execution.
cmd – The command to be executed, including any arguments.
executable – Specifies an absolute path to alter which shell is used.
removes – Excludes a step from running if a specified file doesn’t exist.
stdin – Sets the standard input (stdin) for the command.
warn – Enables or disables task warnings (default: yes).
Let’s look at some examples of how to leverage the shell module in Ansible playbooks.
Ansible Run Commands with Shell Module
Playbooks provide a structured way to perform organized and complex tasks on remote hosts. Using the shell module within a playbook provides you with control and reusability.
Run a Single Command using the Ansible Shell Module
You can easily run a single command using a playbook. Create a new playbook file and insert the following code:
- name: Shell module example hosts: database_servers tasks: - name: Check system information shell: "df -h" register: "os_info" - debug: msg: "{{"os_info".stdout_lines}}"
Save the file using `Ctrl+X`. Then, execute it with the command below.
$ ansible-playbook --ask-become-pass -i hosts environment_var.yml
After running the command, you should see output containing the file system disk space details for each target host (as returned by `df -h`).
Ansible Run Multiple Commands with Shell Module
Ansible can execute multiple tasks sequentially using playbooks. Create a new playbook, for example, with `nano`:
$ sudo nano testplaybook.yml