Skip to main content

Command Palette

Search for a command to run...

Day 11 — Inventory Masterclass

Updated
•3 min read•View as Markdown
S

Who is a highly motivated and results-oriented Software Engineer with a passion for crafting robust and user-friendly web applications. Possesses strong expertise in both backend development and full-stack development, leveraging a diverse skillset to design, develop, and deploy scalable software solutions.

Inventory = Aapke Ansible ke servers ki list + unke variables.


⭐ PART 1 — Static Inventory (hosts file)

Default location:

/etc/ansible/hosts

Example:

[web]
192.168.1.10
192.168.1.11

[db]
192.168.1.20

Run command:

ansible web -m ping
ansible db -m ping

⭐ PART 2 — Inventory with Variables

1️⃣ Host Variables

[web]
192.168.1.10 ansible_user=ubuntu ansible_port=22

2️⃣ Group Variables (VERY IMP)

[web]
192.168.1.10
192.168.1.11

[web:vars]
env=production
app_port=8080

⭐ PART 3 — Group of Groups (parent groups)

[frontend]
192.168.1.11

[backend]
192.168.1.20

[production:children]
frontend
backend

Run:

ansible production -m ping

⭐ PART 4 — Folder Inventory (BEST PRACTICE)

Real DevOps setup:

inventories/
   dev/
      hosts
      group_vars/
         web.yml
         db.yml
   prod/
      hosts
      group_vars/
         web.yml
         db.yml

Playbook:

ansible-playbook -i inventories/prod/hosts site.yml

⭐ PART 5 — Host Vars & Group Vars Folder (PRO LEVEL)

Example:

inventory/
   hosts
   host_vars/
       web1.yml
       db1.yml
   group_vars/
       web.yml
       db.yml

Example — host_vars/web1.yml

server_name: web01
port: 80

Example — group_vars/web.yml

nginx_port: 8080

⭐ PART 6 — Dynamic Inventory – AWS EC2 Discovery (Super Important)

Dynamic inventory cloud se servers automatically fetch karta hai (manual entry nahi).


⭐ AWS Dynamic Inventory Example

You can use plugin:

aws_ec2

Enable it in a file:

inventory_aws.aws_ec2.yml

File:

plugin: aws_ec2
regions:
  - ap-south-1
filters:
  tag:Environment: production
hostnames:
  - tag:Name
compose:
  ansible_host: public_ip_address

Run:

ansible-inventory -i inventory_aws.aws_ec2.yml --list

Playbook run:

ansible-playbook -i inventory_aws.aws_ec2.yml deploy.yml

✔ No need to manually write IP
✔ Auto-detect EC2 instances
✔ Filters by tag
✔ Uses public IP automatically


⭐ PART 7 — INI vs YAML Inventory

INI format:

[web]
192.168.1.10
all:
  children:
    web:
      hosts:
        192.168.1.10:

⭐ PART 8 — Multiple Inventory Files

ansible-playbook -i dev_hosts -i prod_hosts site.yml

Combines both inventories.


⭐ PART 9 — Inventory Facts (Useful)

Ansible automatically collect karta hai:

ansible_facts['distribution']
ansible_facts['hostname']
ansible_facts['default_ipv4']['address']
ansible_facts['memory_mb']['real']

Playbook me use hoga:

msg: "Server IP is {{ ansible_facts['default_ipv4']['address'] }}"

⭐ PART 10 — Real-World Production Inventory Example

inventories/
   prod/
      hosts
      group_vars/
         web.yml
         db.yml
      host_vars/
         app01.yml
         db01.yml

hosts:

[web]
app01 ansible_host=3.110.23.11
app02 ansible_host=3.110.92.44

[db]
db01 ansible_host=3.109.55.90

group_vars/web.yml:

nginx_port: 80
env: production

⭐ DAY 11 PRACTICE TASKS


✅ Task 1 — Static Inventory

Folder:

inventories/dev/
inventories/prod/

Dev servers:

192.168.56.10
192.168.56.11

Prod servers:

3.110.10.10
3.110.10.11

✅ Task 2 — Group Vars

For [web] group make group_vars/web.yml:

env: production
port: 8080

✅ Task 3 — host_vars

For host: 3.110.10.10

File: host_vars/3.110.10.10.yml

server_name: web-prod-1

✅ Task 4 — Group of Groups

Make parent group:

[app:children]
web
backend

🚀 Task 5 (Hard — PRO Level)

Create AWS Dynamic Inventory:

inventory_aws.aws_ec2.yml

Filters:

  • region = ap-south-1

  • instances with tag:Env = dev

Print inventory:

ansible-inventory -i inventory_aws.aws_ec2.yml --list
2 views