Automating Apache Web Server Deployment Using Ansible Roles 🚀

Published: (January 14, 2026 at 02:48 AM EST)
2 min read
Source: Dev.to

Source: Dev.to

Project Overview

  • Automate Apache installation
  • Deploy a static website
  • Use Ansible Roles for a clean structure
  • Implement handlers and variables
  • Ensure OS‑based conditional execution

Ideal for DevOps beginners and infrastructure‑automation use cases.

Tools & Technologies Used

  • Ansible – configuration management
  • Apache2 – web server
  • Linux (Debian‑based)
  • YAML – automation language

Project Directory Structure

sample_role/
├── tasks/
│   └── main.yml
├── handlers/
│   └── main.yml
├── vars/
│   └── main.yml
├── files/
│   └── index.html
├── meta/
└── README.md

The layout follows the standard ansible-galaxy role structure.

Role Components Explained

vars/main.yml

Defines reusable variables:

pkg: apache2
destfile: /var/www/html/

tasks/main.yml

Handles package installation and website deployment:

- name: Install apache2
  package:
    name: "{{ pkg }}"
    state: latest
  when: ansible_os_family == "Debian"

- name: Deploy static website on apache2
  copy:
    src: "."
    dest: "{{ destfile }}"
  become: yes
  notify:
    - start apache2

handlers/main.yml

Ensures the Apache service starts only when required:

- name: start apache2
  service:
    name: apache2
    state: started
    enabled: yes

files/

Contains the static website assets (HTML, CSS, JavaScript, images).
These files are copied directly to Apache’s web‑root directory.

Playbook to Execute the Role

- hosts: all
  become: yes
  roles:
    - sample_role

Execution Flow

  1. Playbook calls the Ansible role.
  2. Variables are loaded.
  3. Apache is installed (Debian‑based OS).
  4. Static website is deployed.
  5. Handler is triggered.
  6. Apache service starts and is enabled.

Expected Output

  • Apache Web Server installed successfully

    Apache installed

  • Apache service running and enabled on boot

    Service running

  • Static website accessible via browser using server IP

    Website view

Benefits of Using Ansible Roles

  • Reusable automation code
  • Clean and organized structure
  • Easy maintenance
  • Faster deployments
  • Reduced human errors

Conclusion

This project shows how Ansible Roles can be leveraged to build scalable and reusable infrastructure automation. By separating tasks, handlers, variables, and files, the solution becomes production‑ready and aligns with real‑world DevOps practices.

Back to Blog

Related posts

Read more »