Table of contents
What is Ad Hoc Command?
An Ansible ad-hoc command
uses the /usr/bin/ansible command-line tool to automate a single task on one or more managed nodes. ad-hoc commands
are quick and easy, but they are not reusable.
Ansible ad-hoc commands
are idempotent—i.e., the state of the node is checked before executing the command, and if no state change can occur, the command isn’t executed.
Ad-hoc commands
are executed in parallel by default, enabling rapid execution across multiple hosts.
Ad-hoc commands
are particularly useful for quick tasks, troubleshooting, and one-time operations, providing flexibility and ease of use without the need for writing and maintaining a full playbook.
Syntax of Ad-hoc Command: -
ansible <hosts> -m <module_name> -a <arguments>
Here:
<hosts> is a comma-separated list of target hosts,
<module> is the name of the Ansible module to use, and
<arguments> are any arguments to be passed to the module
Ad-hoc Commands
1. If you’re using a custom SSH key to connect to the remote servers, you can provide it at execution time with the "--private-key".
ansible <host-pattern> -m <module> -a <arguments> --private-key=~/.ssh/custom_id
2. Ad-hoc command to ping 3 servers from the inventory file.
# For selecting indivisual servers
ansible server1:server2:server3 -m ping
# For selecting all servers listed on inventory file
ansible all -m ping
Here:
-m ping: specifies that we want to use the ping module to ping the servers.
3. Ad-hoc command to check system uptime.
ansible all -m command -a uptime
Here:
-m: specifies that we want to use the command module to execute the uptime command on the remote servers.
-a uptime: specifies the arguments to pass to the command module, which in this case is simply the uptime command.
4. Ad-hoc command to check the free memory or memory usage of hosts.
ansible all -a "free -m"
Here:
-a "free -m": the -a option specifies the arguments to pass to the command to be executed on the remote hosts. In this case, the free -m command is executed to show the memory usage on each host.
5. Ad-hoc command to get physical memory allocated to the host.
ansible all -m shell -a "cat /proc/meminfo|head -2"
Here:
-m shell: the -m option specifies the module to use to execute the command on the remote hosts. In this case, the shell module is used to execute the command.
-a "cat /proc/meminfo|head -2": the -a option specifies the arguments to pass to the module to execute the command on the remote hosts. In this case, the cat /proc/meminfo|head -2 command is executed to display the first two lines of the /proc/meminfo file on each host.
6. Ad-hoc command to check whether Nginx is installed on all the servers.
ansible all -b -m shell -a 'nginx -v'
Here:
-b: for running with elevated privileges(sudo)
7. Ad-hoc command to check whether Docker is installed on all the servers.
ansible all -b -m shell -a 'docker --version'
8. Ad-hoc command to check Python is installed on all the servers.
ansible all -b -m shell -a 'python --version'
9. Ad hoc command to gather fact about hosts.
ansible all -m setup
10. Ad-hoc command to copy a file to remote hosts.
ansible all -m copy -a "src=/path/to/local/file dest=/path/on/remote"
11. Ad-hoc command to check the disk space on all hosts in an inventory file.
ansible all -m shell -a 'df -h'
Here:
-m shell: to execute the command on the remote hosts using the shell.
df -h: to show the disk space usage on each host in the inventory file.
12. Ad-hoc command to list all the running processes on a specific host in an inventory file.
ansible -i inventory_file specific_host -m command -a 'ps aux'
#To see all the running processed of all the servers
ansible all -m command -a 'ps aux'
Here:
-m: to execute the command on the remote hosts using the command module.
ps aux: list all the running processes on the specific host in the inventory file.
13. Ad-hoc command to create a file with permission.
ansible all -m file -a "path=/path/to/file state=touch mode=0755"
Here:
-b: This tells Ansible to run the command as the superuser (i.e., with sudo).
-a "path=/path/to/file": to create a file at the path /path-to-file
state=touch: to create the file if it doesn't exist.
mode=0755: sets the file permissions to 0755.
14. Ad-hoc command to check file is created with given permissions using sudo ls -l.
ansible all -b -m shell -a 'sudo ls -l /path/to/file'
Conclusion
In Conclusion, Ansible Ad hoc commands
are one-line commands used to perform quick tasks on remote servers without the need for writing and executing playbooks. They are particularly useful for tasks that don't require complex orchestration.
Using Ansible ad-hoc commands
provides a quick and efficient way to perform tasks on remote servers without the need for creating and maintaining playbooks.
In this blog, we have seen some of the ad hoc commands.
Hope you find it helpful🤞 So I encourage you to try this on your own and let me know in the comment section👇 about your learning experience.✨
👆The information presented above is based on my interpretation. Suggestions are always welcome.😊
~Smriti Sharma✌