# Where are my inventory variables in a dump hostvars,

**URL:** <https://forum.ansible.com/t/where-are-my-inventory-variables-in-a-dump-hostvars/19219>\
**Category:** Ansible Project\
**Created:** [September 10, 2015, 2:15am UTC](https://forum.ansible.com/t/where-are-my-inventory-variables-in-a-dump-hostvars/19219 "2015-09-10T02:15:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Larry\_Fast](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Larry\_Fast](https://forum.ansible.com/u/Larry_Fast)\
**Post date:** [September 10, 2015, 2:15am UTC](https://forum.ansible.com/t/where-are-my-inventory-variables-in-a-dump-hostvars/19219/1 "2015-09-10T02:15:52Z")

</div>

When I do a dump of hostvars I get a different result than when I dump hostvars[hostname]. Things like group\_names, inventory\_hostname and variables defined in the inventory are not shown in a dump of hostvars. Am I imagining things?

By ‘dump’ I mean a jinja2 template with the following code

{{ hostvars | to\_nice\_json }} - does not include group\_names, inventory\_name, etc  
{{ hostvars[‘my\_hostname’] | to\_nice\_json }} - does include group\_names, etc

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [September 10, 2015, 2:53pm UTC](https://forum.ansible.com/t/where-are-my-inventory-variables-in-a-dump-hostvars/19219/2 "2015-09-10T14:53:13Z")

</div>

hostvars is not a variable, its an object that pretends to be variable  
but lazily loads things, so only the 2nd form is guaranteed to have  
all the vars.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [September 11, 2015, 12:21am UTC](https://forum.ansible.com/t/where-are-my-inventory-variables-in-a-dump-hostvars/19219/3 "2015-09-11T00:21:54Z")

</div>

or you can :

{  
"hostvars": {  
{% for key in groups["all"] %}  
"{{key}}": {{ hostvars[key] |to\_nice\_json }}{% if not loop.last %},{% endif %}  
{% endfor %}

}

---

<div class="post-metadata">

**Author:** ![Larry\_Fast](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Larry\_Fast](https://forum.ansible.com/u/Larry_Fast)\
**Post date:** [September 11, 2015, 11:58pm UTC](https://forum.ansible.com/t/where-are-my-inventory-variables-in-a-dump-hostvars/19219/4 "2015-09-11T23:58:11Z")

</div>

> Thanks Brian,

I was hoping someone would show me “if not loop.last” 🙂
