Automating Web Server Deployment with Ansible: A Step-by-Step Guide

Automating Web Server Deployment with Ansible: A Step-by-Step Guide

In this article, I’ll walk you through the process of using Ansible to install Nginx on a remote server and deploy a custom HTML page. This tutorial will cover the essential steps, from configuring Ansible to running a playbook that installs Nginx and serves a personalized web page.

1. Setting Up Ansible

To begin, you need to install Ansible on your system. Follow these commands:

sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible        

Once installed, verify the installation:

ansible --version        

2. Configuring Inventory

Create your Ansible inventory file located at /etc/ansible/hosts and define your servers. Here’s a template:

[servers]
server1 ansible_host=your public IP address
server2 ansible_host=your public IP address

[demo]
server3 ansible_host=your public IP address

[demo:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ec2-user
ansible_ssh_private_key_file=/home/ubuntu/ansible-key/youral_key.pem

[servers:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu
ansible_ssh_private_key_file=/home/ubuntu/ansible-key/your_key.pem        

Creating the Nginx Role Using Ansible

Use the following command to create the Nginx role:

ansible-galaxy init nginx        

When you use the command, it creates a directory named nginx in your current working directory. /home/ubuntu/ansible/roles/nginx


3. Creating and Running Ansible Playbooks

Install Nginx Playbook (install_nginx.yml):

This playbook installs Nginx and deploys an index.html file to the server.

- name: Install Nginx using roles
  hosts: servers
  become: true
  roles:
    - nginx        


Article content

Role Definition for Nginx (main.yml in roles/nginx/tasks/):

---
# tasks file for nginx
- name: Install Nginx
  ansible.builtin.package:
    name: nginx
    state: present

- name: Ensure Nginx is running
  ansible.builtin.service:
    name: nginx
    state: started
    enabled: yes

- name: Deploy index.html
  ansible.builtin.copy:
    src: index.html
    dest: /var/www/html/index.html        

HTML File (index.html):

This HTML file will be served by Nginx and should be placed in /etc/ansible/roles/nginx/files/.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome Pankaj Chaudhari</title>
</head>
<body style="font-family: Arial, sans-serif; background-color: #f0f0f5; color: #333; text-align: center; padding: 50px;">

    <!-- Container for the main content -->
    <div style="background-color: #fff; padding: 40px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); max-width: 600px; margin: 0 auto;">

        <!-- Welcome message -->
        <h1 style="color: #2c3e50; font-size: 36px; margin-bottom: 10px;">Welcome</h1>
        <h2 style="color: #3498db; font-size: 28px; margin-bottom: 30px;">Pankaj Chaudhari</h2>

        <!-- Role description -->
        <p style="font-size: 18px; color: #7f8c8d;">DevOps Engineer</p>

        <!-- Decorative divider -->
        <hr style="border: 0; height: 2px; background-color: #3498db; width: 50%; margin: 20px auto;">

        <!-- Additional message -->
        <p style="font-size: 16px; color: #34495e; margin-top: 20px;">
            Empowering software development and operations through automation, continuous integration, and cutting-edge tools.
        </p>

        <!-- Button to learn more -->
        <a href="#" style="display: inline-block; margin-top: 30px; padding: 10px 20px; background-color: #e74c3c; color: #fff; text-decoration: none; border-radius: 5px; font-size: 16px;">
            Learn More
        </a>
    </div>
</body>
</html>        

4. Running the Playbook

Execute the playbook with:

ansible-playbook  your_filename.yml        

Check the status of Nginx on your servers:

ansible -a "systemctl status nginx" servers        

final output


Article content


To view or add a comment, sign in

More articles by Pankaj Chaudhari

Insights from the community

Others also viewed

Explore topics