← Back to Week 11 Notes

Lab 2: Configuration Management with Ansible

Course: Operating Systems 3 (Virtualisation & Cloud Technologies) Week: 11 Topic: Automation and Configuration Management


1. Introduction

In Lab 1, you used Heat and Bash scripts to provision infrastructure. You likely have a stack of servers running (e.g., web-01, web-02). Now, in Lab 2, we will shift gears to Configuration Management.

We will pretend "Day 2" has arrived. Your manager wants you to install the Apache web server on all your instances and ensure a custom index page is deployed. Instead of SSHing into each server manually, you will use Ansible.

Lab Objectives


2. Prerequisites


3. Setup Ansible Environment

Ansible is written in Python. The best way to install it is via pip inside a virtual environment.

Step 1: Create Environment

# Go to your week 11 folder
cd ~/ops3/week11

# Create a virtual environment named 'venv'
python3 -m venv venv

# Activate it
source venv/bin/activate

Step 2: Install Ansible

pip install ansible

4. The Inventory

Ansible needs to know which servers to talk to. We will create a file named hosts.yaml.

Step 1: Create hosts.yaml Replace X.X.X.X with the actual Floating IPs of your instances.

all:
  vars:
    ansible_user: ubuntu
    ansible_ssh_private_key_file: mykey.pem
    ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
  children:
    webservers:
      hosts:
        web-01:
          ansible_host: 192.168.100.15  # <--- Replace with your Floating IP
        web-02:
          ansible_host: 192.168.100.16  # <--- Replace with your Floating IP

5. Ad-Hoc Commands

Before writing complex playbooks, verify that Ansible can talk to your servers.

Step 1: The Ping Test The ping module in Ansible doesn't use ICMP; it logs in via SSH and checks if it can run Python.

ansible all -m ping -i hosts.yaml

Step 2: Check Uptime Run a raw shell command across the fleet.

ansible webservers -m command -a "uptime" -i hosts.yaml

6. Write a Playbook

Now we will automate the installation of Apache. Create a file named install_apache.yaml.

---
- name: Configure Web Fleet
  hosts: webservers
  become: yes  # Equivalent to sudo
  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes

    - name: Install Apache
      apt:
        name: apache2
        state: present

    - name: Create Custom Index Page
      copy:
        content: |
          <html>
          <h1>Configured by Ansible</h1>
          <p>Server: {{ ansible_hostname }}</p>
          </html>
        dest: /var/www/html/index.html

    - name: Ensure Apache is Running
      service:
        name: apache2
        state: started

Understanding the Playbook:


7. Execution and Verification

Step 1: Run the Playbook

ansible-playbook -i hosts.yaml install_apache.yaml

Step 2: Analyze Output

Step 3: Test Idempotency

Step 4: Verify via Browser Open the Floating IP of web-01 in your browser.


8. Submission

Submit a short PDF report containing: 1. Screenshot of your ansible all -m ping success. 2. Screenshot of your Playbook run output (The first run vs the second run). 3. Screenshot of the web browser showing the custom page.


Instructor Signature: ___ Date: ___

← Back to Course Index