Interview Prep/Ansible/Inventory and Targeting

A colleague hard-coded IP addresses in playbooks. How do you remove them?

BeginnerScenarioInventory and Targeting
Answer

Situation read → Data is baked into code.

What I'd do →

  1. Put addresses in inventory variables (ansible_host, db_host).
  2. Refer to them with hostvars or group lookups in templates.
db_host: "{{ hostvars[groups['db'][0]].ansible_host }}"

Why → Moving data to inventory means the same playbook works in every environment.

Interview tip → Prefer inventory hostnames and DNS where you can; IPs only as ansible_host.

All Ansible questions Roadmap: Ansible

More on Inventory and Targeting