Interview Prep/Ansible/Inventory and Targeting

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 →

  1. Split into inventories/staging/ and inventories/prod/, each with its own hosts and group_vars/.
  2. Make staging the default in ansible.cfg (inventory = inventories/staging).
  3. Require -i inventories/prod explicitly, and add a pre-task guard for prod.
  4. 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.

All Ansible questions Roadmap: Ansible

More on Inventory and Targeting