Interview › Infrastructure as Code (Terraform, Ansible)
What is an Ansible inventory, and what is the difference between static and dynamic inventory?
Infrastructure as Code (Terraform, Ansible) · Intermediate level
Answer
An Ansible inventory lists the hosts and groups Ansible can manage. Static inventory is a file in INI or YAML. Dynamic inventory is generated from an external source such as AWS, Azure, GCP, VMware, or CMDB, so host lists and metadata stay current automatically.
Technical explanation
Inventory groups model roles, environments, regions, and lifecycle stages.
Dynamic inventory prevents stale host lists when instances scale up or down.
Inventory variables should describe differences, not hide application logic.
Prefer idempotent modules over shell so repeated runs are safe and change reporting is meaningful.
Separate reusable role logic from inventory-specific variables so the same automation works across environments.
Run lint, syntax checks, check mode where useful, and staged rollouts before production-wide changes.
Hands-on example
1. Create a minimal Ansible control workflow for: What is an Ansible inventory, and what is the difference between static and dynamic inventory?
2. Inventory example:
[web]
web1 ansible_host=10.0.1.10 ansible_user=ec2-user
web2 ansible_host=10.0.1.11 ansible_user=ec2-user
[web:vars]
ansible_become=true3. Playbook example:
---
- name: Configure web hosts
hosts: web
become: true
tasks:
- name: Ensure nginx is installed
ansible.builtin.package:
name: nginx
state: present
- name: Ensure nginx is running
ansible.builtin.service:
name: nginx
state: started
enabled: true
4. Run ansible -m ping web first, then ansible-playbook site.yml --check --diff, then the real run.Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Infrastructure as Code (Terraform, Ansible) interview questions
- How does Ansible connect to managed hosts?
- What is a playbook, a play, and a task?
- What is Ansible, and how is it agentless?
- What is idempotency in Ansible, and why does it matter?
- What is OpenTofu, and why did it fork from Terraform?
- Why is a raw shell or command task not idempotent, and how do you make it safe?
- What is the difference between Terraform and CloudFormation, and when choose each?
- What is the creates argument on a command task, and how does it add idempotency?