Ansible Ad-Hoc Commands – Quick & Powerful Automation
Once I finished setting up Ansible and connecting it to my EC2 instances, the very next thing I learned was how to use Ad-Hoc commands.
These became extremely useful for me because they allow automation without writing a playbook.
I could test connectivity, install packages, create files, check uptime, and much more in just a single line.
In this blog, I’ll share what ad-hoc commands are, how I understood them, and the exact commands I practiced.
⭐ What Are Ad-Hoc Commands? (In My Perspective)
Ad-Hoc commands are one-time Ansible commands that I run directly from the terminal.
They are perfect for quick tasks like:
-
checking if servers are reachable
-
installing or removing packages
-
starting or stopping services
-
copying files
-
running shell commands
-
gathering system details
These commands helped me understand Ansible modules faster before writing full playbooks.
⭐ Basic Syntax I Use
Where:
-
<host/group>→ target servers -
-m→ module -
-a→ arguments -
-i→ inventory file
This is the format I followed throughout my learning.
⭐ 1. Testing Connectivity – ping Module
This was the first command I ran:
If everything is correct, I get:
This confirms SSH connection + Ansible setup is working.
⭐ 2. Running Commands – command & shell Modules
Check uptime:
Using shell (for pipes, redirects):
This helped me understand when to use command vs shell.
⭐ 3. Installing Packages
Depending on the OS, I used apt or yum.
On Ubuntu:
On Amazon Linux/CentOS:
This installs nginx or httpd across all servers instantly.
⭐ 4. Managing Services
Start nginx:
Stop nginx:
Enable service at startup:
⭐ 5. Copying Files
This copies a local file to all webservers.
⭐ 6. Managing Users
Add user:
Delete user:
⭐ 7. Changing File Permissions
⭐ 8. Gathering System Facts
This prints details like CPU, RAM, OS version, disk space, etc.
Helpful for debugging and filtering servers based on facts.
⭐ Why Ad-Hoc Commands Are Useful (My Realisation)
After practicing them, I understood that ad-hoc commands:
✔ are perfect for quick, one-time tasks
✔ make troubleshooting faster
✔ help me test modules before writing a playbook
✔ save time when managing multiple servers
They also boosted my confidence because I could immediately see results on my remote machines.
⭐ Final Thoughts
Ad-Hoc commands became one of my favorite features in Ansible.
They are simple, powerful, and extremely helpful during daily operations.
Before writing large playbooks, I still use them to test and verify configurations quickly.
In the next blog, I’ll write about:
👉 Ansible Playbooks – the real automation power of Ansible
Comments
Post a Comment