Hosts are renamed but keep the same IP, and facts caching still shows old names.
AdvancedScenarioInventory and Targeting
Answer
Situation read → Stale fact cache keyed by inventory_hostname.
What I'd do →
- Clear the cache entry: delete the file/key for the old name, or run
ansible-playbook ... --flush-cache. - Re-gather facts:
ansible renamed -m ansible.builtin.setup. - Reduce
fact_caching_timeoutwhere names change often.
Why → The cache key is the inventory name, so renamed hosts leave orphaned entries and new names start empty.
Interview tip → Cached facts speed up playbooks that use gather_facts: smart, but trade freshness.
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?BeginnerYour team keeps prod and staging hosts in one inventory file and someone ran a playbook on prod by mistake. How do you prevent it?IntermediateYou 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.Intermediate