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
- Install Ansible in a Python virtual environment.
- Create a static Inventory file (
hosts.yaml). - Execute Ad-Hoc commands to verify connectivity.
- Write and Run an Ansible Playbook to configure web servers.
2. Prerequisites
- Existing Infrastructure: You should have at least 2 servers running from Lab 1 (or manually created).
- Access: You must have the SSH key (
mykey.pem) available on your lab machine. - Floating IPs: Ensure you know the Floating IP addresses of your servers.
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
- Verify installation by running
ansible --version.
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
- Edit the file with your real IP addresses.
- Ensure
mykey.pemis in the same folder.
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
- Success Requirement: You should see a green
"ping": "pong"response for each server. If you see "UNREACHABLE," check your IP addresses and SSH key permissions (chmod 600 mykey.pem).
Step 2: Check Uptime Run a raw shell command across the fleet.
ansible webservers -m command -a "uptime" -i hosts.yaml
- Verify you get two different uptime responses.
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:
- update_cache: Runs
apt-get update. - state: present: Installs the package if missing.
- copy: key copies text directly into a file on the target.
- {{ ansible_hostname }}: A variable that is magically replaced with the real server name.
7. Execution and Verification
Step 1: Run the Playbook
ansible-playbook -i hosts.yaml install_apache.yaml
Step 2: Analyze Output
- changed: Ansible made a modification.
- ok: The item was already correct (Idempotency).
Step 3: Test Idempotency
- Run the exact same command again.
- Question: How many tasks show as "changed" this time? (Hint: It should be 0).
Step 4: Verify via Browser
Open the Floating IP of web-01 in your browser.
- Did you see "Configured by Ansible"?
- Did the server name appear correctly?
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: ___