# get all nodes of hostgroup within playbook

**URL:** <https://forum.ansible.com/t/get-all-nodes-of-hostgroup-within-playbook/24313>\
**Category:** Ansible Project\
**Created:** [April 25, 2017, 7:25pm UTC](https://forum.ansible.com/t/get-all-nodes-of-hostgroup-within-playbook/24313 "2017-04-25T19:25:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gthielebein](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@gthielebein](https://forum.ansible.com/u/gthielebein)\
**Post date:** [April 25, 2017, 7:25pm UTC](https://forum.ansible.com/t/get-all-nodes-of-hostgroup-within-playbook/24313/1 "2017-04-25T19:25:37Z")

</div>

I need to workaround a bug in “virt\_net” module when more then a network is created.

For this the task uses the iterates explicitly through all hosts of the group instead of performing the task in the context of the _inventory\_hostname_:

`

- name: “gather neccessary libvirt networks”  
set\_fact: virt\_networks=“{{ virt\_networks | default({}) | combine({hostvars[item].cidr:hostvars[item].network}) }}”  
with\_items:  
“{{ libvirt.nodes }}”  
`

libvirt.group is extra defined in the main variable section of my playbook and references manually the hosts group.

`  
variables:

- libvirt:  
nodes: “{{ groups[‘ovirtm’] }}”  
`

Can I get the list of all nodes of my hostgroup without extra defining it in the vars section?

Only found that something like this works, but this may not return the correct group…

`ansible -m debug -a msg="{{ groups[hostvars[inventory_hostname].group_names[0]] }}"`
