π‘ What is Ansible?
Ansible is a powerful automation tool used for:
β Server configuration
β Application deployment
β Orchestration
β Multi-server management
It works over SSH, requires no agent, and uses YAML playbooks to automate tasks.
Ansible is extremely popular in DevOps because of its simplicity and agentless architecture.
In this guide, you will learn:
How to install Ansible
How to set up a project structure
How to create inventory files
How to write playbooks
How to deploy a web page using Ansible
Step 1: Install Ansible on Ubuntu
sudo apt update
sudo apt install ansible -y
ansible --version
Step 2: Create Ansible Project Structure
Create project folder:
mkdir ansible_demo
cd ansible_demo
Create inventory & playbooks directories:
mkdir inventory playbooks
Create inventory file:
cd inventory
touch hosts.ini
Create playbook file:
cd ../playbooks
touch web_prac.yml
Step 3: Add SSH Key to the Project
In the project root directory:
cd ..
mkdir ssh
cd ssh
touch nagios.pem
Paste your PEM key content into nagios.pem.
Then assign secure permissions:
chmod 600 nagios.pem
Step 4: Test SSH Connection with PEM
ssh -i ssh/nagios.pem ubuntu@ip_address_of_any_instance
If you successfully log in β connection OK.
Step 5: Create Files Folder for Static Content
This is used when deploying frontend assets (like index.html):
cd ../playbooks
mkdir files
cd files
touch index.html
You can add your HTML content inside index.html.
Step 6: Sample Playbook (web_prac.yml)
Inside ansible_demo/playbooks/web_prac.yml
---
- name: Setup Web Server
hosts: webserver
become: yes
tasks:
- name: Install NGINX
apt:
name: nginx
state: present
update_cache: yes
- name: Copy index.html to web server
copy:
src: files/index.html
dest: /var/www/html/index.html
owner: www-data
group: www-data
mode: '0644'
- name: Start and enable NGINX
service:
name: nginx
state: started
enabled: yes
Step 7: Inventory File β hosts.ini
Inside ansible_demo/inventory/hosts.ini
[webserver]
13.201.29.244
ansible_user=ubuntu
ansible_ssh_private_key_file=../ssh/nagios.pem
Step 8: Run the Ansible Playbook
From the ansible_demo directory:
ansible-playbook -i inventory/hosts.ini playbooks/web_prac.yml
If successful β
β NGINX installed
β index.html deployed
β Web server active
Step 9: Verify on Browser
Open:
You should see your index.html content served via NGINX.
π Final Result
By the end of this guide, you have:
- Installed Ansible
- Created a professional project structure
- Created an inventory file
- Created a YAML playbook
- Added SSH key for authentication
- Automated deployment of a web page
Top comments (0)