⚙️ Ansible & Configuration Automation Masterclass
1️⃣ What is Ansible & Agentless Architecture
Definition: Ansible Red Hat dwara maintained ek open-source configuration management, application deployment aur provisioning tool hai. Yeh target nodes par bina koi background daemon ya agent install kiye standard OpenSSH protocol ke zariye remotely commands execute karta hai.
| Criteria | Ansible | Puppet / Chef |
|---|---|---|
| Architecture | Agentless (Push Model via SSH/WinRM) | Agent-based (Pull Model with background daemon) |
| Language | Python (Engine) + YAML (Human-readable playbooks) | Ruby / Custom DSL |
| Setup Overhead | Zero target overhead (Requires Python + SSH only) | Heavy client installation, TLS certs management |
2️⃣ Inventory Files: Static & Dynamic
Ansible control node ko batata hai ki kin managed servers par tasks run karne hain:
# Static Inventory (hosts.ini)
[webservers]
web1.fresherjobsalert.com ansible_host=13.233.20.11
web2.fresherjobsalert.com ansible_host=13.233.20.12
[dbservers]
db1.fresherjobsalert.com ansible_host=10.0.4.55
[production:children]
webservers
dbservers
[all:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=~/.ssh/prod_id_rsa
3️⃣ Ad-Hoc Commands & Core Modules
Bina playbook likhe single one-liner tasks execute karna:
# 1. Connectivity Ping Test across all nodes
ansible all -i hosts.ini -m ping
# 2. Check Disk Space
ansible webservers -i hosts.ini -m command -a "df -h"
# 3. Install Nginx using privileged sudo
ansible webservers -i hosts.ini -b -m apt -a "name=nginx state=present update_cache=yes"
4️⃣ YAML Playbooks & Idempotency
Idempotency: Ansible ka primary design pillar. Agar server par Nginx pehle se installed aur running hai, toh playbook dubara run karne par Ansible kuch modify nahi karega aur ok return karega bina unnecessary service restart kiye.
---
- name: Deploy Production Nginx Web Server
hosts: webservers
become: yes
tasks:
- name: Ensure Nginx is installed
ansible.builtin.apt:
name: nginx
state: present
- name: Ensure Nginx service is running & enabled at boot
ansible.builtin.service:
name: nginx
state: started
enabled: yes
notify: Reload Nginx
handlers:
- name: Reload Nginx
ansible.builtin.service:
name: nginx
state: reloaded
5️⃣ Variables, Facts & Jinja2 Templates
- Ansible Facts: Playbook run hote hi control node target machine ke hardware metadata (IP, OS version, RAM, CPU cores) ko
ansible_factsme dynamically gather karta hai. - Jinja2 Templating (
.j2): Dynamic configuration files generate karne ke liye curly-brace syntax ({{ variable_name }}).
# nginx.conf.j2 template snippet
server {
listen {{ http_port | default(80) }};
server_name {{ server_domain }};
root /var/www/html;
worker_processes {{ ansible_facts['processor_vcpus'] }};
}
6️⃣ Loops, Conditionals & Vault Security
# 1. Loops (Installing multiple packages)
- name: Install developer utilities
ansible.builtin.apt:
name: "{{ item }}"
state: present
loop:
- git
- curl
- htop
- fail2ban
# 2. Conditionals (when clause)
- name: Restart RedHat firewalld
ansible.builtin.service:
name: firewalld
state: restarted
when: ansible_facts['os_family'] == "RedHat"
# 3. Encrypting sensitive passwords with Ansible Vault
# ansible-vault encrypt secrets.yml
7️⃣ Ansible Roles & Directory Architecture
Large enterprise projects ko modularize karne ke liye standard folder layout (ansible-galaxy init my_role):
my_role/
├── tasks/ # Main list of tasks (main.yml)
├── handlers/ # Service restart handlers (main.yml)
├── templates/ # Jinja2 configuration templates (.j2)
├── files/ # Static files transferred as-is
├── vars/ # Role-specific high priority variables
├── defaults/ # Default low priority fallback variables
└── meta/ # Role dependencies and author metadata