Day 55 - Understanding Configuration Management with Ansible ๐Ÿš€

Day 55 - Understanding Configuration Management with Ansible ๐Ÿš€

ยท

3 min read

Welcome to Day 55 of the #90DaysOfDevOpsChallenge! Today, we dive into the world of Configuration Management with Ansible. Let's explore how to install and use Ansible on AWS EC2 instances. ๐ŸŒŸ

What's Ansible? ๐Ÿค”

Ansible basics and some tips to get you started - Sysbee

Ansible is an open-source automation tool used for IT tasks like configuration management, application deployment, orchestration, and provisioning. It's agentless, making it simple and efficient to manage multiple servers.


Task-01: Installation of Ansible on AWS EC2 (Master Node) ๐Ÿ–ฅ๏ธ

Step-by-Step Guide:

  1. Launch an EC2 Instance ๐Ÿš€

    • Open the AWS Management Console.

    • Navigate to EC2 Dashboard and click "Launch Instance".

    • Choose an Ubuntu AMI and instance type (t2.micro is free tier eligible).

    • Configure instance details, add storage, and tag your instance.

    • Configure the security group to allow SSH (port 22) access.

    • Review and launch your instance with a key pair.

  2. Connect to Your EC2 Instance ๐Ÿ”—

    • Use SSH to connect to your instance:

        ssh -i /path/to/your-key.pem ubuntu@your-ec2-public-ip
      
  3. Install Ansible ๐Ÿ› ๏ธ

    • Run the following commands to install Ansible:

        sudo apt-add-repository ppa:ansible/ansible
        sudo apt update
        sudo apt install ansible
      

      Once the installation is complete, you can check the version of Ansible using the following command:

        ansible --version
      


๐Ÿ“ Task-02: Editing Ansible Hosts File and Listing Inventory

1๏ธโƒฃ Edit the Ansible Hosts File

  1. Open the Ansible hosts file using vim (or any text editor of your choice):

     sudo vim /etc/ansible/hosts
    
  2. Add the IP addresses or hostnames of the servers you want to manage. The format is as follows:

     [group_name] 
     host1 
     host2 
     host3
    

    For example:

     [web_servers]
     192.168.1.10
     192.168.1.11
     192.168.1.12
    

2๏ธโƒฃ Verify the Inventory

  1. Use the ansible-inventory command to list the inventory in YAML format:

     ansible-inventory --list -y
    
  2. This command will display a list of hosts and their attributes, including the hostnames, IP addresses, and any defined variables or group memberships.


๐ŸŒ Task-03: Setting Up Additional EC2 Instances

1๏ธโƒฃ Launch 2 New EC2 Instances

  1. Launch two new EC2 instances using the same private key as the Ansible master instance. Ensure you have the private key file used for SSH access.

2๏ธโƒฃ Copy Private Key to Master Server

  1. From your local machine:

    Copy the private key file to the Ansible master server. You can use scp for this:

     scp -i /path/to/private_key.pem /path/to/private_key.pem ubuntu@ansible-master:/home/ubuntu/.ssh/
    

  2. On the Ansible master server:

    Navigate to the .ssh directory and set the correct permissions for the private key file:

     cd /home/ubuntu/.ssh
     chmod 600 private_key.pem
    

3๏ธโƒฃ Create Inventory File for Ansible

  1. Open the Ansible hosts file:

     sudo vim /etc/ansible/hosts
    
  2. Add the IP addresses of the new EC2 instances and specify the private key file for authentication. For example:

     [server]
     ansible_node_1 ansible_host=3.111.149.43
     ansible_node_2 ansible_host=3.110.195.44
    
     [all:vars]
     ansible_python_interpreter=/usr/bin/python3
     ansible_ssh_private_key_file=/home/ubuntu/.ssh/ansible_key
    

4๏ธโƒฃ Verify the Inventory

  1. Use the ansible-inventory command to list the inventory and verify the setup:

     ansible-inventory --list -y
    

5๏ธโƒฃ Test Connection with Ping Command

  1. Use the Ansible ping module to test the connection to the nodes:

     ansible all -m ping
    

  2. If the setup is correct, you should receive a pong response from each node, indicating that Ansible can successfully connect and is ready to run commands on them.

  3. If you have a specific group, like new_servers, you can use:

     ansible new_servers -m ping
    


๐ŸŽ‰ Congratulations! You've successfully set up and verified the Ansible inventory and tested the connection to your EC2 instances. Happy automating with Ansible!

Happy Learning! ๐Ÿ“šโœจ

#DevOps #Ansible #AWS #90DaysOfDevOpsChallenge ๐Ÿš€

Did you find this article valuable?

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

ย