Lab 26: Ansible Setup and Configuration Management - 1¶
Objective
- Install Ansible on the control node (local VM or cloud instance).
- Configure passwordless SSH authentication.
- Run first Ansible command.
- Write and execute a simple playbook to deploy a web server.
Step 1: Install Ansible on Control Node¶
sudo apt update
sudo apt install ansible -y
ansible --version
sudo yum install epel-release -y
sudo yum install ansible -y
brew install ansible
- Use WSL2 (Ubuntu) and follow Linux steps.
- Or use Docker container:
docker run -it --rm williamyeh/ansible ansible --version
Step 2: Configure Target Node(s)¶
- Create a second VM (Ubuntu/EC2) as managed node.
- Ensure it has an SSH server installed:
sudo apt install openssh-server -y
Step 3: Setup SSH Key Authentication¶
On control node:
ssh-keygen -t rsa -b 4096
ssh-copy-id your_user_name@target-node-ip
# For Virtual Machines on AWS and using Amazon Linux the username is 'ec2-user`
# For Virtual Machines on AWS and using Ubunut the username is 'ubuntu'
Test:
ssh your_user_name@target-node-ip
(No password should be asked now ✅).
Step 4: Verify Setup with Ansible Ad-hoc Command¶
Create an inventory file inventory.ini:
[web]
192.168.1.20 ansible_user=ubuntu
Run ping:
ansible -i inventory.ini all -m ping
Expected output:
192.168.1.20 | SUCCESS => {
"changed": false,
"ping": "pong"
}
Step 5: Write a Simple Playbook¶
File: webserver.yml
---
- name: Install and start Apache web server
hosts: web
become: yes
tasks:
- name: Install Apache
apt:
name: apache2
state: present
update_cache: yes
- name: Ensure Apache is running
service:
name: apache2
state: started
enabled: yes
Run it:
ansible-playbook -i inventory.ini webserver.yml
Step 6: Verify Web Server¶
- On target node:
systemctl status apache2
http://<target-node-ip> → should show Apache default page.
✅ Checkpoint: Students must take a screenshot of the Apache welcome page.