Day 59 of 90 Days of DevOps Challenge: Ansible Project🔥 - Deploying a Web App Using Ansible

·

3 min read

Ansible has become a crucial tool in our DevOps toolkit, allowing us to automate everything from server setup to application deployment. Today, we'll apply what we’ve learned so far to deploy a simple web application using Ansible on AWS EC2 instances. 🚀

What You'll Learn Today:

  • Setting up multiple EC2 instances

  • Using Ansible to manage server configurations

  • Writing playbooks to install Nginx and deploy a web page

Let’s break this down into simple, actionable steps.


Task 01: Creating EC2 Instances

First, we need to create 3 EC2 instances, which will act as our servers. Make sure all three are created using the same key pair for SSH access.

  • Step 1: Log into your AWS Management Console.

  • Step 2: Launch 3 EC2 instances (Ubuntu 20.04 recommended) with the same key pair.

  • Step 3: Ensure the necessary security groups are in place to allow SSH and HTTP access.

Task 02: Installing Ansible on the Host Server

Next, we install Ansible on one of the EC2 instances (this will be the "host" that controls the others).

# Update and install Ansible
sudo apt update
sudo apt install ansible -y

Task 03: Copy the Private Key to the Host Server

To ensure the host server can communicate with the other EC2 instances, we need to copy the private key from our local machine to the host server.

  • Step 1: On your local machine, copy the private key to the host server:
scp -i "your-key.pem" your-key.pem ubuntu@<Ansible_Host_IP>:/home/ubuntu/.ssh/
  • Step 2: On the host server, set proper permissions for the private key:
chmod 400 /home/ubuntu/.ssh/your-key.pem

Task 04: Accessing the Inventory File

Ansible uses the inventory file to know which servers to manage. Let’s define our EC2 instances in the Ansible inventory file.

sudo vim /etc/ansible/hosts

In the inventory file, add the following lines to list your EC2 instances under a group (e.g., [webservers]):

[webservers]
<EC2-Instance-1-IP> ansible_ssh_private_key_file=/home/ubuntu/.ssh/your-key.pem
<EC2-Instance-2-IP> ansible_ssh_private_key_file=/home/ubuntu/.ssh/your-key.pem
<EC2-Instance-3-IP> ansible_ssh_private_key_file=/home/ubuntu/.ssh/your-key.pem

Task 05: Writing a Playbook to Install Nginx

Now, it’s time to create an Ansible playbook that installs Nginx on all the web servers.

Create a playbook file, install_nginx.yml:

---
- hosts: webservers
  become: yes
  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present

Task 06: Deploying a Sample Webpage

Finally, let’s deploy a simple webpage using another playbook. Create a file called deploy_webpage.yml:

---
- hosts: webservers
  become: yes
  tasks:
    - name: Copy index.html to Nginx default directory
      copy:
        src: /home/ubuntu/index.html
        dest: /var/www/html/index.html

To execute this playbook, use the following command:

ansible-playbook deploy_webpage.yml


Task Summary

  • You set up 3 EC2 instances and installed Ansible on one of them as the host server.

  • You configured the host to communicate with other instances using the same private key.

  • You wrote Ansible playbooks to install Nginx and deploy a webpage across multiple servers.

With just a few simple commands and playbooks, you’ve automated the entire process of deploying a web application!


What's Next?

In the upcoming days, we’ll explore more advanced Ansible concepts and refine our infrastructure management skills even further. Stay tuned for more hands-on projects!

Â