Ansible command Module: Running Commands on Managed Hosts


In the world of IT automation and configuration management, the command module in Ansible serves as a fundamental tool for executing ad-hoc commands on managed hosts. This module allows system administrators and DevOps professionals to run arbitrary commands, scripts, or executables on remote servers, providing the flexibility to perform a wide range of tasks. This blog post explores the usage of the command module, its parameters, and real-world examples to demonstrate its versatility and practicality.

Understanding the Concept of command Module

The command module enables Ansible users to interact with managed hosts on a granular level by executing individual commands directly. Unlike other specialized modules, command provides a way to run virtually any command available on the host, making it a powerful and flexible option for various automation tasks.

How to Use the Ansible command Module

The command module is straightforward to use, and it allows for the execution of commands and scripts on remote hosts. Let’s explore its usage through practical examples:

Syntax and Parameters

The basic syntax of the command module is as follows:

The module accepts the following parameters:

  • cmd: (required) Specifies the command or script to be executed on the managed host. It can be a single command or a script file with an absolute or relative path.
  • removes: (optional) Removes the specified path before running the command, ensuring idempotence.
  • chdir: (optional) Changes the working directory before running the command.
  • creates: (optional) Specifies a path. If the file exists, the command will not run.
  • stdin: (optional) Provides input to the command being executed.
  • warn: (optional) If set to true, Ansible will continue with the playbook execution even if the command returns a non-zero exit status.
  • stdin_add_newline: (optional) Appends a newline character to the provided stdin input.
  • strip_empty_ends: (optional) Strips empty lines from the beginning and end of the command output.
  • executable: (optional) Sets the executable to be used to interpret the command.
  • argv: (optional) Provides additional arguments to the command.
  • free_form: (optional) An alternative way to specify the command, bypassing the cmd parameter.

Running a Basic Command

Let’s start with a simple example of running a basic command on a managed host:

In this example, the command module runs the date command on the managed host, displaying the current date and time.

Executing a Script

The command module can also execute scripts residing on the managed hosts:

In this case, the command module runs the script located at /path/to/ on the managed host.

Capturing Command Output

To capture the output of a command and use it in subsequent tasks, we can use the register parameter:

In this example, the command module captures the disk usage information of the root filesystem using the df command. The captured output is stored in the disk_usage variable, which can then be displayed using the debug module.

Using Shell Features

By default, the command module runs commands directly without using shell features such as pipes or redirects. To use shell features, we can use the shell parameter:

In this example, the command module uses the shell (/bin/bash) to execute the command, which includes piping the output of grep to wc -l.

Real-World Examples

Let’s explore some real-world scenarios where the command module proves useful.

Example 1: Installing Packages

Suppose we need to install a package on managed hosts that do not have a package manager module available. We can use the command module to run the appropriate package installation command:

In this example, the command module installs the specified package using the yum package manager.

Example 2: Checking System Health

To check the health of a managed host and gather various system information, we can use the command module to execute diagnostic commands:

In this case, the command module runs the health_check_command to collect system health information.


The Ansible command module provides an essential mechanism for running arbitrary commands and scripts on managed hosts. Its simplicity and flexibility make it a valuable tool for performing a wide range of automation tasks. Throughout this blog post, we explored the concept of the command module, its parameters, and provided real-world examples to showcase its practical applications.