List of Ansible Ad-Hoc Commands
Ansible Ad-Hoc Commands
Ansible ad-hoc commands allow system administrators to perform quick, one-time tasks on remote machines without writing complex playbooks. These commands utilize Ansible modules to execute actions like installing packages, managing services, or retrieving system information. Ad-hoc commands are perfect for automation, troubleshooting, and simple configurations in IT environments.
1. Ping
Test connectivity to your hosts using the ping
module.
bashCopyansible all -m ping
2. Run a Shell Command
Execute a shell command on remote hosts using the shell
module.
bashCopyansible all -m shell -a "uptime"
3. Run a Command (No Shell)
Execute a command on remote hosts (without the shell) using the command
module.
bashCopyansible all -m command -a "ls -l"
4. Copy a File
Copy a local file to a remote machine using the copy
module.
bashCopyansible all -m copy -a "src=/path/to/local/file dest=/path/to/remote/file"
5. Check Disk Space
Check disk usage on remote hosts using the command
or shell
module.
bashCopyansible all -m shell -a "df -h"
6. Install a Package
Install a package (e.g., vim
) on remote hosts using the apt
or yum
module (depending on your OS).
For Ubuntu/Debian:
bashCopyansible all -m apt -a "name=vim state=present"
For CentOS/RHEL:
bashCopyansible all -m yum -a "name=vim state=present"
7. Start or Restart a Service
Manage services on remote hosts using the service
module.
bashCopyansible all -m service -a "name=nginx state=restarted"
8. Create a User
Create a user on remote hosts using the user
module.
bashCopyansible all -m user -a "name=newuser state=present"
9. Delete a User
Remove a user from remote hosts using the user
module.
bashCopyansible all -m user -a "name=olduser state=absent"
10. Add a Cron Job
Manage cron jobs with the cron
module.
bashCopyansible all -m cron -a "name='Backup job' minute='0' hour='2' job='/usr/bin/backup.sh'"
11. Change File Permissions
Modify file permissions using the file
module.
bashCopyansible all -m file -a "path=/path/to/file mode=0755"
12. Fetch a File
Retrieve a file from a remote host to your local machine using the fetch
module.
bashCopyansible all -m fetch -a "src=/path/to/remote/file dest=/path/to/local/destination flat=yes"
13. Get Facts About Hosts
Gather information about remote systems (e.g., OS, CPU, memory) using the setup
module.
bashCopyansible all -m setup
14. Reboot a System
Reboot a machine using the reboot
module.
bashCopyansible all -m reboot
15. Check for Updates (APT)
Check for package updates on Debian/Ubuntu systems using the apt
module.
bashCopyansible all -m apt -a "update_cache=yes"
16. Add a Repository
Add a repository on a remote host using the apt_repository
module (for Ubuntu/Debian).
bashCopyansible all -m apt_repository -a "repo='ppa:nginx/stable'"
17. Configure Firewall Rules
Use the ufw
module to manage firewall rules on remote Ubuntu servers.
bashCopyansible all -m ufw -a "rule='allow from 192.168.1.1 to any port 22'"
18. Get System Uptime
Retrieve the system uptime from a remote host.
bashCopyansible all -m command -a "uptime"
19. Run a Script
Run a local script on remote hosts using the script
module.
bashCopyansible all -m script -a "/path/to/local/script.sh"
20. Gather Disk Usage Info
Use the command
module to get disk usage stats.
bashCopyansible all -m command -a "df -h"
21. Configure SELinux (RHEL/CentOS)
Manage SELinux mode using the selinux
module.
bashCopyansible all -m selinux -a "policy=targeted state=enforcing"
Useful Options for Ad-Hoc Commands:
--become
: Run the command with elevated privileges (similar tosudo
).bashCopyansible all -m yum -a "name=httpd state=present" --become
-u <username>
: Specify the remote user to connect as.bashCopyansible all -m command -a "whoami" -u myuser
-k
: Prompt for the SSH password.bashCopyansible all -m ping -k