Skip to main content

Command Palette

Search for a command to run...

πŸš€πŸ“… Day 48 DevOps Challenge - Ansible Project: Deploying a Web App with Ansible πŸ”₯

Published
β€’3 min read
πŸš€πŸ“… Day 48 DevOps Challenge - Ansible Project: Deploying a Web App with Ansible πŸ”₯
A

Passionate software engineering student | DevOps enthusiast | Seeking innovation and excellence in software engineering! πŸ‘¨β€πŸ’»

Welcome to this exciting Ansible project where we'll explore the power of Ansible playbooks by deploying a simple web app. In this project, we'll follow a step-by-step approach to create EC2 instances, set up Ansible, and use it to install and deploy a web application using Nginx.

Task 01: Setting Up the Environment πŸ› οΈ

Step 1: Create 3 EC2 Instances

To start, we'll create 3 EC2 instances using the same key pair. These instances will serve as our web servers.

Step 2: Install Ansible on the Host Server

Next, on the host server, we'll install Ansible, a powerful automation tool that will help us manage and configure our EC2 instances.

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible

Step 3: Configure SSH Key for Passwordless Authentication πŸ—οΈ

We'll generate an SSH key pair on our Ansible master server using ssh-keygen. We will then copy the public key, stored in ~/.ssh/id_rsa.pub, to the target server's ~/.ssh/authorized_keys file. This approach enables passwordless authentication and facilitates easy access to the target server.

--> In master server

--> In target server-1

--> In target server-2

Step 4: Access the Inventory File

We'll access the Ansible inventory file using a text editor (here we'll use sudo vim /etc/ansible/hosts) to define the IP addresses of our EC2 instances. This file is crucial for Ansible to know which servers to manage.

Task 02: Creating Ansible Playbook πŸ“˜

Step 1: Create a Playbook to Install Nginx

We'll create an Ansible playbook, which is a YAML file defining the tasks Ansible will perform. Our playbook will include tasks to install Nginx, a popular web server, on the EC2 instances.

---
- name: Install Nginx
  hosts: all  # Specify the group of EC2 instances in your inventory
  become: true  # Run tasks with elevated privileges

  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present

    - name: Start Nginx service
      service:
        name: nginx
        enabled: yes
        state: started

Step 2: Deploy a Sample Webpage

We'll extend our Ansible playbook to deploy a sample webpage using Nginx.

---
  tasks:
    # Previous tasks remain unchanged

    - name: Create HTML directory
      file:
        path: /var/www/html
        state: directory

    - name: Create index.html file
      copy:
        content: "<html><body><h1>Hello, Ansible! πŸš€</h1></body></html>"
        dest: /var/www/html/index.html

--> target server-1

--> target server-2

So, in addition to the method we just covered, there's another way to achieve this. We can create an index.html file with the necessary source code. Additionally, we'll create a playbook to perform this particular task. like this :

Conclusion πŸŽ‰

By completing this Ansible project, you've gained hands-on experience in automating server setup and application deployment. Ansible is a powerful tool that can be leveraged for various automation tasks in a system administration role. Explore further and adapt this project to suit your specific needs and applications. Happy automating! πŸ”₯

M

Nice article πŸ‘ Keep going .

1
A

Thanks! :)

A

Thanks! :)

More from this blog

Adarsh Jha

65 posts