Ansible Fundamentals
Learn Ansible fundamentals including architecture, inventory, playbooks, modules, roles, variables, handlers, templates, Ansible Vault, ad-hoc commands, and production-ready automation with real-world examples.
Introduction
Managing hundreds or thousands of servers manually is time-consuming, error-prone, and difficult to scale. Modern organizations automate infrastructure provisioning, software installation, configuration management, patching, and deployments using automation tools.
Ansible is one of the world's most popular open-source automation tools. It is simple, agentless, and uses SSH to automate servers, cloud infrastructure, networking devices, databases, Kubernetes clusters, and application deployments.
Unlike many configuration management tools, Ansible requires no agent installation on managed nodes, making it easy to adopt in enterprise environments.
This guide introduces the fundamental concepts required for DevOps Engineers, Cloud Engineers, Platform Engineers, SREs, and Solution Architects.
Learning Objectives
After completing this guide, you'll understand
- What is Ansible?
- Why Ansible?
- Configuration Management
- Agentless Architecture
- Inventory
- Playbooks
- Modules
- Variables
- Facts
- Handlers
- Templates
- Roles
- Ansible Vault
- Ad-hoc Commands
- Ansible Workflow
- Production Best Practices
What is Configuration Management?
Configuration Management ensures that servers remain in the desired state.
Examples
- Install Packages
- Configure Apache
- Configure Nginx
- Create Users
- Restart Services
- Configure Firewalls
Instead of configuring each server manually, automation performs these tasks consistently.
Traditional Server Management
Administrator
↓
SSH Server 1
↓
Install Software
↓
SSH Server 2
↓
Repeat
↓
SSH Server 100
↓
Repeat Again
Problems
- Manual Errors
- Slow
- Inconsistent Configuration
- Difficult Scaling
Ansible Automation
flowchart LR
Administrator --> AnsibleControlNode --> Server1
AnsibleControlNode --> Server2
AnsibleControlNode --> Server3
AnsibleControlNode --> Server100
Benefits
- Agentless
- Fast
- Consistent
- Repeatable
- Easy Maintenance
What is Ansible?
Ansible is an open-source automation platform used for
- Configuration Management
- Application Deployment
- Server Provisioning
- Cloud Automation
- Network Automation
- Security Automation
- Infrastructure Automation
Why Ansible?
Advantages
- Agentless
- Simple YAML Syntax
- SSH Based
- Idempotent
- Easy Learning Curve
- Large Module Library
- Multi-Platform Support
- Cloud Integration
Ansible Architecture
flowchart LR
Developer --> ControlNode
ControlNode --> SSH
SSH --> ManagedNodes
Components
- Control Node
- Inventory
- Managed Nodes
- Playbooks
- Modules
Control Node
The machine where Ansible is installed.
Responsibilities
- Execute Playbooks
- Read Inventory
- Connect using SSH
- Execute Modules
Managed Nodes
Servers managed by Ansible.
Examples
- Linux
- Windows
- EC2
- Azure VM
- Kubernetes Nodes
- OpenShift Nodes
Inventory
Inventory defines managed hosts.
Example
[web]
web1.example.com
web2.example.com
[database]
db1.example.com
Static Inventory
Stored locally.
Example
inventory.ini
Dynamic Inventory
Generated automatically.
Examples
- AWS EC2
- Azure
- GCP
- VMware
Useful in cloud environments.
Playbook
Playbooks define automation tasks.
Written using YAML.
Example
---
- hosts: web
become: true
tasks:
- name: Install Nginx
yum:
name: nginx
state: present
Playbook Execution Flow
flowchart LR
Inventory --> Playbook --> Modules --> ManagedNodes
Modules
Modules perform specific tasks.
Examples
- yum
- apt
- copy
- file
- service
- user
- git
- command
- shell
Example
- name: Start nginx
service:
name: nginx
state: started
Variables
Variables improve reusability.
Example
app_name: payment-service
environment: production
Usage
name: "{{ app_name }}"
Facts
Facts collect server information automatically.
Example
ansible_hostname
ansible_os_family
ansible_processor
ansible_memory_mb
Display facts
ansible all -m setup
Handlers
Handlers execute only when notified.
Example
handlers:
- name: Restart Apache
service:
name: httpd
state: restarted
Useful after configuration changes.
Templates
Templates generate configuration files dynamically.
Uses
- Jinja2
- Variables
- Loops
- Conditions
Example
- name: Generate nginx.conf
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
Roles
Roles organize playbooks into reusable components.
Structure
roles/
webserver/
tasks/
handlers/
templates/
files/
defaults/
vars/
meta/
Benefits
- Modular
- Reusable
- Standardized
Ansible Vault
Encrypt sensitive information.
Example
ansible-vault create secrets.yml
Stores
- Passwords
- API Keys
- Database Credentials
- SSH Keys
Ad-hoc Commands
Useful for quick administration.
Ping
ansible all -m ping
Install package
ansible web -m yum -a "name=httpd state=present"
Restart service
ansible web -m service -a "name=httpd state=restarted"
Idempotency
One of Ansible's most important features.
Running the same playbook multiple times produces the same result.
Example
Install Nginx
First Run
Installs Package
Second Run
No Changes Required
Ansible Execution Workflow
flowchart LR
WritePlaybook --> Inventory --> SSH --> Modules --> Servers
Directory Structure
ansible/
inventory/
playbooks/
roles/
templates/
files/
group_vars/
host_vars/
ansible.cfg
Common Modules
| Module | Purpose |
|---|---|
| yum | Install Packages |
| apt | Ubuntu Packages |
| copy | Copy Files |
| file | Manage Files |
| template | Generate Configurations |
| service | Manage Services |
| user | Create Users |
| git | Clone Repositories |
| command | Execute Commands |
| shell | Run Shell Commands |
Advantages
- Agentless
- SSH Based
- Simple YAML
- Cross Platform
- Idempotent
- Large Module Ecosystem
- Cloud Ready
- Easy Integration
Limitations
- Slower than Agent-Based Tools
- SSH Dependency
- Less Suitable for Continuous Monitoring
- Large Inventories Need Optimization
Production Best Practices
Playbooks
- Keep Playbooks Small
- Reuse Roles
- Use Variables
- Avoid Hardcoding
Security
- Use Ansible Vault
- SSH Keys
- Least Privilege
- Secure Inventory
Organization
- Separate Inventories
- Version Control
- Modular Roles
- Document Playbooks
Automation
- Validate Before Execution
- Test in Development
- Use CI/CD
- Automate Rollback
Real-World Example
A DevOps engineer needs to configure 200 Linux servers.
- Server details are stored in the inventory.
- A playbook installs Java, Nginx, and Docker.
- Configuration files are generated using Jinja2 templates.
- Sensitive credentials are stored in Ansible Vault.
- Handlers restart services only when configurations change.
- The playbook is executed from the control node and configures all servers consistently in minutes.
Interview Tips
Remember these keywords
- Agentless
- SSH
- YAML
- Inventory
- Playbook
- Module
- Variable
- Facts
- Handler
- Template
- Role
- Vault
- Idempotent
- Control Node
- Managed Node
Summary
Ansible simplifies infrastructure automation through its agentless architecture, YAML-based playbooks, reusable roles, powerful modules, and idempotent execution model. It enables teams to automate server provisioning, configuration management, application deployment, and cloud operations consistently across environments.
Mastering inventories, playbooks, modules, variables, handlers, templates, roles, and Ansible Vault provides the foundation for enterprise automation and prepares you for DevOps Engineer, Cloud Engineer, Platform Engineer, SRE, and Solution Architect interviews.
In the next chapter, you'll explore Ansible Advanced, including Dynamic Inventory, Collections, AWX, Ansible Automation Platform, Kubernetes/OpenShift automation, CI/CD integration, performance optimization, security, and enterprise-scale automation architectures.