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.
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:
<code>sudo apt update
sudo apt install ansible</code>On RPM‑based systems run:
<code>sudo yum install ansible
# or on newer systems
sudo dnf install ansible</code>2 Ansible Configuration
Edit the inventory file
/etc/ansible/hoststo list target hosts, e.g.:
<code>[tests]
192.168.178.222
[webservers]
192.168.178.100
192.168.178.101
[dbservers]
192.168.178.103
192.168.178.104</code>Adjust the main configuration
/etc/ansible/ansible.cfgas needed. Example settings:
<code>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</code>3 Create Role Directory
Create the role directory under
/etc/ansible/roles/apache:
<code>mkdir -p /etc/ansible/roles/apache</code>4 Create Role Sub‑directories
Inside the role create the standard sub‑directories (tasks, templates, files, handlers, vars, meta, defaults). At minimum
tasksand
templatesare required:
<code>cd /etc/ansible/roles/apache
mkdir tasks templates</code>5 Write tasks/main.yml
Define the steps to install and configure Apache:
<code>---
- 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'</code>6 Write templates/index.html.j2
Template content using Jinja2 variables:
<code>Welcome to {{ ansible_fqdn }} On {{ ansible_default_ipv4.address }}</code>7 Create Playbook
Create a playbook file, e.g.
apache.yml, that applies the
apacherole:
<code>---
- name: Deploy Apache
hosts: your_target_group
become: yes
roles:
- apache</code>8 Run Playbook
Execute the playbook with:
<code>ansible-playbook apache.yml</code>If sudo password is required, Ansible will prompt unless configured otherwise.
9 Verify Result
Check Apache status:
<code>sudo systemctl status httpd</code>Test the created page with curl or wget:
<code>curl http://localhost/site/index.html
wget -qO- http://localhost/site/index.html</code>10 Uninstall Apache After Verification
Playbook to stop and remove Apache:
<code>---
- hosts: tests
tasks:
- name: stop httpd server
service: name=httpd state=stopped
notify:
- remove httpd
handlers:
- name: remove httpd
yum: name=httpd state=removed</code>Run with:
<code>ansible-playbook remove_httpd.yml</code>Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.