This is #90DaysofDevopschallenge under the guidance of Shubham Londhe sir.
Introduction:
- Welcome back to our DevOps journey! In today's installment, we dive into the realm of configuration management with Ansible. As a powerful open-source automation tool, Ansible simplifies IT tasks such as configuration management, application deployment, intra-service orchestration, and provisioning. Let's explore Ansible's capabilities and walk through practical examples to get you started.
What's this Ansible?
- Ansible serves as a versatile platform for automating various IT tasks. It operates agentlessly, meaning no software needs to be installed on managed nodes, making it lightweight and easy to deploy. Ansible uses SSH to communicate with nodes, enabling seamless orchestration across different environments.
Task-01:
Installation of Ansible on AWS EC2 (Master Node)
To install Ansible on an AWS EC2 instance (Master Node), follow these steps:
- Add the Ansible repository to your system:
sudo apt-add-repository ppa:ansible/ansible
- Update the package index:
sudo apt update
- Install Ansible:
sudo apt install ansible
After you finish installing Ansible, check its version by typing this command:
ansible --version
This command installs Ansible along with its dependencies on the EC2 instance, allowing you to use it for automation tasks.
Task-02:
To learn more about the hosts file in Ansible, you can use a text editor to open the file located at /etc/ansible/hosts on your Ansible control node. This file contains a list of all the servers or hosts managed by Ansible.
You can open the hosts file using a command like:
sudo nano /etc/ansible/hosts
Once the file is open, you can add the IP addresses or hostnames of the servers you want Ansible to manage. Each host entry should be structured like this:
[group_name] host1 host2 host3
In this example, "group_name" is a name you choose to identify a group of hosts, and "host1", "host2", and "host3" are the IP addresses or hostnames of the servers.
You can define multiple host groups in the hosts file, each with its own set of hosts.
After adding hosts to the file, you can check Ansible's inventory of manageable hosts using the ansible-inventory command with the --list and -y options, like this:
ansible-inventory --list -y
This command will display a YAML-formatted list showing hosts and their attributes, such as hostnames, IP addresses, and any other variables or group memberships defined for a comprehensive overview of your managed infrastructure.
Task-03:
Set Up 2 More EC2 Instances:
Create 2 additional EC2 instances using the same private key as the previous one (the one we'll call "Node").
Ensure you use the same private key (.pem file) when launching these instances.
Copy Private Key to Ansible Control Node:
Transfer the private key (.pem file) from the Node instance to the master server where Ansible is installed.
You can use the
scp
command for this transfer.scp -i ansible-key.pem ~/Downloads/ansible-key.pem ubuntu@ec2-3-138-200-26.us-east-2.compute.amazonaws.com:/home/ubuntu/.ssh
Set File Permissions:
Once the file is copied, give it appropriate permissions using:
sudo chmod 600 ansible-key.pem
Edit Ansible Hosts File:
Open the Ansible hosts file located at
/etc/ansible/hosts
on the Ansible control node using a text editor.Add the IP addresses of the new EC2 instances to this file.
Also, specify the location of the private key file for authentication.
Verify Host Inventory:
After adding the hosts to the file, you can verify the inventory of hosts that
Ansible can manage using the ansible-inventory command:
ansible-inventory --list -y
Test Connectivity with Ansible:
Perform a connectivity test with Ansible by executing a ping command towards the nodes:
ansible all -m ping
This command checks if Ansible can establish connections to the nodes listed in the inventory file.
A "pong" response indicates that Ansible can communicate with the specified nodes.
To check uptime:
ansible servers -m command -a "uptime"
This command utilizes Ansible's
command
module to execute theuptime
command on all servers listed under the[servers]
group in the inventory file. It provides information about the server's current uptime, load averages, and the number of logged-in users.To check memory usage:
ansible servers -m command -a "free -m"
Similarly, this command uses the
command
module to execute thefree -m
command, which displays the amount of free and used memory in the system, measured in megabytes. It provides insights into the memory usage of each server listed under the[servers]
group in the inventory file.