Master Ansible: From Basics to Advanced Automation for Efficient DevOps
This guide walks you through Ansible fundamentals, installation, core concepts, sample playbooks, advanced modules like templates and handlers, dynamic inventory, and Ansible Tower, enabling you to automate configuration management and deploy complex environments such as LAMP stacks with confidence.
1. Introduction
Automation is essential for modern operations to boost efficiency and cut labor costs. Ansible, a lightweight, agent‑less automation tool, is widely adopted by DevOps and operations engineers for its simplicity and extensibility.
2. Ansible Overview
Ansible is an open‑source automation platform for configuration management, application deployment, and task execution. It communicates over SSH without requiring agents on target hosts, offering a concise YAML‑based syntax that lowers the learning curve compared with tools like Puppet or Chef.
Key features:
Agent‑less architecture : Uses SSH to manage hosts.
Extensible : Custom modules can be added.
Declarative language : Playbooks are written in easy‑to‑read YAML.
3. Core Concepts
Ansible operates on two main objects:
Inventory : Defines the list of managed hosts (static or dynamic).
Playbook : A YAML file that describes a sequence of tasks to run on the inventory.
Common modules include apt, yum, copy, etc., which perform specific actions on target machines.
4. Installation & Configuration
Linux (Ubuntu example)
sudo apt update
sudo apt install ansibleWindows
Install via Windows Subsystem for Linux (WSL) or Cygwin to provide a Linux environment.
Inventory file
Static inventory example ( /etc/ansible/hosts):
[web_servers]
web1.example.com
web2.example.com
[db_servers]
db1.example.com
db2.example.comSSH keys
Ensure the control node has SSH public‑key access to the managed hosts.
5. First Playbook – Install Nginx
---
- name: Install Nginx service
hosts: web_servers
become: yes
tasks:
- name: Update apt cache
apt:
update_cache: yes
- name: Install Nginx
apt:
name: nginx
state: present
- name: Start Nginx service
service:
name: nginx
state: started
enabled: yesKey points: hosts: web_servers targets the defined group. become: yes runs tasks with sudo. apt module manages Ubuntu packages. service ensures Nginx is running and enabled at boot.
6. Advanced Modules & Features
6.1 Template module
Use template to render Jinja2 files into configuration files on the target.
---
- name: Configure Nginx
hosts: web_servers
become: yes
tasks:
- name: Deploy Nginx config
template:
src: /path/to/nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify:
- restart nginx
handlers:
- name: restart nginx
service:
name: nginx
state: restarted6.2 Handlers
Handlers run after a task changes the system state, commonly used to restart services after configuration changes.
handlers:
- name: restart nginx
service:
name: nginx
state: restarted7. Practical Example – Deploy a LAMP Stack
---
- name: Deploy LAMP environment
hosts: web_servers
become: yes
tasks:
- name: Install Apache
apt:
name: apache2
state: present
- name: Install MySQL
apt:
name: mysql-server
state: present
- name: Install PHP and modules
apt:
name: "php libapache2-mod-php php-mysql"
state: present
- name: Start Apache
service:
name: apache2
state: started
enabled: yes
- name: Start MySQL
service:
name: mysql
state: started
enabled: yesThis playbook installs Apache, MySQL, and PHP, then ensures both services start automatically on boot.
8. Advanced Topics
8.1 Dynamic Inventory
For cloud environments (AWS, Azure, etc.), dynamic inventory plugins automatically discover hosts, eliminating manual host list updates.
# Example: AWS dynamic inventory
plugin: aws_ec2
regions:
- us-west-1
- us-east-18.2 Ansible Tower
Ansible Tower provides a web UI, role‑based access control, and audit logging, making it suitable for team‑wide automation governance.
9. Conclusion
Ansible’s straightforward YAML syntax and rich module ecosystem enable engineers to automate routine tasks, deploy complex applications, and maintain consistent configurations across large infrastructures, ultimately improving operational efficiency and reducing errors.
Full-Stack DevOps & Kubernetes
Focused on sharing DevOps, Kubernetes, Linux, Docker, Istio, microservices, Spring Cloud, Python, Go, databases, Nginx, Tomcat, cloud computing, and related technologies.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
