Automate Apache Deployment with Ansible: A Step‑by‑Step Guide
This guide walks you through installing Ansible, configuring inventory, creating an Apache role with tasks and templates, writing a playbook, executing it to deploy Apache, verifying the service, and finally removing Apache using Ansible playbooks on Linux servers.
Deploy Apache with Ansible – Full Tutorial
Using Ansible to deploy the Apache service automates the installation process and guarantees consistent configuration across all servers. The following steps show how to set up Ansible, create a reusable role, write a playbook, run it, verify the deployment, and optionally uninstall Apache.
1. Install Ansible
On Debian‑based systems:
sudo apt update
sudo apt install ansibleOn RPM‑based systems:
sudo yum install ansible
# or on newer systems
sudo dnf install ansible2. Configure Ansible
Edit the inventory file /etc/ansible/hosts to list the target hosts:
[tests]
192.168.178.222
[webservers]
192.168.178.100
192.168.178.101
[dbservers]
192.168.178.103
192.168.178.104Optionally adjust the main configuration file /etc/ansible/ansible.cfg (example excerpt):
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
# sudo_pass = your_sudo_password # not recommended
timeout = 10
log_path = /var/log/ansible.log3. Create the Role Directory
mkdir -p /etc/ansible/roles/apache4. Create Role Sub‑directories
Inside the apache role create the required folders (tasks, templates, etc.):
cd /etc/ansible/roles/apache
mkdir tasks templates files handlers vars meta defaults5. Write tasks/main.yml
---
- name: Install httpd
yum:
name: httpd
state: present
- name: Start httpd service
service:
name: httpd
state: started
enabled: yes
- name: Stop firewalld
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
Welcome to {{ ansible_fqdn }} On {{ ansible_default_ipv4.address }}7. Create the Playbook ( apache.yml )
---
- name: Deploy Apache
hosts: your_target_group # replace with a group from your inventory
become: yes
roles:
- apache8. Run the Playbook
ansible-playbook apache.yml9. Verify the Deployment
Check Apache service status:
sudo systemctl status httpdFetch the generated page:
curl http://localhost/site/index.html
# or
wget -qO- http://localhost/site/index.html10. Uninstall Apache (Optional)
Create a playbook remove_httpd.yml to stop and remove the service:
---
- hosts: tests
tasks:
- name: stop httpd server
service: name=httpd state=stopped
notify:
- remove httpd
handlers:
- name: remove httpd
yum: name=httpd state=removed ansible-playbook remove_httpd.ymlFor more details, see the original article at https://www.cnblogs.com/ydswin/p/18195602 .
Signed-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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
