This lab demonstrates how to configure a remote server as an Apache web server using an Ansible playbook. It includes installing Apache, managing its service, and deploying a custom welcome page.
.
├── hosts.ini
└── playground.ymlvi hosts.ini[webservers]
server1 ansible_user=server1_admin ansible_ssh_pass='server1_admin@123!' ansible_become=true ansible_become_pass='server1_admin@123!'vi playground.yml---
- name: Install and Configure Apache
hosts: webservers
become: true
tasks:
- name: Update apt cache
apt:
update_cache: yes
cache_valid_time: 3600
- name: Install Apache
apt:
name: apache2
state: present
- name: Create a custom index.html
copy:
content: "Welcome to Ansible Lab!"
dest: /var/www/html/index.html
- name: Ensure Apache is running and enabled
systemd:
name: apache2
state: started
enabled: trueansible-playbook -i hosts.ini playground.ymlExecutes the playbook and configures Apache on server1.
- Verify Apache service:
ansible -i hosts.ini webservers -m command -a "systemctl status apache2"- Verify welcome page:
ansible -i hosts.ini webservers -m command -a "cat /var/www/html/index.html"Expected output:
Welcome to Ansible Lab!
- Playbook-based automation
- Package installation using
apt - Service management using
systemd - File deployment using
copymodule - Privilege escalation with
become
This lab demonstrates how Ansible can automate Apache installation, configuration, and service management, ensuring