Objective: Use Ansible (Control Node) to automatically configure a Debian 12 Virtual Machine (Managed Node) as an Apache Web Server.
* Control Node: Windows Laptop running WSL (Ubuntu).
* Managed Node: Debian 12 Virtual Machine (VirtualBox).
* Connectivity: SSH via Bridged Networking.
Before Ansible could talk to the VM, we had to prepare the "target"
-
Network: Set VirtualBox Adapter 1 to Bridged Adapter.
-
SSH Server: Installed and started using sudo apt install openssh-server.
-
Sudo Access:
Logged in as root (su -).
Installed sudo: apt install sudo.
Added the user to the sudo group: usermod -aG sudo guser.
-
IP Discovery: Ran ip addr to find the address.
- Launched WSL: Opened Ubuntu on Windows
- Installed Ansible:
sudo apt update sudo apt install ansible sshpass -y
- Created Project Workspace:
mkdir ~/ansible-web-project && cd ~/ansible-web-project
Created three essential files to define our automation:
A. The Inventory (inventory.ini)
Tells Ansible who to talk to
[webservers]
192.168.1.183 ansible_user=guser
B. The HTML Template (index.html.j2)
A dynamic webpage using Jinja2 variables.
<h1>Deployment Successful!</h1>
<p>Server Hostname: {{ ansible_hostname }}</p>
C. The Playbook (deploy.yml)
The step-by-step instructions for the server.
Task 1: Update apt cache.
Task 2: Install apache2 package.
Task 3: Deploy the template to /var/www/html/index.html.
Task 4: Ensure the service is started and enabled.
The command to synchronize the code to the server:
ansible-playbook -i inventory.ini deploy.yml -K --ask-pass
--ask-pass: Provided the SSH password.
-K: Provided the sudo (become) password.
Result: Apache was installed, the website was updated, and the server became reachable.
| Command | Purpose |
|---|---|
ansible -m ping |
Test Connectivity to the VM |
ls -lh |
Verify project files exist and have content. |
cat <file> |
View the contents of a file in the terminal. |
nano <file> |
Edit a file. |