# I want to show  the group variables, looping over all groups

**URL:** <https://forum.ansible.com/t/i-want-to-show-the-group-variables-looping-over-all-groups/37633>\
**Category:** Ansible Project\
**Created:** [February 8, 2023, 8:21am UTC](https://forum.ansible.com/t/i-want-to-show-the-group-variables-looping-over-all-groups/37633 "2023-02-08T08:21:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Norbert\_Klamann](https://avatars.discourse-cdn.com/v4/letter/n/ce73a5/32.png) [@Norbert\_Klamann](https://forum.ansible.com/u/Norbert_Klamann)\
**Post date:** [February 8, 2023, 8:21am UTC](https://forum.ansible.com/t/i-want-to-show-the-group-variables-looping-over-all-groups/37633/1 "2023-02-08T08:21:42Z")

</div>

Hello all,  
i use Ansible to document and manage a small network of computers and other devices. So I bulit an inventory with several groups and hosts and defined a bunch of variables on each level to describe what i want to achieve, Each host and each group has for example a variable named `host_comment` or `group_comment` respectively.

This is very useful on the host level, but i want to show the group variables separately in a loop over all groups.

I can show a group variable via `hostvars[host].group_comment`but because every hosts belongs to several groups i cannot control, whicht name is displayed.

This template

```auto

{% for group in groups if group != 'ungrouped' %}
{% for host in groups[group] %}
{% if loop.first %}{% set hvh = hostvars[host] %}
{{ group }} {{ hvh.group_comment }}
{% endif %}
{% endfor %}
{% endfor %}

```

gives

```auto

all Nicht per Ansible erreichbare Maschinen, aber sie sollen durch (Meta-) Variablen doukmentiert werden
lxc_container Alle Hosts in FM 174 (soweit Ansible-fähig)
fm174 Alle Hosts in FM 174 (soweit Ansible-fähig)
hetzner Alle Hosts bei Hetzner (cloud)
controllable Alle Hosts in FM 174 (soweit Ansible-fähig)
virtual_FM174 Virtuelle Ansible Hosts in FM174
virtual_ansible_hosts Nicht per Ansible erreichbare Maschinen, aber sie sollen durch (Meta-) Variablen doukmentiert werden

```

I think it is visible that this result makes no sense.

Can anyone help here ?

Thanks a lot !

Norbert

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 8, 2023, 8:47am UTC](https://forum.ansible.com/t/i-want-to-show-the-group-variables-looping-over-all-groups/37633/2 "2023-02-08T08:47:06Z")

</div>

> Hello all,  
> i use Ansible to document and manage a small network of computers and other devices. So I bulit an inventory with several groups and hosts and defined a bunch of variables on each level to describe what i want to achieve, Each host and each group has for example a variable named `host_comment` or `group_comment` respectively.
> 
> This is very useful on the host level, but i want to show the group variables separately in a loop over all groups.
> 
> I can show a group variable via `hostvars[host].group_comment`but because every hosts belongs to several groups i cannot control, whicht name is displayed.
> 
> This template
> 
> ```auto
> {% forgroupingroupsifgroup!='ungrouped'%}
> {% forhostingroups[group]%}
> {% ifloop.first%}{% sethvh=hostvars[host]%}
> {{ group }}{{ hvh.group_comment }}
> {% endif%}
> {% endfor%}
> {% endfor%}
> 
> ```
> 
> gives
> 
> ```auto
> all Nicht per Ansible erreichbare Maschinen, aber sie sollen durch (Meta-) Variablen doukmentiert werden
> lxc_container Alle Hosts in FM 174 (soweit Ansible-fähig)
> fm174 Alle Hosts in FM 174 (soweit Ansible-fähig)
> hetzner Alle Hosts bei Hetzner (cloud)
> controllable Alle Hosts in FM 174 (soweit Ansible-fähig)
> virtual_FM174 Virtuelle Ansible Hosts in FM174
> virtual_ansible_hosts Nicht per Ansible erreichbare Maschinen, aber sie sollen durch (Meta-) Variablen doukmentiert werden
> 
> ```
> 
> I think it is visible that this result makes no sense.

Hello Norbert,

can you give an example of the desired output?

Regards

```
         Racke

```

---

<div class="post-metadata">

**Author:** ![Norbert\_Klamann](https://avatars.discourse-cdn.com/v4/letter/n/ce73a5/32.png) [@Norbert\_Klamann](https://forum.ansible.com/u/Norbert_Klamann)\
**Post date:** [February 8, 2023, 8:58am UTC](https://forum.ansible.com/t/i-want-to-show-the-group-variables-looping-over-all-groups/37633/3 "2023-02-08T08:58:55Z")

</div>

Yes of course , it should look like this

```auto

all - "alle interessierenden Hosts, auch nicht per ansible kontrollierbare"
controllable - Alle Hosts, die mit ansible kontrolliert werden koennten
fm174 - Alle Hosts in FM 174 (soweit Ansible-fähig)
hetzner - Alle Hosts bei Hetzner (cloud)
virtual_ansible_hosts - Nicht per Ansible erreichbare Maschinen, aber sie sollen durch (Meta-) Variablen doukmentiert werden
virtual_FM174 - Virtuelle Ansible Hosts in FM174

```

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 8, 2023, 9:21am UTC](https://forum.ansible.com/t/i-want-to-show-the-group-variables-looping-over-all-groups/37633/4 "2023-02-08T09:21:04Z")

</div>

> Yes of course , it should look like this
> 
> ```auto
> all - "alle interessierenden Hosts, auch nicht per ansible kontrollierbare"
> controllable - Alle Hosts, die mit ansible kontrolliert werden koennten
> fm174 - Alle Hosts in FM 174 (soweit Ansible-fähig)
> hetzner - Alle Hosts bei Hetzner (cloud)
> virtual_ansible_hosts - Nicht per Ansible erreichbare Maschinen, aber sie sollen durch (Meta-) Variablen doukmentiert werden
> virtual_FM174 - Virtuelle Ansible Hosts in FM174
> 
> ```

Hello Norbert,

take a look at the Jinja sort filter: [https://jinja.palletsprojects.com/en/3.0.x/templates/#jinja-filters.sort](https://jinja.palletsprojects.com/en/3.0.x/templates/#jinja-filters.sort)

Regards

```
     Racke

```

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [February 8, 2023, 5:01pm UTC](https://forum.ansible.com/t/i-want-to-show-the-group-variables-looping-over-all-groups/37633/5 "2023-02-08T17:01:22Z")

</div>

I see what you want, but it isn’t possible. From each host’s perspective, the `group_comment` variable will have the value as set in whichever one of that host’s groups where the group name sorts last, as that’s the last one loaded. So it is consistent, just not a solution to your problem.

You can work around it if you want to go to the trouble. Say you have groups named gr1, gr2, gr3, etc. Within each group, put that group’s comment in a variable called `group_comment_<group_name>`, i.e. `group_comment_gr1`, `group_comment_gr2`, `group_comment_gr3`, etc.

Then do something like the following to create a `group_comments` list for each host.

- name: Create a list of group\_comment\_\* variable names

ansible.builtin.set\_fact:

# Two different ways to do it.

group\_comment\_names\_a: “{{ [‘group\_comment\_’] | product(vars.group\_names) | map(‘join’) | flatten }}”

group\_comment\_names\_b: “{{ query(‘ansible.builtin.varnames’, ‘^group\_comment\_.+’) }}”

- name: Join group\_comment\_\* into a list

ansible.builtin.set\_fact:

group\_comments: |

{% set gclist = %}

{% for gcn in group\_comment\_names\_a %}

{% set \_ = gclist.append(query(‘ansible.builtin.vars’, gcn)) %}

{% endfor %}{{ gclist | flatten }}

I tried lots of different ways to invoke `ansible.builtin.vars` on a list using “normal” jinja pipelines before resorting to the old-school for loop above. If somebody knows how to do it I’d love to see your solution.

Anyway, you end up with each host having a list containing each comment from each of its groups. But I somehow doubt that solves your problem either. It’s still a host-centric view of your group comments.

---

<div class="post-metadata">

**Author:** ![Norbert\_Klamann](https://avatars.discourse-cdn.com/v4/letter/n/ce73a5/32.png) [@Norbert\_Klamann](https://forum.ansible.com/u/Norbert_Klamann)\
**Post date:** [February 13, 2023, 10:13am UTC](https://forum.ansible.com/t/i-want-to-show-the-group-variables-looping-over-all-groups/37633/6 "2023-02-13T10:13:54Z")

</div>

" It’s still a host-centric view of your group comments. "

yes thats the problem if you want to call ut so. It would be nice if groups would be objects in their own right but I presume its a can of worms which nobody wants to open because of the felxibility of the concept

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [February 13, 2023, 3:37pm UTC](https://forum.ansible.com/t/i-want-to-show-the-group-variables-looping-over-all-groups/37633/7 "2023-02-13T15:37:43Z")

</div>

Maybe need to back up a bit and ask what information you hope to get from the display.  
If you were to put together your ideal report by hand, what would it look like, fully articulated?

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [February 15, 2023, 11:19am UTC](https://forum.ansible.com/t/i-want-to-show-the-group-variables-looping-over-all-groups/37633/8 "2023-02-15T11:19:19Z")

</div>

Last week I said, “If somebody knows how to do it I’d love to see your solution.” Well, I found the solution hiding in plain sight in [https://docs.ansible.com/ansible/latest/playbook\_guide/complex\_data\_manipulation.html#id15](https://docs.ansible.com/ansible/latest/playbook_guide/complex_data_manipulation.html#id15) and it boils down to this one-liner:

**q(‘vars’, \*q(‘varnames’, ‘^group\_comment\_’))**

That asterisk isn’t a typo; it’s [Python argument list unpacking](https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists). I had not appreciated how that could be used in Jinja expressions, but there it is.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 21, 2023, 4:27pm UTC](https://forum.ansible.com/t/i-want-to-show-the-group-variables-looping-over-all-groups/37633/9 "2023-02-21T16:27:02Z")

</div>

ansible-inventory has an --export option, this is only thing that  
keeps 'group variables' as such. Ansible internally always flattens  
variables to the host and groups are only a way to label hosts and to  
mass assign variables, they are not a first class entity. So the  
solutions above ONLY work when either variables are only defined in a  
specific group or hosts are not members of more than one group that  
defines the variable.
