`'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_default_ipv4'` when templating `hostvars['db01']`
IntermediateTroubleshootingVariables, Facts and Precedence Problems
Answer
Likely causes → Facts for db01 were never gathered in this run. Either db01 is not in the current play, or --limit web* excluded it, so only inventory variables exist for it.
Diagnose →
- ansible.builtin.debug:
msg: "{{ hostvars['db01'].keys() | list }}"
Fix → Gather facts for the database hosts first:
- hosts: dbservers
gather_facts: true
tasks: []
- hosts: webservers
tasks: ...
Alternatively, use ansible.builtin.setup with delegate_to: "{{ item }}" and delegate_facts: true over groups['dbservers'], or turn on a fact cache.
Prevent → Put stable addresses in inventory variables (db_private_ip) instead of relying on another host's facts.
More on Variables, Facts and Precedence Problems
Variables in `group_vars/webservers.yml` are silently ignoredBeginnerYou set `nginx_port: 8443` in inventory `group_vars`, but the role still uses `80`Intermediate`set_fact: release_tag=v2` runs, but later tasks still see `v1`Intermediate`'ansible_eth0' is undefined` on new cloud instancesBeginnerFacts like `ansible_hostname` became undefined after someone changed `ansible.cfg`Intermediate`web01` is in both `prod` and `eu` groups, and `ntp_server` has a different value on every run you checkAdvanced