Ansible Ad-Hoc Commands

Mudit Mathur
4 min readOct 12, 2023

--

TABLE OF CONTENTS

What are Ansible Ad-hoc commands
Ad-hoc command to ping 3 servers from the inventory file
Ad-hoc command to check system uptime
Ad-hoc command to check the free memory or memory usage of hosts
Ad-hoc command to get physical memory allocated to the host
Ad-hoc command to check whether Nginx is installed on all the servers
Ad-hoc command to check whether Docker is installed on all the servers
Ad-hoc command to check Python is installed on all the servers

Ansible ad hoc commands are one-liners designed to achieve a very specific task. They are like quick snippets and your compact Swiss army knife when you want to do a quick task across multiple machines.

To put it simply, Ansible ad hoc commands are one-line Linux shell commands, and playbooks are like a shell script, a collective of many commands with logic.

What are Ansible Ad-hoc commands

Ansible ad hoc commands are one-line commands used to perform simple tasks on remote systems without the need for complex playbooks. They allow you to execute specific modules on target hosts directly from the command line.

Ad hoc commands are particularly useful for quick tasks, troubleshooting, and one-time operations, providing flexibility and ease of use without the need for writing and maintaining a full playbook.

Ad hoc commands follow the syntax

ansible <host-pattern> -m <module> -a <arguments>

# If you’re using a custom SSH key to connect to the remote servers,
# you can provide it at execution time with the --private-key

ansible <host-pattern> -m <module> -a <arguments> --private-key=~/.ssh/custom_id
  • <host-pattern> specifies the target hosts or groups of hosts where the command should be executed.
  • <module> refers to the Ansible module to be used for the task. Modules provide functionality for different operations like file manipulation, package management, user management, etc.
  • <arguments> are optional arguments specific to the module, allowing customization and specifying the desired behavior.

Ad-hoc command to ping 3 servers from the inventory file

# For selecting indivisual servers
ansible server1:server2:server3 -m ping

# For selecting all servers listed on inventory file
ansible all -m ping

Ad-hoc command to check system uptime

ansible all -m command -a uptime

Ad-hoc command to check the free memory or memory usage of hosts

ansible all -a "free -m"

Ad-hoc command to get physical memory allocated to the host

 ansible all -m shell -a "cat /proc/meminfo|head -2"

Ad-hoc command to check whether Nginx is installed on all the servers

ansible all -b -m shell -a 'nginx -v'

Ad-hoc command to check whether Docker is installed on all the servers

ansible all -b -m shell -a 'docker --version'

Ad-hoc command to check Python is installed on all the servers

ansible all -b -m shell -a 'python --version'

In this blog, we’ve delved into the world of Ansible Ad-hoc commands, a powerful tool in the realm of infrastructure management. These commands offer a quick and efficient way to perform tasks and gather information across multiple servers. We explored a range of practical use cases, including:

- Pinging three servers from the inventory file.
- Checking system uptime on hosts.
- Monitoring free memory or memory usage across hosts.
- Determining physical memory allocation for each host.
- Verifying the presence of Nginx and Docker on all servers.
- Checking for the installation of Python across all servers.

These Ansible Ad-hoc commands are invaluable for real-time system administration and maintenance. They empower you to swiftly collect data and perform tasks across your infrastructure. Your feedback is crucial to our growth, so don’t hesitate to connect with me on LinkedIn at Mudit Mathur.

Stay tuned for more DevOps insights and Ansible tips as we continue our journey. #Day56 #90daysofdevops

--

--

Mudit Mathur
Mudit Mathur

No responses yet