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

ansible <host/group> -m <module> -a "<arguments>" -i inventory.ini

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:

ansible all -i inventory.ini -m ping

If everything is correct, I get:

pong

This confirms SSH connection + Ansible setup is working.


2. Running Commands – command & shell Modules

Check uptime:

ansible webservers -i inventory.ini -m command -a "uptime"

Using shell (for pipes, redirects):

ansible webservers -i inventory.ini -m shell -a "df -h | grep /dev"

This helped me understand when to use command vs shell.


3. Installing Packages

Depending on the OS, I used apt or yum.

On Ubuntu:

ansible webservers -i inventory.ini -m apt -a "name=nginx state=present" --become

On Amazon Linux/CentOS:

ansible webservers -i inventory.ini -m yum -a "name=httpd state=present" --become

This installs nginx or httpd across all servers instantly.


4. Managing Services

Start nginx:

ansible webservers -i inventory.ini -m service -a "name=nginx state=started" --become

Stop nginx:

ansible webservers -i inventory.ini -m service -a "name=nginx state=stopped" --become

Enable service at startup:

ansible webservers -i inventory.ini -m service -a "name=nginx enabled=yes" --become

5. Copying Files

ansible webservers -i inventory.ini -m copy -a "src=index.html dest=/var/www/html/index.html" --become

This copies a local file to all webservers.


6. Managing Users

Add user:

ansible all -i inventory.ini -m user -a "name=devuser state=present" --become

Delete user:

ansible all -i inventory.ini -m user -a "name=devuser state=absent" --become

7. Changing File Permissions

ansible webservers -i inventory.ini -m file -a "path=/tmp/test.txt mode=0644 owner=root group=root" --become

8. Gathering System Facts

ansible webservers -i inventory.ini -m setup

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

Popular posts from this blog

Ansible Playbooks – How I Learned to Automate Complete Tasks

Understanding Ansible Modules

“What is Ansible? – My Understanding as a DevOps Learner”