π Getting Started with Ansible: Automate Web Server Deployment in Minutes


π Introduction to Ansible
Ansible is a powerful open-source automation tool used for configuration management, application deployment, and orchestration. It simplifies infrastructure tasks by allowing you to describe the desired state of your systems in simple, human-readable YAML files β no custom agents needed.
Hereβs why Ansible is widely loved:
β Agentless β Uses SSH to connect to remote nodes.
β Declarative β Define "what" you want, not "how" to do it.
β Scalable β Manage one to thousands of servers with ease.
π§© Key Concepts
Term | Description |
Inventory | A file listing the target servers |
Playbook | YAML file containing tasks to run |
Task | A single action, like installing a package |
Module | Pre-built code Ansible uses to perform tasks |
Role | A reusable collection of tasks, variables, templates, etc. |
π§ Installing Ansible on Ubuntu
On your control node (Ubuntu), run:
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible
Check if itβs installed:
ansible --version
π Set Up Ansible Inventory
The inventory file tells Ansible where and how to connect to your servers.
Example: /etc/ansible/hosts
[local]
localhost ansible_connection=local
[server]
ansible-slave ansible_host=<ec2-ip-address>
[server:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu
ansible_ssh_private_key_file=/home/ubuntu/key/docker_key.pem
π This config connects to a remote Ubuntu EC2 instance using SSH and a .pem
key.
π Sample Ansible Playbook: Host a Static Web Page
Hereβs a working Ansible playbook that:
Installs Nginx
Starts and enables the service
Copies a static HTML file to
/var/www/html
Playbook: httpd.yml
---
- name: Host the webpage using the playbook
hosts: all
become: yes
tasks:
- name: Install the nginx package
apt:
name: nginx
state: present
- name: Start and enable the service
service:
name: nginx
state: started
enabled: yes
- name: Copy the httpd index.html file to /var/www/html
copy:
src: /home/ubuntu/project/index.html
dest: /var/www/html/
HTML Page: index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome Page</title>
<style>
body {
background-color: #f4f4f4;
font-family: Arial, sans-serif;
text-align: center;
padding-top: 100px;
}
h1 {
color: #333;
}
p {
color: #777;
}
</style>
</head>
<body>
<h1>Welcome to My Web Server</h1>
<p>This page is served using Apache and Ansible.</p>
</body>
</html>
β Run Your Playbook
Check syntax before running:
ansible-playbook httpd.yml --syntax-check
Run the playbook with verbosity:
ansible-playbook httpd.yml -v
Verify:
Visit http://<your-ec2-ip>
in your browser β your web page should appear!
π― Conclusion
With just a few lines of YAML, you've automated the provisioning of a complete web server using Ansible. From server setup to firewall configuration and content deployment, Ansible simplifies operations and brings consistency across environments.
Subscribe to my newsletter
Read articles from Advait Hinde directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Advait Hinde
Advait Hinde
I am an Associate Platform Engineer with 2.4 years of experience in IT, specializing in Linux administration, Cloud, and Big Data. With a strong focus on managing Linux servers and Cloudera Applications. My skill set includes a basic understanding of AWS Cloud services such as EC2, IAM, VPC, as well as DevOps tools like Ansible, Git, Jenkins, Docker, Kubernetes, and Containers. Certifications: Red Hat Certified System Administrator | Red Hat Certified Engineer | Red Hat Certified Specialist in Ansible Automation (EX-407) | Certified Kubernetes Administrator (Certification ID: LF-pe1lwgmff0)