# Function get\_group\_variables() not working?

**URL:** <https://forum.ansible.com/t/function-get-group-variables-not-working/15040>\
**Category:** Ansible Developer\
**Created:** [April 17, 2014, 1:44pm UTC](https://forum.ansible.com/t/function-get-group-variables-not-working/15040 "2014-04-17T13:44:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jurgen\_Haas](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@Jurgen\_Haas](https://forum.ansible.com/u/Jurgen_Haas)\
**Post date:** [April 17, 2014, 1:44pm UTC](https://forum.ansible.com/t/function-get-group-variables-not-working/15040/1 "2014-04-17T13:44:24Z")

</div>

It looks like the function get\_group\_variables() isn’t working. Here is some example code:

from ansible import inventory

inventory\_manager = inventory.Inventory(options.inventory)  
hv = inventory\_manager.get\_variables(‘hostname’)  
gv = inventory\_manager.get\_group\_variables(‘groupname’)

“hv” is getting a dictionary with all the variables but “gv” is always empty. Is that a bug or a feature?

---

<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:** [April 17, 2014, 10:13pm UTC](https://forum.ansible.com/t/function-get-group-variables-not-working/15040/2 "2014-04-17T22:13:09Z")

</div>

I’d suggest taking a look about how it is being used in core Ansible code, but I’m also wondering if there isn’t an inventory configuration to debug first…

Can you share details of your inventory?

–Michael

---

<div class="post-metadata">

**Author:** ![Jurgen\_Haas](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@Jurgen\_Haas](https://forum.ansible.com/u/Jurgen_Haas)\
**Post date:** [April 21, 2014, 8:50am UTC](https://forum.ansible.com/t/function-get-group-variables-not-working/15040/3 "2014-04-21T08:50:38Z")

</div>

It seems that the function is never used in Core.

So I’ve created an inventory and posted that to [https://gist.github.com/jurgenhaas/11136532](https://gist.github.com/jurgenhaas/11136532) (the directory structure is broken: there is a hosts and a test.py in the root, and a couple of files in subdirectories group\_vars and host\_vars). test.py is taken from original ansible script and a couple of lines added at the end to grab and print variables.

When you then run `./test.py all -i ./hosts -m setup` you get empty variables for the group and all the variables for the host. I think that demonstrates the problem.

---

<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:** [April 21, 2014, 10:28am UTC](https://forum.ansible.com/t/function-get-group-variables-not-working/15040/4 "2014-04-21T10:28:46Z")

</div>

Hi,

I can confirm Inventory.get\_group\_variables(‘groupname’) doesn’t do anything usefull currently, it just calls for group.get\_variables() - which basically return groups.vars - and caches it’s results, but groups.vars never gets any data filled in (at least not with ini files, it is used I believe with scripts).

Keep in mind that host and groups vars are data that is parsed by the group vars plugins, and as such is technically not a direct part of the inventory itself, as they are picked up late by runner calls, and per host, not per group.

This is one of the things that is addressed by my PR “vars\_plugins refactor and move into core inventory #6734” ([https://github.com](https://github.com)/ansible/ansible/pull/6734)  
​​

Serge

---

<div class="post-metadata">

**Author:** ![Jurgen\_Haas](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@Jurgen\_Haas](https://forum.ansible.com/u/Jurgen_Haas)\
**Post date:** [April 22, 2014, 7:50am UTC](https://forum.ansible.com/t/function-get-group-variables-not-working/15040/5 "2014-04-22T07:50:40Z")

</div>

Sounds good and I hope your PR makes it into core soon.

---

<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:** [April 22, 2014, 6:57pm UTC](https://forum.ansible.com/t/function-get-group-variables-not-working/15040/6 "2014-04-22T18:57:31Z")

</div>

Yep, we’re currently attacking the “P2” ticket queue, once this gets reduced to 0 we’ll see about testing all of this out. I’m interested in this very much as well, though the consequences of merging this close to release are a little dangerous (as I would say this of any large patch set, not singling out this one).

---

<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:** [April 22, 2014, 7:03pm UTC](https://forum.ansible.com/t/function-get-group-variables-not-working/15040/7 "2014-04-22T19:03:28Z")

</div>

​I definitely agree. I looked at lots of use cases and lots of tests, and  
even then I - later - encountered lots of other cases. I'm pretty sure  
there still will be corner cases to roughen out when testing at large.​
