Interview Prep/Ansible/Variables, Facts and Precedence Problems

`'ansible_eth0' is undefined` on new cloud instances

BeginnerTroubleshootingVariables, Facts and Precedence Problems
Answer

Likely causes → Interface names depend on the hardware and OS: ens5 on Nitro EC2, enp0s3 on VirtualBox, eth0 on older images. Hard-coding eth0 breaks when the instance type changes.

Diagnose → ansible web01 -m setup -a 'filter=ansible_interfaces'

Fix → Use the interface-independent facts:

bind_ip: "{{ ansible_facts['default_ipv4']['address'] }}"

Prevent → Never reference a fact named after an interface in a role. Use default_ipv4, all_ipv4_addresses, or inventory variables.

All Ansible questions Roadmap: Ansible

More on Variables, Facts and Precedence Problems