Day 55 Task: Understanding Configuration Management with Ansible

Day 55 Task: Understanding Configuration Management with Ansible

ยท

5 min read

๐Ÿ”ถ What's this Ansible?

Ansible is an open-source automation tool or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning.

๐Ÿ”ถ Task-01: Installation of Ansible on AWS EC2 (Master Node)

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

To install Ansible on an AWS EC2 instance and set it up as a master node, you can follow these steps:

Step 1: Launch an EC2 Instance

  1. Log in to your AWS Management Console.

  2. Navigate to the EC2 dashboard.

  3. Launch a new EC2 instance.

  4. Configure the instance with appropriate security groups and key pairs. Make sure you have SSH access to the instance.

  5. Launch the instance.

  6. Connect to Your EC2 Instance

Step 2: Add Ansible PPA repository by using the below command

sudo apt-add-repository ppa:ansible/ansible

Step 3: Update the System

Update the package manager and upgrade the system packages to the latest versions:

sudo apt update -y

Step 4: Install Ansible

sudo apt install ansible -y

Step 5: Verify Ansible Installation

You can verify the installation by checking the Ansible version:

ansible --version

This should display the installed Ansible version.


๐Ÿ”ถ Task-02: Read more about the Host file

sudo nano /etc/ansible/hosts

The /etc/ansible/hosts file, often referred to as the Ansible inventory file, is a critical component in Ansible. It defines the hosts or remote servers that Ansible will manage and allows you to group them into categories. This inventory file provides the necessary information for Ansible to connect to these remote hosts, such as their IP addresses or DNS names and the SSH or other connection parameters.

ansible-inventory --list -y

The ansible-inventory command is used in Ansible to display the current inventory. When you run ansible-inventory --list -y, it will show the inventory in YAML format. The inventory is typically defined in the /etc/ansible/hosts file, but it can also be dynamically generated using scripts or other sources.

Here's what the ansible-inventory --list -y command does:

  1. ansible-inventory: This is the command itself.

  2. --list: This option tells Ansible to list the inventory. It will display the inventory in JSON format.

  3. -y: This option tells Ansible to output the inventory data in YAML format. If you omit this option, the output will be in JSON format.

When you run this command, Ansible will read the inventory file and any other dynamic inventory sources (if configured) and then display the complete inventory information in either YAML format. This information includes all defined groups, hosts, host variables, and group variables.

This command is useful for debugging and verifying your inventory setup when working with Ansible. It helps you confirm that Ansible can correctly detect and interpret your inventory sources.


๐Ÿ”ถ Task-03: Setup 2 more EC2 instances with the same Private keys as the previous instance (Node)

To set up two more EC2 instances with the same private keys as the previous instance (Node), copy the private key to the master server where Ansible is set up, and then try a ping command using Ansible to the nodes, you can follow these steps:

Launch Two New EC2 Instances

  1. Log in to your AWS Management Console.

  2. Navigate to the EC2 service.

  3. Launch two new EC2 instances with the same private key as the previous instance (Node). You can do this by selecting the same key pair during the instance launch process.

  • Copy the private key to the master server where Ansible is set

    Assuming you already have the private key (.pem file) on your local machine, use a tool like scp (secure copy) to copy the private key to your Ansible master server. Replace your-private-key.pem and ansible-master-ip with your actual private key file name and Ansible master server's IP address:

      scp -i your-existing-key.pem your-private-key.pem ec2-user@ansible-master-ip:/path/to/destination/
    

    This will copy the private key to the specified path on your Ansible master server.

Test Ansible Ping Command

  1. On your Ansible master server, ensure Ansible is correctly installed.

  2. Create an Ansible inventory file (e.g., sudo vim /etc/ansible/hosts) and define the IP addresses of the two EC2 instances you want to manage. Example:

    
     [servers]
     server_1 ansible_host=<server-1 Public IP>
     server_2 ansible_host=<server-2 Public IP>
    

    Replace with the actual IP addresses of your EC2 instances, and provide the correct path to the private key file for each host.

    • Try a ping command using Ansible to the Nodes.

Test the Ansible ping command to check connectivity to the nodes:

    ansible all -m ping

This command uses the -i option to specify the inventory file and the -m option to specify the Ansible module (ping) for testing connectivity. It will attempt to SSH into each node using the provided private key and user (ec2-user in this example) and report the results.

If the setup is correct and there are no connectivity issues, you should see successful ping responses from the nodes.

That's it! We have now set up two additional EC2 instances, copied the private key to the Ansible master, and tested Ansible's connectivity to the nodes.


Happy Learning :)

Stay in the loop with my latest insights and articles on cloud โ˜๏ธ and DevOps โ™พ๏ธ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).

Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.

#Git #Linux Devops #Devopscommunity #90daysofdevopschallenge #python #docker #Jenkins #Kubernetes #AWS

Did you find this article valuable?

Support Chandresh Patle's Blog by becoming a sponsor. Any amount is appreciated!

ย