Where are my inventory variables in a dump hostvars,

71 views
Skip to first unread message

Larry Fast

unread,
Sep 9, 2015, 10:15:52 PM9/9/15
to Ansible Project
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

Brian Coca

unread,
Sep 10, 2015, 10:53:18 AM9/10/15
to Ansible Project
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.


--
Brian Coca
Message has been deleted

Brian Coca

unread,
Sep 10, 2015, 8:22:06 PM9/10/15
to Ansible Project
or you can :

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

}


--
Brian Coca

Larry Fast

unread,
Sep 11, 2015, 7:58:11 PM9/11/15
to Ansible Project
Thanks Brian,
I was hoping someone would show me "if not loop.last" :) 
Reply all
Reply to author
Forward
0 new messages