Hi,
I need to dump all the vars available at certain steps of playbook so I tried to follow this tip.
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:
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:
inject['universe'] = inject.copy()
which works but seems a bit ugly to me.
So I ask advice on how it should be implemented better.
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!
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.
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.
O, in that case, this tool does exactly that:
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.
Thanks Serge! I will try it.
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.
Those are essentially “magic” variables that we have added, so that they are available to tasks for use directly.
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'] ?
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.
Yes, set_fact variables are available in hostvars but variables which are defined in group_vars/all.yml are not.
That is correct, but what is available in hostvars are the calculated
variables per host.
I don’t think that’s correct:
$ cat group_vars/all
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.
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
Thanks gentlemen, I see the way now.
I’d be somewhat curious of teaching the debug module a new trick…
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.