Operations 8 min read

Automate Apache Deployment with Ansible: Step‑by‑Step Guide

Learn how to automate the installation, configuration, and management of Apache HTTP Server using Ansible, covering installation of Ansible, inventory setup, role creation, task and template writing, playbook execution, verification, and clean removal, with full command examples for Debian and RPM systems.

Raymond Ops
Raymond Ops
Raymond Ops
Automate Apache Deployment with Ansible: Step‑by‑Step Guide

Using Ansible to deploy Apache is a reliable way to automate the installation and ensure consistent configuration across servers.

Table of Contents

1 Install Ansible

2 Ansible Configuration

3 Create Role Directory

4 Create Role Sub‑directories

5 Write tasks/main.yml

6 Write templates/index.html.j2

7 Create Playbook

8 Run Playbook

9 Verify Result

10 Uninstall Apache After Verification

1 Install Ansible

On Debian‑based systems run:

sudo apt update
sudo apt install ansible

On RPM‑based systems run:

sudo yum install ansible
# or on newer systems
sudo dnf install ansible

2 Ansible Configuration

Edit the inventory file /etc/ansible/hosts to list target hosts, e.g.:

[tests]
192.168.178.222

[webservers]
192.168.178.100
192.168.178.101

[dbservers]
192.168.178.103
192.168.178.104

Adjust the main configuration /etc/ansible/ansible.cfg as needed. Example settings:

inventory = /path/to/your/inventory/file
forks = 5
remote_user = your_username
private_key_file = /path/to/your/private_key
host_key_checking = False
sudo_user = your_sudo_username
timeout = 10
log_path = /var/log/ansible.log

3 Create Role Directory

Create the role directory under /etc/ansible/roles/apache:

mkdir -p /etc/ansible/roles/apache

4 Create Role Sub‑directories

Inside the role create the standard sub‑directories (tasks, templates, files, handlers, vars, meta, defaults). At minimum tasks and templates are required:

cd /etc/ansible/roles/apache
mkdir tasks templates

5 Write tasks/main.yml

Define the steps to install and configure Apache:

---
- name: Install httpd
  yum:
    name: httpd
    state: present

- name: Start httpd service
  service:
    name: httpd
    state: started
    enabled: yes

- name: Stop firewalld service
  service:
    name: firewalld
    state: stopped
    enabled: no

- name: Create /site directory
  file:
    path: /var/www/html/site
    state: directory
    mode: '0755'

- name: Deploy index.html from template
  template:
    src: index.html.j2
    dest: /var/www/html/site/index.html
    mode: '0644'

6 Write templates/index.html.j2

Template content using Jinja2 variables:

Welcome to {{ ansible_fqdn }} On {{ ansible_default_ipv4.address }}

7 Create Playbook

Create a playbook file, e.g. apache.yml, that applies the apache role:

---
- name: Deploy Apache
  hosts: your_target_group
  become: yes
  roles:
    - apache

8 Run Playbook

Execute the playbook with: ansible-playbook apache.yml If sudo password is required, Ansible will prompt unless configured otherwise.

9 Verify Result

Check Apache status:

sudo systemctl status httpd

Test the created page with curl or wget:

curl http://localhost/site/index.html
wget -qO- http://localhost/site/index.html

10 Uninstall Apache After Verification

Playbook to stop and remove Apache:

---
- hosts: tests
  tasks:
    - name: stop httpd server
      service: name=httpd state=stopped
      notify:
        - remove httpd
  handlers:
    - name: remove httpd
      yum: name=httpd state=removed

Run with:

ansible-playbook remove_httpd.yml
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

automationConfiguration ManagementApacheAnsiblePlaybook
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.