A Beginner’s Guide to Ansible Ad Hoc Commands

Ansible is a powerful automation tool that allows you to manage and configure your infrastructure through code. One of its most convenient features is the ability to run ad hoc commands, which are one-off commands executed against your managed hosts without the need for writing a full playbook. In this tutorial, we’ll explore how to run ad hoc commands in Ansible and leverage their flexibility for various tasks.

Step 1: Installing Ansible

Before we can start using Ansible, we need to ensure it’s installed on our system. If you haven’t already installed Ansible, you can do so using your system’s package manager or by following the official installation instructions on the Ansible website.

On RHEL, CentOS or Fedora you can simply install Ansible with:

$ sudo yum install ansible

Step 2: Setting up Inventory (optional)

Ansible uses an inventory file to define the hosts it will manage. The inventory file typically resides at /etc/ansible/hosts, but you can specify a custom location if needed. You can define hosts in your inventory file by IP address, hostname, or even group them together for easier management. For example:

# Define a group named "webservers"
[webservers]
server1.example.com
server2.example.com

# Define another group named "database"
[database]
dbserver1.example.com

For learning purposes, you can also execute commands against localhost. You won’t need an inventory file for that and the target machine will be the same where you are executing Ansible commands

Step 3: Running Ad Hoc Commands

Now that we have Ansible and the inventory ready, let’s dive into running ad hoc commands. You can run ad hoc commands using the ansible command-line tool followed by the -m flag to specify the module and the -a flag to provide any necessary arguments.

ansible <host-pattern> -m <module> -a '<module arguments>' [--become]
  • <host-pattern>: Specifies the target hosts or groups from your inventory.
  • <module>: Specifies the Ansible module you want to execute.
  • <module arguments>: Provides arguments specific to the module being used.
  • --become: (Optional) Allows you to execute commands with escalated privileges using sudo or su.

Let’s explore some practical examples of ad hoc commands:

ansible all -m setup

This command gathers facts about all hosts in your inventory, providing information such as operating system details, IP addresses, and available memory. If you haven’t set up an inventory file, you can run it against localhost as follows:

ansible localhost -m setup

Running Shell Commands

ansible web_servers -m shell -a 'df -h'

This command runs the df -h command on hosts belonging to the web_servers group, displaying disk usage information.

Pinging Hosts

To verify the connectivity with target host, it can be convenient to run an ad Hoc command which executes the ping module. For example:

ansible web_server -m ping

Installing Packages

ansible db_servers -m yum -a 'name=httpd state=present' --become

This Ad Hoc command is a bit more complex. As you can see, it uses the yum module but also it passes some parameters to the module to specify the package name and that the outcome should be an installation.

To check the list of parameters for a Module, besides the documentation, you can use the ansible.documentation which is an invaluable offline tool:

ansible-doc yum

Besides the parameter list, the ansible-doc command also includes examples of module usage:

ansible ad hoc commands step-by-step guide

Conclusion

Running ad hoc commands in Ansible provides a convenient way to perform quick tasks and gather information across your infrastructure. By mastering the use of ad hoc commands, you can streamline your workflow and efficiently manage your systems with Ansible’s powerful automation capabilities.

To continue your journey through Ansible, check this article: Ansible Playbook Example for beginners

Found the article helpful? if so please follow us on Socials