Ansible ad hoc commands are one-liners designed to achieve a very specific task they are like quick snippets and your compact swiss army knife when you want to do a quick task across multiple machines.
To put simply, Ansible ad hoc commands are one-liner Linux shell commands and playbooks are like a shell script, a collective of many commands with logic.
Ansible ad hoc commands come in handy when you want to perform a quick task.
Task-01
Write an ansible ad hoc ping command to ping 2 servers from the inventory file
ansible -i /path/to/inventory/file server1:server2 -m ping
This command uses the Ansible command with the following options:
-i /path/to/inventory/file: Specifies the path to the inventory file containing the servers we want to ping. /etc/ansible/hosts are by default paths of the inventory file. there is no need to write a path in the ansible ad hoc command. If your inventory file is at a different location then you need to write the path of the inventory file in ad hoc command.
server1:server2:server3: specifies the list of servers to ping, separated by colons.
-m ping: specifies that we want to use the ping module to ping the servers.
In my case, I added the naming convention as server_1 and server_2 in the /etc/ansible/hosts file
Get your Ansible Master and Node ready
Connect to your Ansible Master
sudo nano /etc/ansible/hosts
Add the host IPs in the host file with the names server_1 and server_2
ansible server_1:server_2 -m ping
Write an ansible ad hoc command to check uptime
ansible -i /path/to/inventory/file all -m command -a uptime
-m command: 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.
# Iam only specifying server_1 and server_2 for uptime
$ ansible server_1:server_2 -m command -a uptime
You can refer to this blog to understand the different examples of ad-hoc commands and try out them, post the screenshots in a blog with an explanation.
happy Learning :)
ansible ad hoc command to check the free memory or memory usage of hosts
$ ansible -i /path/to/inventory/file all -a "free -m"
$ ansible all -a "free -m"
ansible ad-hoc command to get physical memory allocated to the host
$ ansible all -m shell -a "cat /proc/meminfo|head -2"
-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.
check the disk space on all hosts in an inventory file
$ ansible all -m shell -a 'df -h'
list all the running processes on a specific host in an inventory file
$ ansible all -m command -a 'ps aux'
To run a shell command with sudo on all hosts in an inventory file
$ ansible all -b -m shell -a 'sudo-command'
$ ansible all -b -m shell -a 'sudo apt-get update'
Check the status of a specific service on all hosts in an inventory file
$ ansible all -m service -a 'name=jenkins state=started'
To copy a file to all hosts in an inventory file
$ ansible all -m copy -a 'src=/local/path/to/file dest=/remote/path/to/file mode=0644'