# How are debug: var=vars / var=hostvars supposed to work?

**URL:** <https://forum.ansible.com/t/how-are-debug-var-vars-var-hostvars-supposed-to-work/16523>\
**Category:** Ansible Project\
**Created:** [October 15, 2014, 8:31pm UTC](https://forum.ansible.com/t/how-are-debug-var-vars-var-hostvars-supposed-to-work/16523 "2014-10-15T20:31:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ross\_Rosen](https://avatars.discourse-cdn.com/v4/letter/r/a88e4f/32.png) [@Ross\_Rosen](https://forum.ansible.com/u/Ross_Rosen)\
**Post date:** [October 15, 2014, 8:31pm UTC](https://forum.ansible.com/t/how-are-debug-var-vars-var-hostvars-supposed-to-work/16523/1 "2014-10-15T20:31:50Z")

</div>

Per the documentation and this thread: [https://groups.google.com/d/msg/ansible-project/rE8aVu4LNS0/sGKweGPvffoJ](https://groups.google.com/d/msg/ansible-project/rE8aVu4LNS0/sGKweGPvffoJ)  
I _think_ that debug: var=hostvars should print all the variables in scope (for each host). I’m not sure what var=vars is - non-host-specific variables?

In either case, I’m getting extremely odd behavior that I can’t figure out. I’m sorry this is long, but it is hopefully easy to follow and I suspect is either a simple answer or there is a bug.

(some stuff removed for brevity or security)

Here is my command line:  
ansible-playbook playbook.yml --inventory-file dynamic\_inventory.sh --limit stage --user xxx --private-key xxx.pub -e IS\_VAGRANT=False -C

Here is the result of my dynamic inventory for host ‘stage’  
{  
“hosts”: [  
“192.xxx.xxx.176”  
],  
“vars”: {  
“HOST\_NAME”: “[stage.yyyyyyyyy.com](http://stage.yyyyyyyyy.com)”,  
“SERVER\_NAME”: “[stage.yyyyyyyyy.com](http://stage.yyyyyyyyy.com)”  
}  
}

Now here are the key plays, and results, from my playbook

- debug: var=vars

> > This looks pretty good (but note it doesn’t show variables from my inventory)

PLAY [all] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK: [baseUbuntu | debug var=vars] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [192.xxx.xxx.176] =\> {  
“vars”: {  
“DIFFTOOL”: “git --no-pager diff”,  
… \<DELETED - but _no_ HOST\_NAME\> …  
“IS\_VAGRANT”: “False”,  
… \<DELETED - but _no_ SERVER\_NAME\> …  
“\_original\_file”: “/Users/xxxxxx/tmp/thissite/ansible\_config/roles/baseUbuntu/tasks/main.yml”,  
“always\_run”: false,  
“changed\_when”: null,  
“delegate\_to”: null,  
… …  
“role\_uuid”: “160302ff-f06f-4a78-8204-f0fcc4a8c750”  
}  
}

- debug: var=hostvars

> > This looks wrong to me. Shouldn’t it either be a superset of vars and my inventory variables?  
> > (HOST\_NAME and SERVER\_NAME)

TASK: [baseUbuntu | debug var=hostvars] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [192.xxx.xxx.176] =\> {  
“hostvars”: {  
“192.xxx.xxx.176”: {  
“IS\_VAGRANT”: “False”  
}  
}  
}

Hmmm… maybe I I’m not properly setting my inventory variables.  
Nope! See ‘HOST\_NAME’ is properly set. Why doesn’t it show up in vars or hostvars?

- debug: msg=“HOST\_NAME = _{{ HOST\_NAME }}_”  
TASK: [baseUbuntu | debug msg=“HOST\_NAME = _{{ HOST\_NAME }}_”] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [192.xxx.xxx.176] =\> {  
“msg”: “HOST\_NAME = _[stage.yyyyyyyyy.com](http://stage.yyyyyyyyy.com)_”  
}

And this one is even weirder. I use {{ HOST\_NAME }} and in the “name” clause it doesn’t  
render and in the hostname clause it does!

- name: “Set hostname to {{ HOST\_NAME }}”  
hostname: name=“{{ HOST\_NAME }}”

TASK: [baseUbuntu | debug msg=“HOST\_NAME = _{{ HOST\_NAME }}_”] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [192.xxx.xxx.176] =\> {  
“msg”: “HOST\_NAME = _[stage.yyyyyyyyy.com](http://stage.yyyyyyyyy.com)_”  
}

I’m baffled - I’d appreciate any tips. Thank you!

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [October 20, 2014, 9:12pm UTC](https://forum.ansible.com/t/how-are-debug-var-vars-var-hostvars-supposed-to-work/16523/2 "2014-10-20T21:12:57Z")

</div>

“I _think_ that debug: var=hostvars should print all the variables in scope (for each host)”

It doesn’t do this, but rather looks for variable names inside hostvars.

You can think of the “hostvars” syntax as a way to reference variables for other hosts.

I guess whether it should work might be a technicality, but that’s what it was intended for.

---

<div class="post-metadata">

**Author:** ![Michael\_Hoglan](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@Michael\_Hoglan](https://forum.ansible.com/u/Michael_Hoglan)\
**Post date:** [October 22, 2014, 11:55pm UTC](https://forum.ansible.com/t/how-are-debug-var-vars-var-hostvars-supposed-to-work/16523/3 "2014-10-22T23:55:07Z")

</div>

Ive stopped doing  
debug: var=hostvars  
and do  
debug: var=hostvars[inventory\_hostname]  
or  
debug: var=hostvars[item]  
with\_items: play\_hosts

As you do not get all the vars outputted in when you do it the first way, but the second way I do. I am completely guessing as I have not dug into the code to see, but I am suspecting that hostvars is coming from a cache that is not up to date, and when you access the specific element in hostvars that is coming from a cache that contains the latest.

Thanks!  
Michael

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [October 23, 2014, 7:22am UTC](https://forum.ansible.com/t/how-are-debug-var-vars-var-hostvars-supposed-to-work/16523/4 "2014-10-23T07:22:02Z")

</div>

​Yes, 'hostvars' is implemented as a caching dictionary, which means you  
need to explicitly request vars for a particular key/host.​

---

<div class="post-metadata">

**Author:** ![Michael\_Hoglan](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@Michael\_Hoglan](https://forum.ansible.com/u/Michael_Hoglan)\
**Post date:** [October 23, 2014, 1:50pm UTC](https://forum.ansible.com/t/how-are-debug-var-vars-var-hostvars-supposed-to-work/16523/5 "2014-10-23T13:50:37Z")

</div>

I know there probably internal implementation details on why it is the way it is, but wouldn’t we want debug: var=hostvars to not come from the cache and be the latest truth of what is contained in hostvars? If I am doing a debug of the variable, I am already in a mode where performance is not a concern, I am trying to gather insight into the state of the system. So maybe if the debug task is used, the variable in the task should be fully interpolated, or unioned with the rest of dictionaries / lists that are accessed when specifying hostvars[hostname]

Thanks!  
Michael

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [October 24, 2014, 1:21pm UTC](https://forum.ansible.com/t/how-are-debug-var-vars-var-hostvars-supposed-to-work/16523/6 "2014-10-24T13:21:37Z")

</div>

“but wouldn’t we want debug: var=hostvars to not come from the cache and be the latest truth of what is contained in hostvars”

Serge mispoke a bit above. The problem is not that hostvars is a cache, but rather that hostvars is an object that provides access to individual variables.

We aren’t really concerned with:

- debug: var=hostvars

as it’s a very unusual thing to do.

typically this is how vars are shown with debug:

- hosts: webservers  
tasks:
- debug: var=foo

And the “hostvars” trick is only needed when accessing variables from _other_ servers, typically in templates for instance when building out something like an iptables config.
