⚡ ~/naveed Interview Prep
⚡ Portfolio Home ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Ansible Automation Interview Questions Scenario 5 of 15 in Ansible Automation
Senior DevOps / SRE [L2] Ansible Cloud Dynamic Inventory Production Scenario [L2]
🎯 Target Role / Context: Senior DevOps / Cloud Engineer Scenario #5 · Dynamic Cloud Inventory

Q: You manage 500+ AWS EC2 instances across multiple environments. How do you implement Ansible AWS dynamic inventories with keyed groups and tag filtering for zero maintenance?

Replace static hosts files with the official amazon.aws.aws_ec2 YAML inventory plugin. Query live AWS APIs to discover instances dynamically, filter by running states, and organize hosts using keyed_groups based on AWS tags.

#Ansible #AWS #EC2 #Auto Scaling #Dynamic Inventory
🎙️ Candidate Opening & Architectural Context
""In modern autoscaling cloud environments, instance lifecycles are ephemeral. Static IP inventories result in immediate configuration drift and SSH unreachable errors. I solve this by configuring the amazon.aws.aws_ec2 inventory plugin with tag-based keyed groups and private IP resolution.""
Advertisement
⚡ Recommended Practice Lab

Want to master this scenario in a live sandbox? KodeKloud's CKA & CKAD Hands-On Certification Track covers this exact problem with hands-on terminal drills.

🛠️ Production Runbook & Step-by-Step Resolution

1️⃣

Create Declarative aws_ec2.yml Config

Configure the official AWS collection inventory plugin file (must end with aws_ec2.yml or aws_ec2.yaml):

  • Specify plugin: plugin: amazon.aws.aws_ec2
  • Define target regions: regions: [us-east-1, eu-west-1]
  • Filter out terminated or stopping instances: filters: { instance-state-name: running }
  • Resolve hostnames via private IP: hostnames: [private-ip-address]
2️⃣

Construct Dynamic Groups Using AWS Tags

Automatically classify instances into Ansible host groups based on their AWS resource tags:

  • keyed_groups:
  • - key: tags.Environment   prefix: env → creates env_production, env_staging
  • - key: tags.Role   prefix: role → creates role_webserver, role_api
Advertisement
3️⃣

Verify and Target Dynamic Fleets

Audit the discovered infrastructure tree before executing configuration updates:

  • Verify inventory tree: ansible-inventory -i aws_ec2.yml --graph
  • Inspect metadata in JSON: ansible-inventory -i aws_ec2.yml --list
  • Execute playbook against dynamic group: ansible-playbook -i aws_ec2.yml deploy.yml --limit 'role_webserver:&env_production'
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"The amazon.aws.aws_ec2 plugin queries AWS APIs in real time, eliminating static inventory decay. Grouping instances with keyed_groups allows precise, tag-driven targeting of autoscaling fleets across VPCs and availability zones."
⚡ 60-Second Elevator Pitch Talking Points
  • Static inventory files fail in autoscaling architectures where instance IDs and IP addresses change constantly.
  • Deploy the 'amazon.aws.aws_ec2' inventory plugin configured in 'aws_ec2.yml' with live AWS API queries.
  • Filter instances to only running states: 'filters: instance-state-name: running'.
  • Generate dynamic host groups using 'keyed_groups' mapped to AWS tags (Environment, Role, Service).
  • Set 'hostnames: private-ip-address' for secure private VPC communication via bastion/VPN.
  • Validate discovered infrastructure with 'ansible-inventory -i aws_ec2.yml --graph' before launching playbooks.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. Naming the inventory file without the required aws_ec2.yml suffix Ansible's auto plugin detection ignores files that do not match the *aws_ec2.yml naming pattern.
  2. Defaulting to public IP hostnames for instances in private subnets Private EC2 instances lack public IPs; playbooks fail with connection timeouts unless private-ip-address is declared.
  3. Failing to filter instance-state-name: running Ansible attempts to connect over SSH to stopped and terminating instances, causing timeouts and failed plays.
Advertisement
Want more Ansible Automation scenarios?
Explore our complete collection of scenario-based Ansible Automation interview runbooks.
Browse All Ansible Automation Questions →