Understanding Ad-hoc Commands in Ansible: A Comprehensive Guide
Day 56 : #90DaysOfDevOps Challange
๐ฃ Welcome to Day 56 of our DevOps journey! Yesterday, I published a blog post on setting up Ansible and pinging different servers using an Ansible master. To visit click here. Today, we will dive deeper into the world of Ansible by exploring ad-hoc commands. ๐ Ad-hoc commands are handy tools that allow us to perform quick tasks without writing complex playbooks. Let's expand our knowledge and learn more about ad-hoc commands in Ansible.
Ad-hoc Commands
Ad-hoc Commands in Ansible Before we jump into the practical examples, let's understand the essence of ad-hoc commands in Ansible. Ad-hoc commands are one-liners executed in the command-line interface (CLI) to perform specific tasks on remote servers without the need for a playbook. They provide a convenient and efficient way to achieve quick automation.
Task 1 - Pinging Multiple Servers
Imagine you have an inventory file located at /home/ubuntu/ansible/inventories
with the details of three servers you want to ping. Let's perform this task using an ad-hoc command:
Step 1: Open your terminal or command prompt and navigate to the directory where your Ansible project is located. ๐ฅ
Step 2: Use the following ad-hoc command to ping the servers:
ansible all -i /home/ubuntu/ansible/inventories -m ping
๐ Replace /home/ubuntu/ansible/inventories
with the correct path to your inventory file.
Step 3: Hit enter, and Ansible will execute the command. You will receive ping responses from all servers if they are reachable. ๐
Section 3: Task 2 - Checking Server Uptime Now, let's explore another practical example of using ad-hoc commands to check the uptime of a server:
Step 1: Open your terminal or command prompt and navigate to the directory where your Ansible project is located. ๐ฅ๏ธ
Step 2: Use the following ad-hoc command to check the server uptime:
ansible all -i /home/ubuntu/ansible/inventories/dev_inv -a "uptime"
๐ Replace /home/ubuntu/ansible/inventories
with the correct path to your inventory file.
ansible all -i /home/ubuntu/ansible/inventories/prod_inv -a "uptime"
Here if I don't specify the path by default inventory in my /etc/ansible/hosts folder will get executed.let's Check
Step 3: Hit enter, and Ansible will execute the command. You will receive the uptime information from all servers specified in the inventory file. ๐
Conclusion
๐ Congratulations!We have now explored ad-hoc commands in Ansible. Ad-hoc commands provide a quick and effective way to perform tasks on remote servers without the need for elaborate playbooks. Today, we covered two practical examples: pinging multiple servers and checking server uptime using ad-hoc commands.
By mastering ad-hoc commands, you can increase your productivity and efficiently manage your infrastructure using Ansible. However, keep in mind that for more complex automation scenarios, playbooks are recommended as they provide better maintainability and advanced features.
Stay tuned for tomorrow's blog post as we delve deeper into the world of DevOps. Happy automating! ๐