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 handy when you want to perform a quick task.
Tasks
Task 1) Write an ansible ad hoc ping command to ping 3 servers from inventory file
To ping all the servers run below command
ansible -m ping all -i /etc/ansible/hosts
Task 2) Write an ansible ad hoc command to check uptime
To check the uptime run below command
ansible -m command -a “uptime” -i /etc/ansible/hosts all
Task 3) Check the free memory or memory usage of hosts using ansible ad hoc command
To check the memory usage run below
ansible -m command -a “free -m” -i /etc/ansible/hosts all