DEV Community

jay soni
jay soni

Posted on

Automating Apache Web Server Deployment Using Ansible Roles πŸš€

  • In this project, I demonstrate how to automate the installation, configuration, and deployment of a static website on an Apache Web Server using Ansible Roles.
  • The project follows Ansible best practices and is structured to be reusable, modular, and production-ready.

πŸ“Œ Project Overview

The main goal of this project is to:

  • Automate Apache installation

  • Deploy a static website

  • Use Ansible Roles for clean structure

  • Implement handlers and variables

  • Ensure OS-based conditional execution

This project is ideal for DevOps beginners and infrastructure automation use cases.

πŸ›  Tools & Technologies Used

  • Ansible – Configuration Management

  • Apache2 – Web Server

  • Linux (Debian-based OS)

  • YAML – Automation language

πŸ“ Project Directory Structure
sample_role/
β”œβ”€β”€ tasks/
β”‚ └── main.yml
β”œβ”€β”€ handlers/
β”‚ └── main.yml
β”œβ”€β”€ vars/
β”‚ └── main.yml
β”œβ”€β”€ files/
β”‚ └── index.html
β”œβ”€β”€ meta/
└── README.md

This structure follows the standard ansible-galaxy role layout.

βš™οΈ Role Components Explained
πŸ”Ή vars/main.yml

Defines reusable variables:


pkg: apache2
destfile: /var/www/html/
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή handlers/main.yml

Ensures Apache service starts only when required:

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

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή files/

Contains static website files:

  • HTML
  • CSS
  • JavaScript
  • Images  These 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 service running and enabled on boot

Static website accessible via browser using server IP

🌟 Benefits of Using Ansible Roles

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

  • Reduced human errors

🏁 Conclusion

This project demonstrates how Ansible Roles can be used 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.

Top comments (0)