Exercise-7
7. Configuration Management with Ansible: Basics of Ansible: Inventory, Playbooks, and Modules, Automating Server Configurations with Playbooks, Hands-On: Writing and Running a Basic Playbook.
๐ฏ Objective:
To understand the basics of Ansible — Inventory, Playbooks, and Modules — and use Playbooks to automate server configurations. This includes setting up a Linux environment using Vagrant and VirtualBox, installed via Chocolatey.
๐ What is Ansible?
Ansible is an open-source configuration management and automation tool. It helps system administrators and DevOps engineers to automate:
-
Software installation and configuration
-
Server provisioning
-
Application deployment
-
System updates and patching
๐ Key Features of Ansible:
-
Agentless: No agent is needed on the target machine. It uses SSH to communicate.
-
Simple syntax: Uses YAML to define tasks in Playbooks.
-
Idempotent: Running the same playbook multiple times won’t cause unintended changes.
-
Modular: Uses built-in modules like
apt
,yum
,copy
,service
, etc.
๐ง Ansible Components:
Component | Description |
---|---|
Inventory | List of target machines to automate |
Modules | Reusable scripts used to perform actions |
Playbook | YAML file that contains tasks to be executed |
Task | A single unit of work to be executed on a host |
๐ป Part 1: Setting Up the Environment on Windows Using Chocolatey
๐ซ Step 1: Install Chocolatey
Open PowerShell as Administrator and run:
Set-ExecutionPolicy Bypass -Scope Process -Force; `
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; `
iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
Close and reopen PowerShell after installation.
๐ฆ Step 2: Install VirtualBox and Vagrant via Chocolatey
choco install virtualbox -y
choco install vagrant -y
๐ Part 2: Create and Configure a Linux VM using Vagrant
๐ Step 3: Create a Project Directory
mkdir ansible-vm
cd ansible-vm
๐ Step 4: Initialize a Vagrant Project
vagrant init ubuntu/bionic64
This creates a Vagrantfile
.
✏️ Step 5: Edit the Vagrantfile
Update the file to look like:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
vb.cpus = 2
end
end
▶️ Step 6: Start the Virtual Machine
vagrant up
๐ Step 7: SSH into the VM
vagrant ssh
๐ง Part 3: Install Ansible inside the Linux VM
Once inside the VM:
sudo apt update
sudo apt install ansible -y
Verify:
ansible --version
๐งช Part 4: Write and Run Your First Ansible Playbook
๐ Step 8: Create Inventory File
Inside the VM, create inventory.ini
:
[web]
127.0.0.1 ansible_connection=local
๐ Step 9: Create a Playbook
Create setup_webserver.yml
:
---
- name: Setup Apache Web Server
hosts: web
become: yes
tasks:
- name: Install Apache
apt:
name: apache2
state: present
update_cache: yes
- name: Enable and start Apache
service:
name: apache2
state: started
enabled: yes
- name: Deploy custom HTML page
copy:
content: "<h1>Welcome to Ansible Web Server!</h1>"
dest: /var/www/html/index.html
▶️ Step 10: Run the Playbook
ansible-playbook -i inventory.ini setup_webserver.yml
๐ Part 5: Verify the Web Server
✅ Check Web Server from inside VM:
curl http://localhost
Expected output:
<h1>Welcome to Ansible Web Server!</h1>
✅ Conclusion:
In this experiment, you have:
-
Installed VirtualBox and Vagrant using Chocolatey
-
Created a Linux VM using Vagrant
-
Installed Ansible and learned its core components
-
Wrote and executed a basic Ansible Playbook
-
Deployed a simple web server automatically
๐ Learn More:
Comments
Post a Comment