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 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:
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.
Connect to Your EC2 Instance ๐
Use SSH to connect to your instance:
ssh -i /path/to/your-key.pem ubuntu@your-ec2-public-ip
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
Open the Ansible hosts file using
vim
(or any text editor of your choice):sudo vim /etc/ansible/hosts
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
Use the
ansible-inventory
command to list the inventory in YAML format:ansible-inventory --list -y
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
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
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/
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
Open the Ansible hosts file:
sudo vim /etc/ansible/hosts
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
Use the
ansible-inventory
command to list the inventory and verify the setup:ansible-inventory --list -y
5๏ธโฃ Test Connection with Ping Command
Use the Ansible
ping
module to test the connection to the nodes:ansible all -m ping
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.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 ๐