# Q on hostvars access to inventory variables ad-hoc vs playbook

**URL:** <https://forum.ansible.com/t/q-on-hostvars-access-to-inventory-variables-ad-hoc-vs-playbook/30248>\
**Category:** Ansible Project\
**Created:** [May 2, 2019, 4:48pm UTC](https://forum.ansible.com/t/q-on-hostvars-access-to-inventory-variables-ad-hoc-vs-playbook/30248 "2019-05-02T16:48:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![countryroad62](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@countryroad62](https://forum.ansible.com/u/countryroad62)\
**Post date:** [May 2, 2019, 4:48pm UTC](https://forum.ansible.com/t/q-on-hostvars-access-to-inventory-variables-ad-hoc-vs-playbook/30248/1 "2019-05-02T16:48:08Z")

</div>

I seem to be able to query hostvars for a particular host from the command line and get at the inventory variables  
but I haven’t figured out how to do this in a playbook. Might it be from hostvars being lazily loaded - or my lack of  
understanding how to access hostvars inventory variables successfully (or both).

I’ve search and haven’t found anything quite this specific - or actually simple (just give me the inventory vars for this host via hostvars).

thx  
jim

As an example, this works:

$ ansible prtmpftp -m debug -a “var=tags”  
prtmpftp | SUCCESS =\> {  
“tags”: {  
“Veeam\_Backup”: [  
“Linux\_Production\_Servers”  
]  
}  
}

I’m trying to do this:

---

<div class="post-metadata">

**Author:** ![countryroad62](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@countryroad62](https://forum.ansible.com/u/countryroad62)\
**Post date:** [May 15, 2019, 6:52pm UTC](https://forum.ansible.com/t/q-on-hostvars-access-to-inventory-variables-ad-hoc-vs-playbook/30248/2 "2019-05-15T18:52:25Z")

</div>

Let me simplify my issue. My inventory json includes the snippet above for a specific host. I’m looking for any vmware tags present. Ad-hoc debug gives this:

$ ansible prtmpftp -m debug -a “var=tags”  
prtmpftp | SUCCESS =\> {  
“tags”: {  
“Veeam\_Backup”: [  
“Linux\_Production\_Servers”  
]  
}  
}

$ ansible prtmpftp -m debug -a “var=hostvars[inventory\_hostname][‘tags’]”  
prtmpftp | SUCCESS =\> {  
“hostvars[inventory\_hostname][‘tags’]”: {  
“Veeam\_Backup”: [  
“Linux\_Production\_Servers”  
]  
}  
}

$ ansible prtmpftp -m debug -a “var=hostvars[inventory\_hostname][‘tags’][‘Veeam\_Backup’]”  
prtmpftp | SUCCESS =\> {  
“hostvars[inventory\_hostname][‘tags’][‘Veeam\_Backup’]”: [  
“Linux\_Production\_Servers”  
]  
}

However, in a playbook I can’t reference the tags in hostvars for this host:

---

<div class="post-metadata">

**Author:** ![flowerysong1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@flowerysong1](https://forum.ansible.com/u/flowerysong1)\
**Post date:** [May 15, 2019, 7:19pm UTC](https://forum.ansible.com/t/q-on-hostvars-access-to-inventory-variables-ad-hoc-vs-playbook/30248/3 "2019-05-15T19:19:16Z")

</div>

You told ansible-playbook to use ‘prtmpftp,’ as the inventory source; this does not include any host vars, since it’s a simple comma-separated list of hostnames. You should instead use the same inventory you used in your adhoc command by running `ansible-playbook hostvarstest.yml`

---

<div class="post-metadata">

**Author:** ![countryroad62](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@countryroad62](https://forum.ansible.com/u/countryroad62)\
**Post date:** [May 15, 2019, 7:50pm UTC](https://forum.ansible.com/t/q-on-hostvars-access-to-inventory-variables-ad-hoc-vs-playbook/30248/4 "2019-05-15T19:50:20Z")

</div>

That was it. Thanks!
