Interview Prep/Ansible/Variables, Facts and Precedence Problems

The generated `upstream {}` block contains `web01` instead of IPs, and Nginx cannot resolve the names

BeginnerTroubleshootingVariables, Facts and Precedence Problems
Answer

Likely causes → The template uses inventory_hostname, which is the inventory alias. The address to connect to is ansible_host, and the real interface IP is a fact.

Fix →

{% for h in groups['app'] %}
server {{ hostvars[h]['ansible_host'] | default(h) }}:8080;
{% endfor %}

Or use hostvars[h]['ansible_facts']['default_ipv4']['address'] if facts for those hosts have been gathered.

Prevent → Use inventory_hostname for labels and logs, and ansible_host or IP facts for network addresses.

All Ansible questions Roadmap: Ansible

More on Variables, Facts and Precedence Problems