# group\_by adding all hosts?

**URL:** <https://forum.ansible.com/t/group-by-adding-all-hosts/14727>\
**Category:** Ansible Project\
**Created:** [March 7, 2014, 3:38pm UTC](https://forum.ansible.com/t/group-by-adding-all-hosts/14727 "2014-03-07T15:38:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_R](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@Matthew\_R](https://forum.ansible.com/u/Matthew_R)\
**Post date:** [March 7, 2014, 3:38pm UTC](https://forum.ansible.com/t/group-by-adding-all-hosts/14727/1 "2014-03-07T15:38:29Z")

</div>

Hi,

I’m pulling my hair out on this, but it’s probably something simple. I’m trying to create a dynamic group that has all our OpenVZ Guests in it.

So, in my ‘site.yml’ I have:

`

- name: Setup Groups  
hosts: all  
tasks:
- name: Create Groups based on OS  
group\_by: key=dist\_{{ ansible\_distribution }}
- name: Create VZ Guest Group  
group\_by: key={{ ansible\_virtualization\_type }}-{{ ansible\_virtualization\_role }}  
`

My problem is, it seems to be putting **every** host in this group:

`TASK: [Create VZ Guest Group] ************************************************* changed: [hostx.umaryland.edu] => {"changed": true, "groups": {"openvz-guest": ["hostx", "hosty", "hostz"]}}`

‘hostz’ is _definitely_ not a OpenVZ guest- in fact, if you run the ‘setup’ module against hostz, ‘ansible\_virtualization\_type’ and ‘ansible\_virtualization\_role’ aren’t even returned in the output.

For what it’s worth, I’m running ansible 1.4.3 from the EPEL repository.

What am I missing? I’m referencing:

[https://github.com/ansible/ansible-examples/blob/master/language\_features/group\_by.yml](https://github.com/ansible/ansible-examples/blob/master/language_features/group_by.yml)

and

[http://docs.ansible.com/group\_by\_module.html](http://docs.ansible.com/group_by_module.html)

Thanks,  
Matt

---

<div class="post-metadata">

**Author:** ![Brian\_Green](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@Brian\_Green](https://forum.ansible.com/u/Brian_Green)\
**Post date:** [March 10, 2014, 5:35am UTC](https://forum.ansible.com/t/group-by-adding-all-hosts/14727/2 "2014-03-10T05:35:50Z")

</div>

I ran into odd issues when using group names with hyphens in them. Similar situation of not using variable names with hyphens in them. Not sure if this is the case but its worth mentioning.

---

<div class="post-metadata">

**Author:** ![Matthew\_R](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@Matthew\_R](https://forum.ansible.com/u/Matthew_R)\
**Post date:** [March 10, 2014, 1:51pm UTC](https://forum.ansible.com/t/group-by-adding-all-hosts/14727/3 "2014-03-10T13:51:09Z")

</div>

Hi Brian,

Thanks for the tip. I actually tried changing the hyphen to an underscore, but no luck.

I think what’s happening is that on Linux systems that aren’t virtualized, the ‘ansible\_virtualization\_\*’ facts aren’t getting set at all, so the grouping mechanism can’t handle it.

I’m thinking about submitting a patch in the ‘setup’ module that if it falls through to the end in Linux to set ‘ansible\_virtualization\_\*’ to ‘NA’ like setup does for everything else it can’t figure out. Then, at least, non-virtual machines would be grouped into the ‘NA’ group, which is good enough for my purposes.

Matt
