π Day 56 of the #90DaysOfDevOpsChallenge π
Today, we're diving into Ansible Ad-Hoc Commands! π οΈ
What are Ansible Ad-Hoc Commands? π€
Ansible ad-hoc commands are one-liners designed to achieve specific tasks quickly across multiple machines. Think of them as your compact Swiss army knife for quick tasks. π‘οΈ While playbooks are like shell scripts with multiple commands and logic, ad-hoc commands are like one-liner shell commands.
Why Use Ad-Hoc Commands?
They come in handy when you need to perform a quick task without writing a full playbook. Perfect for tasks like pinging servers, checking uptime, or managing packages. π
Task-01: Write an Ansible Ad-Hoc Ping Command
Let's write an ad-hoc command to ping three servers from your inventory file.
Step-by-Step Guide:
Set Up Your EC2 Instances on AWS π
Launch three EC2 instances using your preferred method. Make sure you have the key pair to SSH into them.
Ensure you have the Ansible control node (master) set up.
Configure Your Ansible Inventory File π
SSH into your Ansible master node:
ssh -i /path/to/your-key.pem ubuntu@your-ec2-master-ip
Open the Ansible hosts file:
sudo vim /etc/ansible/hosts
Add your three servers to the inventory:
[server] ansible_node_1 ansible_host=13.233.236.229 ansible_node_2 ansible_host=65.2.31.118 [all:vars] ansible_python_interpreter=/usr/bin/python3 ansible_ssh_private_key_file=/home/ubuntu/.ssh/ansible_key
Run the Ansible Ad-Hoc Ping Command π―
Use the following ad-hoc command to ping the three servers:
ansible server -m ping
If everything is set up correctly, you should see a
pong
response from each server.
Task-02: Write an Ansible Ad-Hoc Command to Check Uptime
Now, let's write an ad-hoc command to check the uptime of the servers.
Step-by-Step Guide:
Run the Ansible Uptime Command π
Use the following ad-hoc command to check uptime:
ansible server -a "uptime"
This command will run the
uptime
command on all servers in theserver
group and display the results.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.
To check the free memory or memory usage of hosts using the ansible ad hoc command
ansible all -i <path_to_inventory_file> -m command -a "free -m"
Ad hoc command to get physical memory allocated to the host
ansible all -m shell -a "cat /proc/meminfo|head -2"
To check the disk space on all hosts in an inventory file
ansible -i <path_to_inventory_file> all -m shell -a 'df -h'
Create a Directory with 755 permission using ansible ad hoc command
ansible all -m file -a "path=/home/ubuntu/ansible state=directory mode=0755" -b
Create a file with 755 permission using ansible ad hoc commands
COPY
ansible all -m file -a "path=/home/ubuntu/testing.txt state=touch mode=0755"
To run a shell command with sudo on all hosts
ansible all -b -m shell -a 'sudo apt-get update && sudo apt-get install docker.io -y'
To check the version on all the servers
ansible all -b -m shell -a 'sudo docker --version'
Conclusion π
Congratulations on learning how to use Ansible ad-hoc commands! These commands are incredibly useful for quick tasks and make managing multiple servers a breeze. Keep experimenting with different ad-hoc commands to enhance your automation skills.
Happy Learning! πβ¨
#DevOps #Ansible #AWS #90DaysOfDevOpsChallenge π