LOGBOOK

HELP

Quiz Entry - updated: 2026.11.05

What are magic variables, and what do hostvars, group_names, groups and inventory_hostname contain?

  • hostvars: the variables of every managed host, so one host can read another's.
  • group_names: the groups the current host belongs to.
  • groups: every group in the inventory and its hosts.
  • inventory_hostname: the current host's name as written in the inventory.

Magic variables seen from host web1: inventory_hostname is web1, group_names lists its groups, groups maps every group to its hosts, hostvars gives access to any host's variables

Magic variables are set by Ansible itself. Their names are reserved: don't define your own variables with these names.

Two details worth knowing:

  • hostvars contains a host's facts only once facts have been gathered for that host, in this play or an earlier play of the run. Before that, hostvars['db1']['ansible_facts'] is empty.
  • inventory_hostname can differ from ansible_facts['hostname']. The inventory might call a host web1 (or an IP address, or an alias with ansible_host) while the machine calls itself srv-4711. Use inventory_hostname when you mean "this inventory entry", and the fact when you mean what the OS reports.

Go deeper:

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