![]() |
Photo By: hitesh choudhary |
Updated on: 29 जुलाई 2025
🧠 Chapter 1: Ansible और Playbook का परिचय
आज के DevOps Automation की दुनिया में Ansible एक बेहद लोकप्रिय और शक्तिशाली टूल है जो IT infrastructure को configure, deploy और manage करने के लिए उपयोग होता है।
🔹 Ansible क्या है?
Ansible एक open-source automation tool है जिसे Red Hat द्वारा maintain किया जाता है। यह agentless होता है, यानी target servers पर किसी भी विशेष software की आवश्यकता नहीं होती — बस SSH access होना चाहिए।
- ✅ Python पर आधारित है
- ✅ Human-readable YAML files से काम करता है
- ✅ Configuration Management, Application Deployment और Continuous Delivery के लिए उपयोग होता है
🔹 Playbook क्या होता है?
Ansible में Playbook एक YAML format में लिखा गया file होता है जिसमें automation के लिए tasks define किए जाते हैं। यह एक तरह का blueprint है जो server पर क्या-क्या changes करने हैं, वो step-by-step बताता है।
📘 परिभाषा: "Ansible Playbook is a file that defines a series of tasks to be run on a group of hosts in a desired order, written in YAML syntax."
🔹 Ansible CLI vs Playbook
Ansible दो तरीकों से automation tasks चला सकता है:
- Ad-Hoc Commands: एक बार चलने वाले temporary tasks (जैसे एक बार server update करना)
- Playbooks: Reusable, repeatable, और version-controllable automation scripts
Playbook का इस्तेमाल तब किया जाता है जब हमें consistent, audited और documented configuration की ज़रूरत हो — जैसे एक production server में Apache install करना या Docker deploy करना।
🔹 YAML का क्या महत्व है?
Ansible Playbook YAML (YAML Ain't Markup Language) में लिखा जाता है क्योंकि:
- 🔸 यह human-readable होता है
- 🔸 Indentation-based होता है, जिससे nested structure clear होता है
- 🔸 JSON के मुकाबले अधिक readable और lightweight होता है
नीचे आप एक simple Ansible Playbook का उदाहरण देख सकते हैं:
---
- name: Install and start Apache
hosts: webservers
become: true
tasks:
- name: Install Apache
apt:
name: apache2
state: present
- name: Start Apache
service:
name: apache2
state: started
enabled: true
यह Playbook क्या करता है?
- Webservers group के सभी hosts पर Apache install करता है
- Service को start और enable करता है
🎯 क्यों जरूरी है ये जानना?
अगर आप DevOps, Cloud Infrastructure, या Linux System Administration में करियर बना रहे हैं, तो Ansible Playbook एक जरूरी स्किल है। यह न सिर्फ आपके manual efforts को बचाता है, बल्कि reproducible automation का दरवाज़ा खोलता है।
🔗 Official Ansible Documentation (RedHat)
📘 Chapter 2: YAML Syntax और Ansible Playbook की Basic Structure
YAML (YAML Ain't Markup Language) एक human-readable data format है जिसका उपयोग Ansible Playbooks को define करने के लिए किया जाता है। YAML की simplicity और indentation-based structure इसे configuration management के लिए ideal बनाता है।
🔹 YAML के Basic Rules:
- ✅ Indentation space-based होता है (tabs नहीं, सिर्फ spaces)
- ✅ Key-Value pairs format में data लिखा जाता है
- ✅ Lists को hyphen (-) से define किया जाता है
# एक सामान्य YAML उदाहरण
---
- name: Sample Task
hosts: localhost
tasks:
- name: Print Hello
debug:
msg: "Hello from YAML!"
📌 क्या होता है '---'? यह YAML डॉक्यूमेंट की शुरुआत को दर्शाता है।
🔹 Ansible Playbook की Structure:
एक typical Ansible Playbook में निम्नलिखित मुख्य सेक्शन होते हैं:
Section | Purpose |
---|---|
name |
Playbook या Task का नाम (readability के लिए) |
hosts |
Target servers जिन पर tasks चलेंगे |
become |
root privileges के लिए (sudo access) |
tasks |
टास्क्स की लिस्ट जो चलानी है |
🔹 Playbook का Example Structure:
---
- name: Apache Installation Example
hosts: web
become: true
tasks:
- name: Install Apache
apt:
name: apache2
state: present
- name: Start Apache Service
service:
name: apache2
state: started
ऊपर दिए गए Playbook में:
- Apache package install किया गया है
- Service को start किया गया है
📌 Extra Tips:
- Playbook को YAML validator से check करें (जैसे YAMLChecker.com)
- Lint tool जैसे
ansible-lint
का use करें production से पहले
📘 Chapter 3: Ansible Modules क्या होते हैं? Top 10 Modules Explained (हिंदी में)
Ansible Modules वो tools होते हैं जो actual task perform करते हैं — जैसे file copy करना, software install करना, user बनाना आदि। जब आप Playbook में कोई task define करते हैं, तो आप किसी न किसी module का use कर रहे होते हैं।
🔹 Module क्या होता है?
Module एक छोटा program या function होता है जो specific task को execute करता है। Ansible में 3000+ से ज़्यादा built-in modules होते हैं जिनका उपयोग आप कर सकते हैं:
- ✅ Files & Directories manage करना
- ✅ Package install/update करना
- ✅ Services start/stop/restart करना
- ✅ Cloud resources create करना (AWS, GCP, Azure)
📋 Top 10 Useful Ansible Modules
Module Name | Purpose |
---|---|
apt |
Debian/Ubuntu packages install करने के लिए |
yum |
RHEL/CentOS के लिए package management |
copy |
Local से remote file copy करना |
template |
Jinja2 templating के साथ dynamic config push करना |
file |
File के permissions, owner, state control करना |
user |
User accounts manage करना |
service |
Systemd services start/stop करना |
debug |
Playbook output print करना (for testing) |
shell |
Raw shell commands run करना |
ping |
Check करने के लिए कि host reachable है या नहीं |
🛠️ Module Use Example:
- name: Install Apache on Ubuntu
apt:
name: apache2
state: present
📘 Module Parameters क्या होते हैं?
हर module के अपने parameters होते हैं जो उसकी configuration को define करते हैं:
name
– कौन सा package या file हैstate
– present, absent, started, stopped, आदिowner
,group
,mode
– file permissions
🔗 Official Module Index:
👉 Ansible Official Modules List
📘 Chapter 4: Ansible Variables, Facts और Templates Explained (हिंदी में)
Ansible की सबसे बड़ी ताकत है — Variables और Templates की मदद से dynamic automation। यह हमें हर system के अनुसार Playbook को intelligent बनाने की सुविधा देता है। चलिए step-by-step समझते हैं:
🔹 Ansible Variables क्या होते हैं?
Variables reusable values होते हैं जिन्हें आप अलग-अलग जगह use कर सकते हैं जैसे IP address, usernames, packages, paths आदि। इससे code DRY (Don’t Repeat Yourself) बनता है।
# Example
vars:
apache_pkg: apache2
tasks:
- name: Install Apache using variable
apt:
name: "{{ apache_pkg }}"
state: present
{{ apache_pkg }} एक variable है जिसे curly brackets में reference किया गया है।
---📚 Variables Define करने के तरीके:
- vars: Task के अंदर variables define करने के लिए
- vars_files: अलग YAML files से variables load करने के लिए
- group_vars/host_vars: Inventory के base पर
- Extra vars: CLI से pass करने के लिए
🔍 Ansible Facts क्या होते हैं?
Facts system-specific data होते हैं जो Ansible खुद collect करता है — जैसे hostname, OS type, IP address, memory, CPU info आदि। ये हर host से runtime में automatically gather होते हैं।
- name: Show system facts
debug:
var: ansible_facts
आप specific fact को भी use कर सकते हैं जैसे:
{{ ansible_facts['os_family'] }}
{{ ansible_facts['default_ipv4']['address'] }}
---
🧩 Templates क्या होते हैं?
Ansible में Templates Jinja2 templating engine पर आधारित होते हैं। ये `.j2` extension वाले files होते हैं जिन्हें dynamic data के साथ generate किया जाता है। इससे आप config files को variables और conditions के साथ auto-generate कर सकते हैं।
# Apache Config Template Example (apache.conf.j2)
ServerAdmin {{ admin_email }}
ServerName {{ server_name }}
DocumentRoot {{ doc_root }}
Playbook में इसे कैसे use करें?
- name: Deploy Apache Config
template:
src: apache.conf.j2
dest: /etc/apache2/sites-available/000-default.conf
---
🧠 Bonus Tip: Variables + Facts + Templates = Smart Playbooks
आप playbooks को इतना intelligent बना सकते हैं कि हर server पर अलग value के साथ automatically deploy हों — बिना एक भी line change किए!
📘 Chapter 5: Ansible Handlers, Loops और Conditions (With Real Examples)
Ansible में Handlers, Loops, और Conditional Statements automation को ज़्यादा powerful और intelligent बनाते हैं। ये advanced features playbook को flexible और responsive बनाते हैं। आइए इन तीनों को detail में समझते हैं:
🔹 1. Handlers क्या होते हैं?
Handlers ऐसे special tasks होते हैं जो तब trigger होते हैं जब कोई task बदलता है। इनका उपयोग अक्सर service को restart या reload करने के लिए किया जाता है — लेकिन वो तभी चलते हैं जब पहले task में बदलाव हुआ हो।
# Example Playbook with Handler
---
- name: Install Apache and Configure
hosts: web
become: true
tasks:
- name: Install Apache
apt:
name: apache2
state: present
notify: Restart Apache
- name: Upload new config
template:
src: apache.conf.j2
dest: /etc/apache2/apache2.conf
notify: Restart Apache
handlers:
- name: Restart Apache
service:
name: apache2
state: restarted
notify: handler को trigger करता है। लेकिन handler सिर्फ तब चलेगा जब task में कोई change होगा।
---🔄 2. Loops (with_items)
Loops का use आप repetitive tasks को simplify करने के लिए कर सकते हैं — जैसे multiple packages install करना, users create करना आदि।
- name: Install multiple packages
apt:
name: "{{ item }}"
state: present
with_items:
- curl
- git
- unzip
with_items: loop चलता है हर item के लिए।
नया syntax (Ansible 2.5+):
- name: Install packages using loop
apt:
name: "{{ item }}"
state: present
loop:
- curl
- git
- unzip
---
🧪 3. Conditions (when)
when का use करके आप tasks को conditional बना सकते हैं — यानि वो task सिर्फ तब चले जब कोई condition पूरी हो।
- name: Install Apache on Debian only
apt:
name: apache2
state: present
when: ansible_facts['os_family'] == "Debian"
Multiple conditions भी लिख सकते हैं:
when:
- ansible_facts['distribution'] == "Ubuntu"
- ansible_facts['ansible_kernel'] == "5.15.0"
---
💡 Bonus: Handlers + Loops + Conditions Combined
- name: Create multiple users
user:
name: "{{ item }}"
state: present
loop:
- devuser
- testuser
- admin
when: ansible_facts['os_family'] == "Debian"
notify: Update Audit Logs
handlers:
- name: Update Audit Logs
command: /usr/local/bin/update_audit_log.sh
---
📘 Chapter 6: Ansible Roles, Directory Structure और Reusability (हिंदी में पूरी जानकारी)
जैसे-जैसे आपका automation बढ़ता है, वैसे-वैसे Playbooks complex होने लगते हैं। ऐसे में Roles का इस्तेमाल करके आप Ansible Playbooks को modular, scalable और reusable बना सकते हैं। ये enterprise-grade best practice है।
🔹 Ansible Roles क्या होते हैं?
Role एक structured way है जिससे आप Playbooks के अलग-अलग हिस्सों को logically अलग कर सकते हैं — जैसे कि tasks, handlers, templates, vars, और defaults। इससे code को समझना, debug करना और reuse करना आसान हो जाता है।
📁 Role की Standard Directory Structure:
roles/
└── apache/
├── tasks/
│ └── main.yml
├── handlers/
│ └── main.yml
├── templates/
│ └── apache.conf.j2
├── vars/
│ └── main.yml
├── defaults/
│ └── main.yml
├── files/
│ └── index.html
├── meta/
│ └── main.yml
└── README.md
हर folder का specific purpose होता है:
Folder | Purpose |
---|---|
tasks | Main tasks list |
handlers | Triggered handlers |
vars | Custom variables |
defaults | Default values (lowest priority) |
files | Static files to copy |
templates | Jinja2 templates |
meta | Dependencies and metadata |
🛠️ Role को कैसे Run करें?
---
- hosts: web
become: true
roles:
- apache
बस इतना लिखकर आप पूरा role execute कर सकते हैं। Role folders `roles/` directory में होने चाहिए।
---🚀 Ansible Galaxy से Ready-Made Roles:
आप community-contributed roles को भी use कर सकते हैं Ansible Galaxy से।
ansible-galaxy install geerlingguy.apache
और फिर use करें:
roles:
- geerlingguy.apache
🔗 Official Source: Ansible Galaxy
📘 Chapter 7: Ansible Vault, Secrets और Credentials को Secure करना (हिंदी में)
जब आप automation करते हैं तो बहुत सी sensitive जानकारी जैसे passwords, API keys, secret tokens आपको YAML files में store करनी होती है। इन्हें plain-text में रखना security के लिए बड़ा खतरा हो सकता है। यही वजह है कि Ansible में Vault नाम का feature दिया गया है — जो encrypted data management करता है।
🔐 Ansible Vault क्या है?
Ansible Vault एक CLI-based tool है जो YAML files को encrypt और decrypt करने में मदद करता है। आप entire files, variable blocks, या सिर्फ कुछ secrets को encrypt कर सकते हैं।
🛡️ Vault File Create कैसे करें?
# नया encrypted file बनाना:
ansible-vault create secrets.yml
यह command एक password मांगेगा और फिर editor में content add करने देगा। जो कुछ भी आप लिखेंगे वो encrypted save होगा।
🔓 Vault File को Edit करना:
ansible-vault edit secrets.yml
📖 Vault File को View करना:
ansible-vault view secrets.yml
🔄 Vault Encrypt/Decrypt Existing File:
ansible-vault encrypt passwords.yml
ansible-vault decrypt passwords.yml
🚀 Vault Password का इस्तेमाल Playbook में:
ansible-playbook site.yml --ask-vault-pass
या फिर password file का path दें:
ansible-playbook site.yml --vault-password-file ~/.vault_pass.txt
📂 Vault + Roles में कैसे करें?
आप group_vars
और host_vars
folders में secrets को encrypted files में store कर सकते हैं।
group_vars/
├── all.yml
├── all.vault.yml ← encrypted file
🎯 Best Practices for Secure Secrets Handling:
- ✅ Passwords और API Keys को कभी भी plain YAML में मत रखें
- ✅ Vault password को git में कभी ना डालें
- ✅ CI/CD pipelines में vault password को environment variable के रूप में manage करें
- ✅ केवल need-to-know developers को ही vault password दें
🔗 Official Ansible Vault Guide
📘 Chapter 8: Ansible में Error Handling, Debugging और Troubleshooting (हिंदी में)
Ansible automation करते समय कभी-कभी playbook fail हो जाती है या unexpected behavior होता है। इसलिए ये जानना ज़रूरी है कि आप error को कैसे पकड़ें, कैसे debug करें और कैसे troubleshoot करें — ताकि आपके automation workflows हमेशा smooth चलें।
🔍 Ansible Debugging Basics:
Debugging के लिए Ansible एक built-in debug
module provide करता है, जिससे आप variables, messages या conditions को print कर सकते हैं।
- name: Show variable value
debug:
var: apache_pkg
- name: Show custom message
debug:
msg: "Apache install होने वाला है!"
⚠️ Error Handling with ignore_errors
अगर आप चाहते हैं कि कोई task fail होने पर भी बाकी playbook execute होती रहे, तो आप ignore_errors: yes
का use कर सकते हैं:
- name: Try installing package
apt:
name: notarealpackage
state: present
ignore_errors: yes
Note: ये use करें only when failure acceptable हो।
🛑 Fail Module का Use:
कभी-कभी आपको manually playbook को रोकना पड़ सकता है किसी specific condition के आधार पर। इसके लिए आप fail
module का use कर सकते हैं:
- name: Stop if environment is not production
fail:
msg: "This playbook should only run in production!"
when: env != "production"
🧪 Block, Rescue, Always Structure:
यह structure try-catch जैसा होता है — जहाँ आप errors को पकड़कर fallback actions ले सकते हैं।
- block:
- name: Try this risky task
command: /bin/something-risky
rescue:
- name: Handle failure
debug:
msg: "Command failed, executing rescue steps."
always:
- name: This runs always
debug:
msg: "Cleanup actions here."
📌 Block: Main logic
🛟 Rescue: Error आने पर fallback
🔄 Always: हमेशा चलेगा — चाहे error हो या ना हो
🔧 Extra Debugging with Verbosity:
Run-time debugging के लिए आप verbosity flags जोड़ सकते हैं:
ansible-playbook site.yml -v # verbose
ansible-playbook site.yml -vv # more verbose
ansible-playbook site.yml -vvv # even more
ansible-playbook site.yml -vvvv # connection debug
-vvvv SSH level तक debugging दिखाता है, helpful for network-related errors.
🧠 Common Troubleshooting Tips:
- ✅ Always test playbooks in staging before production
- ✅ Use dry-run mode:
--check
- ✅ Check inventory paths, YAML syntax, module parameters
- ✅ Look for indentation mistakes
📘 Chapter 9: Ansible Tags, Includes और Import Modules – Modularization का अगला Level
जैसे-जैसे आपकी playbooks बड़ी होती जाती हैं, उन्हें maintain करना और debug करना मुश्किल हो सकता है। Ansible इस challenge को handle करने के लिए कुछ advanced features देता है — जैसे Tags, Include, और Import modules।
🏷️ 1. Tags का उपयोग – Selective Execution
Tags से आप playbook के specific हिस्सों को run कर सकते हैं, बिना पूरी playbook चलाए। ये especially deployment, updates या debugging में useful होता है।
- name: Install Apache
apt:
name: apache2
state: present
tags: install
- name: Copy Configuration
template:
src: apache.conf.j2
dest: /etc/apache2/apache2.conf
tags: config
- name: Start Apache
service:
name: apache2
state: started
tags: start
Run only config tag:
ansible-playbook site.yml --tags "config"
---
📥 2. Includes और Imports – Task Split करना
जब आपकी playbook बहुत बड़ी हो जाए, तो आप उसे अलग-अलग फाइलों में तोड़ सकते हैं – जिससे readability और reusability बढ़ती है।
# site.yml
- name: Configure webserver
hosts: web
become: yes
tasks:
- include_tasks: install.yml
- import_tasks: configure.yml
🔹 include_tasks: runtime पर include होता है – dynamic
🔹 import_tasks: compile time पर include होता है – static
📦 3. Difference Between include vs import:
Feature | include_tasks | import_tasks |
---|---|---|
When it runs | Runtime | Compile time |
Supports conditionals | Yes | No (directly) |
Best for | Dynamic tasks | Static structure |
🚀 Use-Case Example:
मान लीजिए आप staging और production दोनों को handle कर रहे हैं:
- name: Choose env-specific config
include_tasks: "config_{{ env }}.yml"
when: env is defined
✅ अपने Playbooks को modular और scalable बनाएं
✅ Environment-specific files का इस्तेमाल करें
✅ Execution time को optimize करें
✅ Tags का उपयोग करके केवल ज़रूरी task ही चलाएं
📘 Chapter 10: Real-World Ansible Use-Cases और Project-Level Application (हिंदी में)
अब जब आपने Ansible की foundations सीख ली हैं, तो चलिए देखते हैं कि इसे वास्तविक प्रोजेक्ट्स और production-level environments में कैसे इस्तेमाल किया जाता है। नीचे दिए गए उदाहरणों में आपको industries, cloud, devops और sysadmin level पर Ansible की practical ताकत का अंदाज़ा मिलेगा।
🏢 Use-Case 1: Web Server Deployment Automation
मान लीजिए आपको 20 Ubuntu servers पर Apache और PHP deploy करने हैं – आप ये सब Ansible की एक ही playbook से कर सकते हैं।
# site.yml
- hosts: webservers
become: yes
tasks:
- name: Install Apache
apt:
name: apache2
state: present
- name: Install PHP
apt:
name: php
state: present
- name: Start Apache
service:
name: apache2
state: started
✅ Benefit: 10 मिनट में 20 सर्वर एक जैसा setup हो जाएगा।
---
☁️ Use-Case 2: AWS Infrastructure Provisioning (With boto + dynamic inventory)
- name: Launch EC2 Instances
hosts: localhost
connection: local
gather_facts: no
tasks:
- name: Create EC2 instance
ec2:
key_name: mykey
instance_type: t2.micro
image: ami-0abcdef1234567890
wait: yes
region: ap-south-1
count: 2
✅ Benefit: Manual console interaction की ज़रूरत नहीं, infrastructure as code!
---
🔁 Use-Case 3: Continuous Deployment with Git Pull
- name: Pull Latest Code
git:
repo: 'https://github.com/user/project.git'
dest: /var/www/html
version: main
✅ Benefit: Zero-downtime deployments across environments.
---
🔐 Use-Case 4: Secure Configuration Compliance
आप Ansible को security hardening या CIS compliance के लिए भी use कर सकते हैं। जैसे:
- name: Disable Root SSH Login
lineinfile:
path: /etc/ssh/sshd_config
regexp: '^PermitRootLogin'
line: 'PermitRootLogin no'
notify: Restart SSH
✅ Benefit: Compliance automation, बिना human error के।
---
💻 Use-Case 5: User और SSH Key Management (Multi-team Environments)
- name: Add Dev Team Users
user:
name: "{{ item.name }}"
groups: dev
shell: /bin/bash
with_items:
- { name: 'rahul' }
- { name: 'neha' }
✅ Benefit: Multiple users को centralized तरीके से deploy करना।
---
✅ Apache + MySQL + PHP Stack Deploy
✅ CI/CD Pipelines Setup (GitLab/Bitbucket)
✅ Kubernetes Node Bootstrap with Ansible
✅ Docker Compose Apps Setup (WordPress, Ghost, etc.)
✅ SSL Certificate Renewal & Monitoring
🔚 निष्कर्ष: क्या Ansible सीखना आपके लिए फायदेमंद है?
अगर आप एक DevOps Engineer, System Administrator या Cloud Architect बनना चाहते हैं, तो Ansible आपके टूलबॉक्स का एक अहम हिस्सा होना चाहिए। इसके YAML-based Playbooks, एजेंट-लेस आर्किटेक्चर, और automation capabilities ने इसे industry standard बना दिया है।
अब आपने Ansible की basics से लेकर real-world production use-cases तक सब कुछ सीख लिया है — आपको अब इसे छोटे projects में use करके practice करनी चाहिए और फिर gradually उसे CI/CD pipelines, Kubernetes bootstrap, या AWS provisioning जैसे advanced use-cases में apply करना चाहिए।
❓ अक्सर पूछे जाने वाले सवाल (FAQ)
Q1: क्या Ansible सीखने के लिए Python आनी चाहिए?
Answer: नहीं, लेकिन Python की basic understanding से module customization आसान हो जाता है।
Q2: क्या Ansible free है?
Answer: हां, Ansible एक open-source automation tool है। हालांकि Red Hat इसके enterprise version भी ऑफर करता है।
Q3: क्या Ansible Windows पर काम करता है?
Answer: हां, Windows को configure करने के लिए भी Ansible modules मौजूद हैं। लेकिन Ansible खुद Linux/macOS पर install होता है।
Q4: क्या Ansible को Terraform के साथ use किया जा सकता है?
Answer: हां, आप Terraform से infra provision कर सकते हैं और फिर Ansible से configuration manage कर सकते हैं।
🎁 Bonus: Ansible सीखने के लिए Recommended Resources
- 📘 Official Ansible Documentation
- 🎥 YouTube Playlist: “Ansible in Hindi” by Simplilearn, K21, or LearnCodeOnline
- 🌐 Ansible Galaxy – Public Roles & Playbooks
✉️ कोई सवाल है? Comment ज़रूर करें – हम जवाब देंगे!
✍️ लेखक के बारे में
अनुराग राय एक अनुभवी DevOps और Cloud तकनीकी लेखक हैं। इन्हें Hindi में तकनीकी विषयों को सरल भाषा में समझाने में महारत हासिल है। इनके ब्लॉग पर आपको Cloud, AI, Cybersecurity और Programming से जुड़े अनगिनत गाइड्स मिलेंगे।
Post a Comment
Blogger FacebookYour Comment Will be Show after Approval , Thanks