This question is targeted at Matt Martz and Brian Coca (but anyone should feel free to jump in). I recently asked a question about how to access a variable in a task. Matt, you recommended hostvars[inventory_hostname][item], and Brian, you recommended lookup(‘vars’, item). I remember a discussion that included you both a few years ago where Matt, you promoted hostvars, and Brian, you promoted vars, but I can’t remember where it was (freenode? github?). So each of you has a preference, and I’m curious why. Both are documented approaches, but I don’t see any documentation around the subtleties associated with each Care to share?
Both are valid approaches but do different things, hostvars will
provide a untemplated subset of what you can get through the vars
lookups for the case of 'inventory_hostname', but the lookups do not
currently expose hostvars for other hosts.
A small rephrase as my statement above is not fully true:
hostvars are templated but restricted to variables in the hostvars for
that host itself, so hostvars that depend on variables in other scopes
are not templated. Under the hood templating fails but this is non
fatal as we expect it often, so we return the original template.
Using the vars directly in the context of the host elsewhere will '
just work'TM as there the rest of the vars exist in that context.