# hostvars vs lookup(vars)

**URL:** <https://forum.ansible.com/t/hostvars-vs-lookup-vars/38723>\
**Category:** Ansible Project\
**Created:** [March 21, 2024, 1:25pm UTC](https://forum.ansible.com/t/hostvars-vs-lookup-vars/38723 "2024-03-21T13:25:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjwagner.dba](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@rjwagner.dba](https://forum.ansible.com/u/rjwagner.dba)\
**Post date:** [March 21, 2024, 1:25pm UTC](https://forum.ansible.com/t/hostvars-vs-lookup-vars/38723/1 "2024-03-21T13:25:03Z")

</div>

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?

Thanks  
Rob

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [March 21, 2024, 1:50pm UTC](https://forum.ansible.com/t/hostvars-vs-lookup-vars/38723/2 "2024-03-21T13:50:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rjwagner.dba](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@rjwagner.dba](https://forum.ansible.com/u/rjwagner.dba)\
**Post date:** [March 25, 2024, 3:14pm UTC](https://forum.ansible.com/t/hostvars-vs-lookup-vars/38723/3 "2024-03-25T15:14:31Z")

</div>

I think I got it. Thanks Brian

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [March 25, 2024, 4:07pm UTC](https://forum.ansible.com/t/hostvars-vs-lookup-vars/38723/4 "2024-03-25T16:07:49Z")

</div>

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.
