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 handy when you want to perform a quick task.
Task-01
write an ansible ad hoc ping command to ping server-3 from inventory file
- Now, if I want to update or install only one server and avoid affecting the others, I'll place the specific server in a separate group. Goto the host file and create a group named 'prd' for production. I'll assign the desired server to this group, remove it from other groups, save the host file, and then use this command to ping the 'prd' group.
ansible prd -m ping
- It will show an error because it doesn’t have the Ansible SSH private key. You can verify this using the inventory command.
ansible-inventory --list
- To solve that error, go to the host file and grant access to
'all'
by using[all:vars]
. Save the host file and ping it again to confirm successful access to your application.
- Execute this command to solve your error
ansible prd -m ping
Write an ansible ad hoc command to check your date , time and uptime
- Now you can see date time and update or install your 'prd' servers using these commands.
ansible prd -a "free -h"
ansible prd -a "date"
ansible prd -a "sudo apt-get update"
ansible prd -a "sudo apt-get install nginx -y"
ansible prd -a "uptime"
Happy Learning
Thanks For Reading! :)
-Sri Parthu💝💥