Day 56 - Mastering Ad-Hoc Commands in Ansible πŸ”§βœ¨

Day 56 - Mastering Ad-Hoc Commands in Ansible πŸ”§βœ¨

Β·

3 min read

🌟 Day 56 of the #90DaysOfDevOpsChallenge 🌟
Today, we're diving into Ansible Ad-Hoc Commands! πŸ› οΈ

chapter 3:- Performing Ad Hoc Commands with Ansible - linuxlove


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.

Configure Ansible and Run ad-hoc 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:

  1. 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.

  2. 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
      

  3. 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:

  1. 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 the server 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 πŸš€

Did you find this article valuable?

Support Nilkanth Mistry by becoming a sponsor. Any amount is appreciated!

Β