Interview Prep/Ansible/Variables, Facts and Precedence Problems

Facts like `ansible_hostname` became undefined after someone changed `ansible.cfg`

IntermediateTroubleshootingVariables, Facts and Precedence Problems
Answer

Likely causes → inject_facts_as_vars = False was set. Facts are then only available under ansible_facts, not as top-level ansible_* variables. Recent ansible-core releases also warn about relying on the injected names.

Diagnose → ansible-config dump --only-changed | grep -i inject

Fix → Update the code to the namespaced form:

"{{ ansible_facts['hostname'] }}"   # not ansible_hostname
"{{ ansible_facts['os_family'] }}"  # not ansible_os_family

Prevent → Write all new roles with ansible_facts[...]. It is future-proof and does not collide with your own variables.

All Ansible questions Roadmap: Ansible

More on Variables, Facts and Precedence Problems