Mastering Ansible Ad-hoc Commands

Mastering Ansible Ad-hoc Commands

Today on my DevOps journey, I delved into Ansible ad-hoc commands, and I'm excited to share what I learned. These commands are incredibly useful for quick tasks across multiple servers.

What are Ansible Ad-hoc Commands?

Ansible ad-hoc commands are one-line commands that perform specific tasks across multiple servers. Think of them as the equivalent of Linux shell commands, while playbooks are more like shell scripts. They're perfect for quick operations that you don't need to repeat often.

Task Implementation

Let's go through the tasks I completed today:

1. Pinging Multiple Servers

The first task was to ping three servers from our inventory file. Here's the command:

ansible server1,server2,server3 -m ping

Output:

server1 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
server2 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
server3 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

This command checks if our servers are responsive and accessible to Ansible.

2. Checking Server Uptime

Next, I checked the uptime of all servers using this command:

ansible all -m command -a "uptime"

Output:

server1 | SUCCESS | rc=0 >>
 14:12:18 up 15 days, 3:42, 1 user, load average: 0.08, 0.03, 0.01
server2 | SUCCESS | rc=0 >>
 14:12:19 up 10 days, 5:15, 2 users, load average: 0.15, 0.10, 0.07

3. Additional Useful Ad-hoc Commands

I also experimented with several other helpful commands:

Checking Disk Space

ansible all -m shell -a "df -h"

Monitoring Memory Usage

ansible all -m shell -a "free -m"

Checking OS Version

ansible all -m command -a "cat /etc/os-release"

Working with Files and Directories

# Creating a directory
ansible all -m file -a "path=/tmp/test state=directory"
# Copying files
ansible all -m copy -a "src=/local/file dest=/remote/file"

Package Management

# Installing packages
ansible all -m apt -a "name=nginx state=present" --become

Practical Example: Installing Nginx

After running the nginx installation command, we can verify the successful deployment by accessing each server:

Nginx Installation on Worker 1

Our AWS EC2 infrastructure shows all instances running smoothly:

Key Takeaways

  1. Ad-hoc commands are perfect for quick, one-time tasks

  2. They follow a simple syntax: ansible [pattern] -m [module] -a "[module options]"

  3. You can perform various operations like system checks, file operations, and package management

  4. The --become flag is essential for commands requiring elevated privileges

Conclusion

Today's learning was focused on practical implementations of Ansible ad-hoc commands. These commands serve as a powerful tool in a DevOps engineer's toolkit for quick server management tasks. While they shouldn't replace playbooks for complex operations, they're invaluable for day-to-day administrative tasks.

Follow along with my DevOps journey as we continue to explore more aspects of infrastructure automation!

#DevOps #Ansible #Automation #Infrastructure #Learning