๐ Ansible Project: Automating Infrastructure Deployment ๐
Day 59 : #90DaysOfDevOps Challange
Learning of Day 59: Today, we will dive into the world of Ansible, a powerful automation tool for managing and configuring systems. In this tutorial, we will guide you through the process of setting up an Ansible environment and using it to deploy Nginx and a sample webpage on three EC2 instances. Let's get started! ๐ช
Step 1๏ธโฃ: Creating EC2 Instances To begin, create three EC2 instances using the Ubuntu t2.micro instance type. Make sure all three instances are associated with the same key pair. This key pair will allow you to access the instances securely. ๐ฅ๏ธ๐ฅ๏ธ๐ฅ๏ธ
Step 2๏ธโฃ: Installing Ansible Now, let's install Ansible on the host server. You can use the following commands to install Ansible on Ubuntu:
sudo apt update
sudo apt install ansible
Once the installation is complete, Ansible will be available on your system. โ๏ธ
Step 3๏ธโฃ: Copying Private Key To enable communication between the Ansible host and the EC2 instances, we need to copy the private key from your local machine to the Ansible host. Use the following command to copy the private key to the /home/ubuntu/.ssh
directory on the Ansible host:
scp -i /path/to/your/key.pem /path/to/your/key.pem ubuntu@ansible_host_ip:/home/ubuntu/.ssh/
Make sure to replace /path/to/your/key.pem
with the actual path to your private key file and ansible_host_ip
with the IP address of your Ansible host. ๐
Step 4๏ธโฃ: Accessing the Inventory File Next, we need to configure the inventory file, which lists the hosts Ansible will manage. Access the inventory file using the following command:
sudo nano /etc/ansible/hosts
In the inventory file, add the IP addresses or hostnames of the three EC2 instances under a suitable group name, such as [web-servers]
. Save and exit the file. ๐
Step 5๏ธโฃ: Creating the Playbook It's time to create a playbook that will install Nginx on the EC2 instances. Create a file named nginx-playbook.yml
and add the following content:
-
name: This playbook will install nginx
hosts: servers
become: yes
tasks:
- name: install nginx
apt:
name: nginx
state: latest
- name: start nginx
service:
name: nginx
state: started
enabled: yes
Save the playbook file. This playbook specifies that it should be executed on the hosts in the servers
group and installs Nginx using the apt
module. ๐
Step 6๏ธโฃ: Deploying a Sample Webpage Now, let's deploy a sample webpage on the EC2 instances using the Ansible playbook. Modify the nginx-playbook.yml
file and add the following task at the end:
- name: Deploy sample webpage
copy:
src: /path/to/your/sample-webpage/index.html
dest: /var/www/html/index.html
become: true
become_user: root
become_method: sudo
Replace /path/to/your/sample-webpage/index.html
with the actual path to your sample webpage file. This task copies the index.html
file to the default Nginx web server
directory. ๐
Step 7๏ธโฃ: Running the Playbook To run the playbook and deploy Nginx with the sample webpage, use the following command:
ansible-playbook nginx-playbook.yml
Ansible will connect to the EC2 instances, install Nginx, and deploy the sample webpage. Once the playbook execution is complete, you can access the webpage by entering the IP addresses or hostnames of the EC2 instances in a web browser. ๐
Congratulations! You have successfully set up an Ansible environment, installed Nginx, and deployed a sample webpage on multiple EC2 instances. Keep exploring Ansible's vast capabilities to automate and streamline your infrastructure management tasks! ๐
That's it for today's learning adventure! We hope you enjoyed it. Stay tuned for more exciting tutorials. Happy automating! ๐คโจ