# Ability to dump all variables

**URL:** <https://forum.ansible.com/t/ability-to-dump-all-variables/16267>\
**Category:** Ansible Developer\
**Created:** [September 18, 2014, 8:06am UTC](https://forum.ansible.com/t/ability-to-dump-all-variables/16267 "2014-09-18T08:06:13Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Uvizhev](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@Alexander\_Uvizhev](https://forum.ansible.com/u/Alexander_Uvizhev)\
**Post date:** [September 18, 2014, 8:06am UTC](https://forum.ansible.com/t/ability-to-dump-all-variables/16267/1 "2014-09-18T08:06:13Z")

</div>

Hi,  
I need to dump all the vars available at certain steps of playbook so I tried to follow [this tip](https://coderwall.com/p/13lh6w).  
At first it looks like it does what I need but then I noticed that dumping `vars` gives me variables specified only in role’s vars file and no vars inherited from `group_vars/` or set by set\_fact module.  
Inspecting `lib/ansible/runner/ __init__.py` I see that vars inherited from `group_vars/` are contained in `host_variables` and those set by set\_fact module are in self.vars\_cache.get(host, {}).

So all variables I need are collected in inject dict in \_executor\_internal() but only some of them could be dumped because of subdict placement:

```auto
inject['vars'] = self.module_vars

```

What I do now is the same idea: I put inject dict as a subdict of itself just after filling it and before putting other vars to subdicts:

```auto
inject['universe'] = inject.copy()

```

which works but seems a bit ugly to me.

So I ask advice on how it should be implemented better.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [September 18, 2014, 12:59pm UTC](https://forum.ansible.com/t/ability-to-dump-all-variables/16267/2 "2014-09-18T12:59:00Z")

</div>

Hi Alexander,

To accomplish this, you could make modifications to the debug module. Just have it display self.runner.get\_inject\_vars(hostname), which should contain every variable (this function was just added a few days ago, so it’s in the devel branch only right now). You could make this a new parameter to the module, such as dump\_all=yes.

Let us know if you have any further questions regarding this, and if you get this working feel free to send us a PR for it as others may be interested in it.

Thanks!

---

<div class="post-metadata">

**Author:** ![Alexander\_Uvizhev](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@Alexander\_Uvizhev](https://forum.ansible.com/u/Alexander_Uvizhev)\
**Post date:** [September 18, 2014, 1:11pm UTC](https://forum.ansible.com/t/ability-to-dump-all-variables/16267/3 "2014-09-18T13:11:05Z")

</div>

Hi James,  
Thanks for your reply.  
Basically I need not to debug my playbook but to make all the variables available to another piece of code so I have to dump them to a JSON file on a remote machine.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [September 18, 2014, 1:16pm UTC](https://forum.ansible.com/t/ability-to-dump-all-variables/16267/4 "2014-09-18T13:16:43Z")

</div>

Ahh, in that case I would write a new module/action\_plugin to do that (essentially a clone of debug), so that you can access the internal variable structures. Just remember that this is generally a not-safe thing to do, since those variables may contain sensitive data like passwords or keys that your plays are using, so be careful with that.

---

<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:** [September 18, 2014, 1:26pm UTC](https://forum.ansible.com/t/ability-to-dump-all-variables/16267/5 "2014-09-18T13:26:02Z")

</div>

​O, in that case, this tool does exactly that:

[https://github.com/ginsys/ansible-plugins/blob/devel/bin/ansible-inventory](https://github.com/ginsys/ansible-plugins/blob/devel/bin/ansible-inventory)

The dump happens in the json format accepted by ansible from dynamic  
inventory scripts, but you can tweak it to the format you need.​

---

<div class="post-metadata">

**Author:** ![Alexander\_Uvizhev](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@Alexander\_Uvizhev](https://forum.ansible.com/u/Alexander_Uvizhev)\
**Post date:** [September 18, 2014, 1:46pm UTC](https://forum.ansible.com/t/ability-to-dump-all-variables/16267/6 "2014-09-18T13:46:38Z")

</div>

Thanks Serge! I will try it.

---

<div class="post-metadata">

**Author:** ![Alexander\_Uvizhev](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@Alexander\_Uvizhev](https://forum.ansible.com/u/Alexander_Uvizhev)\
**Post date:** [September 18, 2014, 1:50pm UTC](https://forum.ansible.com/t/ability-to-dump-all-variables/16267/7 "2014-09-18T13:50:36Z")

</div>

That's a good idea but at first I'd like to understand what logic is hidden behind all these  
inject['hostvars']  
inject['group\_names']  
inject['groups']  
inject['vars']  
inject['defaults']  
inject['environment']  
inject['playbook\_dir']  
inject['omit']

and why they contain only a part of all data.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [September 18, 2014, 1:55pm UTC](https://forum.ansible.com/t/ability-to-dump-all-variables/16267/8 "2014-09-18T13:55:53Z")

</div>

Those are essentially “magic” variables that we have added, so that they are available to tasks for use directly.

---

<div class="post-metadata">

**Author:** ![Alexander\_Uvizhev](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@Alexander\_Uvizhev](https://forum.ansible.com/u/Alexander_Uvizhev)\
**Post date:** [September 18, 2014, 2:04pm UTC](https://forum.ansible.com/t/ability-to-dump-all-variables/16267/9 "2014-09-18T14:04:18Z")

</div>

And why some of them are in a root of 'inject' dict and some in inject['vars'] for instance? Those in inject['vars'] are accessible in tasks directly as well.  
Why can't we add variables from 'group\_vars/' and 'set\_fact' module to some other places like inject['group\_vars'] and inject['set\_fact\_vars'] ?

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [September 18, 2014, 2:16pm UTC](https://forum.ansible.com/t/ability-to-dump-all-variables/16267/10 "2014-09-18T14:16:02Z")

</div>

Things added via set\_fact are available in the hostvars variable (ie. hostvars[‘hostname’].variable\_name). The hostvars contains all host\_vars and group\_vars variables too.

---

<div class="post-metadata">

**Author:** ![Alexander\_Uvizhev](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@Alexander\_Uvizhev](https://forum.ansible.com/u/Alexander_Uvizhev)\
**Post date:** [September 18, 2014, 2:38pm UTC](https://forum.ansible.com/t/ability-to-dump-all-variables/16267/11 "2014-09-18T14:38:18Z")

</div>

Yes, set\_fact variables are available in hostvars but variables which are defined in group\_vars/all.yml are not.

---

<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:** [September 18, 2014, 2:42pm UTC](https://forum.ansible.com/t/ability-to-dump-all-variables/16267/12 "2014-09-18T14:42:05Z")

</div>

​That is correct, but what is available in hostvars are the calculated  
variables per host.​

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [September 18, 2014, 2:47pm UTC](https://forum.ansible.com/t/ability-to-dump-all-variables/16267/13 "2014-09-18T14:47:32Z")

</div>

I don’t think that’s correct:

$ cat group\_vars/all

---

<div class="post-metadata">

**Author:** ![Alexander\_Uvizhev](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@Alexander\_Uvizhev](https://forum.ansible.com/u/Alexander_Uvizhev)\
**Post date:** [September 18, 2014, 3:14pm UTC](https://forum.ansible.com/t/ability-to-dump-all-variables/16267/14 "2014-09-18T15:14:08Z")

</div>

Aha, I found the problem.  
Try this:  
$ cat play.yml

- hosts: localhost  
gather\_facts: no  
tasks:
- debug: var=hostvars

$ ansible-playbook -vv play.yml | grep testing

It gives nothing. Andible do not resolve all dict correctly. Seems like templating limitation.

---

<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:** [September 18, 2014, 5:05pm UTC](https://forum.ansible.com/t/ability-to-dump-all-variables/16267/15 "2014-09-18T17:05:17Z")

</div>

​hostvars is not a normal dict, but uses caching that is only filled in  
when explictly getting a particular key, hence hostvars[inventory\_hostname]  
works

---

<div class="post-metadata">

**Author:** ![Alexander\_Uvizhev](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@Alexander\_Uvizhev](https://forum.ansible.com/u/Alexander_Uvizhev)\
**Post date:** [September 19, 2014, 7:29am UTC](https://forum.ansible.com/t/ability-to-dump-all-variables/16267/16 "2014-09-19T07:29:54Z")

</div>

Thanks gentlemen, I see the way now.

---

<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:** [September 24, 2014, 2:28am UTC](https://forum.ansible.com/t/ability-to-dump-all-variables/16267/17 "2014-09-24T02:28:57Z")

</div>

I’d be somewhat curious of teaching the debug module a new trick…

- debug: var=all

Or something like that.

Rather than it being a new module.

The coderwall tip is obsolete and because it’s hard to moderate coderwall, we’ve mostly stopped pointing to it I think - if you can still see a link to that in the docs, let me know.
