As far as I understand it, fact variables are exposed for the current server that Ansible is operating on. So if I say: {{ ansible_default_ipv4.address }}, that is going to get evaluated for each host as I perform some operation using that variable.
I want that same variable, but for all hosts in the current play. Say I had three hosts I was operating against:
one: 98.0.0.1
two: 98.0.0.2
three: 98.0.0.3
I want to be able to do something like:
hosts={% for fact in facts %}
{{ fact[‘ansible_default_ipv4.address’] }},
{% endfor %}
and have that evaluated to “98.0.0.1,98.0.0.2,98.0.0.3”
inventory_hostname is a var worth mentioning: it's the hostname of the local system. useful for:
hostvars[inventory_hostname]['ansible_default_ipv4']
there's also a 'groups' top-level var you can use to iterate through the inventory, which may or may not be
better than iterating through hostvars to get your list of hosts. pardon, been a couple months since i touched
that particular use-case.
As far as I understand it, fact variables are exposed for the *current*
server that Ansible is operating on. So if I say: {{
ansible_default_ipv4.address }}, that is going to get evaluated for each
host as I perform some operation using that variable.
Not the case, actually.
Facts are available for all servers in the current play or talked to in any
previous play *OR* potentially all servers previously talked to if "fact
caching" is enabled.