LOGBOOK

HELP

Quiz Entry - updated: 2026.11.05

How do you read a fact of another host, and what has to happen first?

Use hostvars: hostvars['demo2.example.com']['ansible_facts']['interfaces']. It only works if facts for demo2 were already gathered earlier in this play or in a previous play of the same run.

Play 1 gathers facts on db1, play 2 on the web servers reads db1's IP through hostvars; without play 1 the lookup is empty

* hostvars only has another host's facts once they were gathered earlier in the run. *

- name: Print list of network interfaces for demo2
  ansible.builtin.debug:
    var: hostvars['demo2.example.com']['ansible_facts']['interfaces']

The classic use: a web server's configuration needs the database server's IP address. The web servers can read it from hostvars['db1']['ansible_facts']['default_ipv4']['address'], provided a play that ran on db1 gathered its facts first. If it didn't, the lookup fails because the value isn't there yet.

It works for regular variables too, not just facts (hostvars['db1']['db_port']).

Note that this task runs on every host in the play, so with 50 hosts it prints demo2's interfaces 50 times. When you only need to use one host's information to configure others, write the task so it reads the value once and applies it, rather than repeating the same lookup everywhere.

You can refresh facts at any point with a setup task, at the cost of a longer run.

Go deeper:

From Quiz: ITIA / Ansible Variables, Secrets and Facts | Updated: Nov 05, 2026