Streamlining Website Deployment: Mastering Ansible Playbooks for Multi-Server Infrastructure
Welcome back, fellow tech enthusiasts! In our previous blog, we ventured into the world of Ansible, getting our hands dirty with the installation process and mastering some basic commands. If you haven’t already, make sure to check out our previous blog on How to install Ansible on an Ubuntu system and set up the master-slave configuration as we’ll be building upon those foundations.
Link to Previous Blog: How to Install Ansible on Ubuntu and Master-Slave Setup
Today, we’re taking the next exciting step on our Ansible journey as we delve into the realm of website deployment.
In this blog, we’ll seamlessly combine our newfound Ansible expertise with web hosting capabilities. Brace yourselves as we guide you through the process of deploying a website across multiple servers, all while maintaining control from a centralized location. With Ansible Playbooks as our trusty automation tool, you’re in for a treat!
So, let’s roll up our sleeves and get ready to make website deployment a breeze with Ansible. Here are the steps we’ll be covering:
Step 1: Preparing Your Ansible Playbook We’ll start by crafting a tailored Ansible Playbook to define the desired state of our infrastructure and web application.
Step 2: Configuring Your Website Next, we’ll dive into configuring the website that’s about to grace your servers. From HTML to assets, we’ve got you covered.
Step 3: Deploying the Website With everything set up, it’s time to unleash Ansible’s power. We’ll deploy your website across multiple servers effortlessly.
Step 4: Post-Deployment Verification After deployment, we’ll ensure everything is running smoothly by verifying the website’s functionality on all servers.
By the end of this blog, you’ll not only have a website up and running but also a newfound appreciation for Ansible’s ability to simplify complex tasks. So, let’s jump right in and make web hosting with Ansible a walk in the park!
- Now let's install nginx using ansible because we deploy one project.
vi install_nginx.yml
-
name: Install Nginx and start it
hosts: servers
become: yes
tasks:
- name: Install Nginx
apt:
name: nginx
state: latest
2. In above file we use “become” because we run command as root user and apt in package name because our OS is ubuntu and save this file and run
ansible-playbook install_nginx.yml
3. Now as you see we only add install nginx task lets modify and enable nginx and run
vi install_nginx.yml
-
name: Install Nginx and start it
hosts: servers
become: yes
tasks:
- name: Install Nginx
apt:
name: nginx
state: latest
- name: Start Nginx
service:
name: nginx
state: started
enabled: yes
4. save this file and run playbook
ansible-playbook install_nginx.yml
5. now if you want to check is nginx start or not copy public Ip of server 1 or 2 and hit with port 80
6. now let's create a sample html website and deploy using ansible
vi deploy_website.yml
-
name: Install Nginx
hosts: prod
become: yes
tasks:
- name: Install Nginx
apt:
name: nginx
state: latest
- name: Start Nginx
service:
name: nginx
state: started
enabled: yes
- name: Deploy Web page
copy:
src: index.html
dest: /var/www/html
7. save this file and create index.html at current location
vi index.html
<!DOCTYPE html>
<html>
<head>
<title>Cool Static Page</title>
<style>
/* CSS styles go here */
body {
background-color: #f2f2f2;
font-family: Arial, sans-serif;
text-align: center;
}
h1 {
color: #333333;
}
.container {
width: 600px;
margin: 0 auto;
padding: 20px;
background-color: #ffffff;
border: 1px solid #cccccc;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.button {
display: inline-block;
padding: 10px 20px;
background-color: #333333;
color: #ffffff;
text-decoration: none;
border-radius: 5px;
transition: background-color 0.3s;
}
.button:hover {
background-color: #555555;
}
</style>
</head>
<body>
<div class="container">
<h1>Welcome to My Cool Page!</h1>
<p>This is a static web page created using HTML.</p>
<a class="button" href="https://shrihariharidas73.medium.com/">Click Me!</a>
</div>
</body>
</html>
8. save this file and run our deploy playbook
ansible-playbook deploy_website.yml
9. copy the 3rd instance Ip and paste in browser
Resources:
- Ansible: Ansible is Simple IT Automation
- YouTube: Train With Shubham