Skip to content

Latest commit

 

History

History
115 lines (79 loc) · 2.03 KB

File metadata and controls

115 lines (79 loc) · 2.03 KB

Ansible Use Case: Managing Apache Service

Overview

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.


Project Structure

.
├── hosts.ini
└── playground.yml

Implementation Steps

1. Create Inventory File

vi hosts.ini
[webservers]
server1 ansible_user=server1_admin ansible_ssh_pass='server1_admin@123!' ansible_become=true ansible_become_pass='server1_admin@123!'

2. Create Playbook

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: true

3. Run Playbook

ansible-playbook -i hosts.ini playground.yml

Executes the playbook and configures Apache on server1.


4. Validation

  • 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!

Key Concepts

  • Playbook-based automation
  • Package installation using apt
  • Service management using systemd
  • File deployment using copy module
  • Privilege escalation with become

Summary

This lab demonstrates how Ansible can automate Apache installation, configuration, and service management, ensuring