Your team keeps prod and staging hosts in one inventory file and someone ran a playbook on prod by mistake. How do you prevent it?
IntermediateScenarioInventory and Targeting
Answer
Situation read → Environment separation is weak; the blast radius is controlled by a human typing the right -l.
What I'd do →
- Split into
inventories/staging/andinventories/prod/, each with its ownhostsandgroup_vars/. - Make staging the default in
ansible.cfg(inventory = inventories/staging). - Require
-i inventories/prodexplicitly, and add a pre-task guard for prod. - In CI, only the prod pipeline job has the prod credentials.
- hosts: all
pre_tasks:
- ansible.builtin.pause:
prompt: "Type PROD to continue"
register: confirm
when: env_name == 'prod'
- ansible.builtin.assert:
that: confirm.user_input == 'PROD'
when: env_name == 'prod'
Why → Separate inventories mean separate variables and separate credentials, so a typo cannot cross environments.
Interview tip → Mention that group_vars per environment also prevents prod secrets leaking into staging runs.
More on Inventory and Targeting
You are handed a fresh Ansible project and 10 servers. How do you make sure Ansible can reach all of them before writing any playbook?BeginnerYou need to run a playbook on only the web servers in Mumbai that are not in the canary group. How?BeginnerServers are autoscaled in AWS and IPs change daily. How do you keep the inventory accurate?IntermediateA host is reachable by SSH on port 2222 with a different user from all others. How do you model that?BeginnerYou must configure servers in a private subnet that is only reachable through a bastion.IntermediateYou want to run a task on every host in the `db` group and also on localhost in the same playbook.Beginner