Ansible Ad-Hoc Commands: A Practical Guide
When you're managing multiple servers with Ansible, you don't always need to write a playbook. Sometimes, all you need is a quick command to check disk usage, reboot a few machines, or restart a service.That’s where ad-hoc commands come in. They're fast, direct, and perfect for routine tasks.
Let’s walk through some of the most useful ones.
Test Connectivity to All Hosts
Before running any tasks, it's a good idea to check that Ansible can reach your hosts.
ansible all -m ping
This uses the ping
module to confirm that each host in your inventory is up and accepting connections.
Run Commands on Remote Hosts
You can execute shell commands on one or more hosts using the -a
flag:
ansible all -a "uptime"
This checks how long each machine has been running.
You can also target a specific group:
ansible webservers -a "df -h"
This runs df -h
only on hosts listed under the webservers
group.
Manage Files on Remote Machines
Create a file:
ansible all -m file -a "path=/tmp/testfile state=touch"
Delete a file:
ansible all -m file -a "path=/tmp/testfile state=absent"
Create a directory with specific permissions:
ansible all -m file -a "path=/var/logs mode=0755 state=directory"
Change the mode (permissions) of an existing file:
ansible all -m file -a "path=/tmp/testfile mode=0644"
Manage Packages
Install NGINX on Debian-based systems:
ansible all -m apt -a "name=nginx state=present"
Remove Apache on RHEL-based systems:
ansible all -m yum -a "name=httpd state=absent"
Install with elevated privileges (using -b
for become):
ansible all -b -m yum -a "name=nginx state=present"
Manage Services
Start a service:
ansible all -m service -a "name=nginx state=started"
Stop a service:
ansible all -m service -a "name=nginx state=stopped"
Restart a service:
ansible all -m service -a "name=nginx state=restarted"
Manage Users
Create a user:
ansible all -m user -a "name=john state=present"
Remove a user:
ansible all -m user -a "name=john state=absent"
Manage Cron Jobs
Add a new scheduled task:
ansible all -m cron -a "name='backup job' minute=0 hour=3 job='/usr/bin/backup.sh'"
This adds a job named “backup job” that runs daily at 3:00 AM.
Reboot Remote Hosts
To reboot all hosts in your inventory:
ansible all -m reboot
Transfer Files
Copy a local file to remote hosts:
ansible all -m copy -a "src=/path/to/local/file dest=/tmp/file"
Create a file with specific content directly:
ansible all -m copy -a "content='Hello World' dest=/tmp/hello.txt"
Gather System Facts
Collect useful system information (OS, IP, CPU, etc.):
ansible all -m setup
This is especially useful if you want to inspect facts for conditional logic later.
Run Shell and Command Modules
Run a shell command that uses environment variables:
ansible all -m shell -a "echo $HOME"
Use the command
module for simple commands (without shell features like pipes or variables):
ansible all -m command -a "ip addr show"
Check Disk Usage
To quickly check disk usage across all hosts:
ansible all -a "df -h"
This gives you a good overview of available space on each server.
This list covers the most common ad-hoc commands you’ll use in day-to-day operations. They’re a great way to interact with your systems without writing a playbook.
Keep them close. When you're in the middle of a task and need to act fast, a well-formed ad-hoc command can save you a lot of time.
Thanks for reading!
If you enjoyed this content, don't forget to leave a like ❤️ and subscribe to get more posts like this every week.