Day 59: Automating Web App Deployment with Ansible Project.

Vedant ThavkarVedant Thavkar
4 min read

Introduction

In the realm of IT infrastructure management and automation, Ansible has emerged as a powerful and versatile tool. It falls under the category of configuration management and orchestration tools, designed to simplify the deployment and management of software, applications, and infrastructure across various servers. Ansible's strength lies in its agentless architecture, simplicity, and the ability to automate repetitive tasks, making it a preferred choice for system administrators, DevOps professionals, and organizations aiming to streamline their operations.

Ansible Playbooks: A Brief Overview

At the core of Ansible's functionality are playbooks—declarative configuration files that specify the desired state of a system. These playbooks consist of a series of tasks, each describing a specific action to be taken on the target hosts. Tasks can range from installing software packages and configuring services to managing files and executing custom scripts.

Ansible operates on a push-based model, meaning that the control machine (the system where Ansible is installed) initiates connections to target hosts via SSH. This agentless approach eliminates the need to install any software or agents on the managed hosts, simplifying the setup and ensuring a lightweight and non-intrusive interaction.

The Essence of Infrastructure as Code (IaC)

Infrastructure as Code (IaC) is a fundamental concept in modern IT practices. It involves managing and provisioning infrastructure through machine-readable script files rather than physical hardware configuration or interactive configuration tools. Ansible playbooks exemplify this IaC paradigm, allowing users to define infrastructure configurations in code, enabling version control, collaboration, and the ability to reproduce infrastructure consistently.

Purpose of the Project

The goal of the presented project is to showcase Ansible's capabilities in a real-world scenario. By deploying a simple web application using Ansible playbooks, we aim to demonstrate the automation of tasks such as provisioning servers, installing software packages, and configuring services. This project not only provides a hands-on experience with Ansible but also emphasizes the practical benefits of using automation tools for managing and scaling infrastructure.

As we progress through the tasks, we will delve into creating EC2 instances, installing Ansible, configuring SSH access, defining inventory, and ultimately crafting a playbook to deploy an Nginx web server along with a sample webpage.

Task 01: Setting Up the Infrastructure

1. Create 3 EC2 Instances

  • Use your preferred cloud provider's interface or command-line tools to create three EC2 instances.

  • Ensure all instances use the same key pair for SSH access.

  • Take note of the public IP addresses of these instances.

2. Install Ansible on the Host Server

  • Connect to the designated host server where Ansible will be installed.

  • Run the following commands to install Ansible:

sudo apt update
sudo apt install ansible

3. Copy Private Key to Host Server

  • Copy the private key from your local machine to the Ansible host server. Replace /path/to/your/private-key.pem with your actual private key path.
scp /path/to/your/private-key.pem ubuntu@<ansible-host-ip>:/home/ubuntu/.ssh/

4. Access Inventory File

  • Edit the Ansible inventory file using a text editor. Open the file with the following command:
sudo vim /etc/ansible/hosts
  • Add the public IP addresses of your EC2 instances to the inventory file under a specified group. For example:
[web-servers]
ec2-1-ip
ec2-2-ip
ec2-3-ip

Task 02: Creating the Ansible Playbook

1. Install Nginx

  • Create an Ansible playbook file, for example, deploy-web-app.yaml, using a text editor:
sudo vim deploy-web-app.yaml
  • Insert the following content into the playbook:

  •     - hosts: all
          become: true
          tasks:
            - name: Install Nginx
              apt:
                name: nginx
                state: present
    

2. Deploy Sample Webpage

  • Extend the playbook to deploy a sample webpage. Create or obtain a sample HTML file (e.g., index.html).

  • Modify the playbook to include a task to copy this file to the Nginx default directory:

- hosts: all
  become: true
  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present

    - name: Deploy Sample Webpage
      template:
        src: /path/to/your/sample/index.html
        dest: /var/www/html/index.html

Task 03: Running the Ansible Playbook

  • Execute the Ansible playbook to run the defined tasks on the specified hosts:
ansible-playbook deploy-web-app.yaml
  • Ansible will connect to the EC2 instances, install Nginx, and deploy the sample webpage.

Conclusion

  • Congratulations! You've successfully automated the deployment of a simple web app using Ansible.

  • This project illustrates the efficiency and power of Ansible in managing infrastructure and automating routine tasks.

  • Feel free to customize and expand upon this project to suit your specific needs and explore further possibilities with Ansible automation.

10
Subscribe to my newsletter

Read articles from Vedant Thavkar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vedant Thavkar
Vedant Thavkar

"DevOps enthusiast and aspiring engineer. Currently honing skills in streamlining development workflows and automating infrastructure. Learning AWS, Docker, Kubernetes, Python, and Ansible. Eager to contribute and grow within the DevOps community."