Understanding Ad-hoc commands in Ansible
Day 56 and Day 57 of #90daysofdevops challenge
Table of contents
- What are Ad hoc commands?
- Task-01: Write an ansible ad hoc ping command to ping 2 servers from the inventory file
- Task-02: Write an ansible ad hoc command to check uptime
- Task-03: Ansible ad hoc command to check the free Ram memory or memory usage of hosts.
- Task-04: Write an ansible ad hoc command to check the disk space on all hosts in an inventory file.
- Task-05: Write an ansible ad hoc command to list all the running processes on a specific host in an inventory file.
What are Ad hoc commands?
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 simply, Ansible ad hoc commands are one-liner Linux shell commands and playbooks are like a shell script, a collective of many commands with logic.
Ansible ad hoc commands come in handy when you want to perform a quick task.
Task-01: Write an ansible ad hoc ping command to ping 2 servers from the inventory file
We have already created EC2 instances and made a connection between them. Check out this blog for a better understanding:
- Let's start the instances.
- Now connect to the Master instance and ping the 2 servers.
Task-02: Write an ansible ad hoc command to check uptime
To check the uptime of a remote server using Ansible's ad hoc command, you can use the ansible
command with the -a
option for the uptime
command. Here's the command and an explanation of each part:
ansible <target_host> -m command -a uptime
<target_host>
: Replace this with the hostname or IP address of the remote server you want to check the uptime for.-m command
: This specifies the Ansible module to use. In this case, we're using thecommand
module, which allows us to execute a shell command on the remote host.-a "uptime"
: This is the argument passed to thecommand
module. It specifies the shell command to run on the remote host, which is simply "uptime" in this case.
Task-03: Ansible ad hoc command to check the free Ram memory or memory usage of hosts.
ansible <target_host> all -a "free -m"
Task-04: Write an ansible ad hoc command to check the disk space on all hosts in an inventory file.
ansible <target_host> all -m shell -a "df -h"
Task-05: Write an ansible ad hoc command to list all the running processes on a specific host in an inventory file.
ansible <target_host> specific_host -m command -a 'ps aux'
"Thank you for enjoying my DevOps blog! Your positive response fuels my passion to dive deeper into technology and innovation.
Stay tuned for more captivating DevOps articles, where we'll explore this dynamic field together. Follow me on Hashnode and connect on LinkedIn (https://www.linkedin.com/in/som-shanker-pandey/) for the latest updates and discussions.