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:
sudo apt update
sudo apt install ansibleOn RPM‑based systems run:
sudo yum install ansible
# or on newer systems
sudo dnf install ansible2 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.104Adjust 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.log3 Create Role Directory
Create the role directory under /etc/ansible/roles/apache:
mkdir -p /etc/ansible/roles/apache4 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 templates5 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:
- apache8 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 httpdTest the created page with curl or wget:
curl http://localhost/site/index.html
wget -qO- http://localhost/site/index.html10 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=removedRun with:
ansible-playbook remove_httpd.ymlSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
