Day 59: Ansible Project — Deploy A Web Application

Radheya Zunjur
3 min readSep 4, 2023

--

Ansible playbooks are amazing, as you learned yesterday.
What if you deploy a simple web app using ansible, sounds like a good project, right? Today we are going to deploy a web application using ansible

Things to be covered in Hands-On.

  1. Creating Host server.
  2. Install Ansible to Host server.
  3. Creating 2 more EC2 instances (servers)
  4. Add these EC2s to inventory file.
  5. Configure the servers using command method.
  6. Uses of playbooks.
  7. Deployment of a simple webpage using Ansible.

Tasks:

Task 1) Create 3 EC2 instances. Make sure all three are created with same key pair

Here, we have already created 3 EC2 instances in last articles of our series. So we will use the same instances here.

Task 2) Install Ansible in host server.

Task 3) Copy the private key from local to Host server (Ansible_host)

Task 3) Access the inventory file using sudo vim /etc/ansible/hosts

All the above tasks are been already done. You can check the previous article to perform the above tasks.

Task 4) Create a playbook to install Nginx

Create a file called “install_nginx.yaml”

---
- name: playbook5
hosts: all
become: true
tasks:
- name: update apt cache
apt:
update_cache: yes

- name: install nginx
apt:
name: nginx
state: present

We have successfully installed nginx on our hosts.

Task 5) Deploy a sample webpage using the ansible playbook

Now we will deploy a sample webpage using the ansible playbook. this sounds interesting, is it?

Create a file called “index.html”

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to My Nginx Ansible Web Application</title>
<style>
/* CSS styles */
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 0;
text-align: center;
}

header {
background-color: #333;
color: #fff;
padding: 20px;
}

h1 {
font-size: 2em;
}

.container {
margin: 20px;
padding: 20px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body>
<header>
<h1>Welcome to My Nginx Ansible Web Application</h1>
</header>
<div class="container">
<p>This is a basic HTML page with CSS.</p>
<p>Replace this content with your own web application content.</p>
</div>
</body>
</html>

Now we will create a playbook to perform this particular task. Create a file called deploy_webapplication.yaml

---
- name: Deploy a web app
hosts: all
become: true
tasks:
- name: update apt cache
apt:
update_cache: yes

- name: install nginx
apt:
name: nginx
state: latest

- name: start nginx
service:
name: nginx
state: started

- name: deploy webpage
copy:
src: index.html
dest: /var/www/html

Our web application has been deployed. So, now we will run this playbook and see that webpage has deployed to all the dedicated servers.

This web application will be accessed by all the host ip’s.

--

--

Radheya Zunjur
Radheya Zunjur

Written by Radheya Zunjur

Database Engineer At Harbinger | DevOps | Cloud Ops | Technical Writer

No responses yet