Can anybody explain why I can access ansible_default_ipv4 in a task, but
not in a jinja2 file?
- debug: msg="{{ hostvars[inventory_hostname]['ansible_default_ipv4'] }}"
This gives an ipv4 address like 10.10.10.10
In roles/common/tasks/main.yml
- name: Template /etc/hosts using inventory default ipv4
template: src=hosts.j2 dest=/etc/hosts
TASK: [common | Template /etc/hosts using inventory default ipv4]
I suspect you probably haven’t talked to that host yet previously.
The solution would be to make sure you talk to the host in a previous play, even if that play contains no other facts than fact gathering.
(FWIW, fact caching is something we’re looking to add in a future release)
I discovered this “feature” today and found it rather perplexing. I’ve used the workaround of ‘gather_facts: yes’ to fix it in this case, but it would be nice to be able to pre-populate the hostvars array with playbook and/or command-line variables instead of having to rely on the fact gathering “hack.”