# json\_query on getent\_group-data: resolving the group-name for a given group-id

**URL:** <https://forum.ansible.com/t/json-query-on-getent-group-data-resolving-the-group-name-for-a-given-group-id/26506>\
**Category:** Ansible Project\
**Created:** [January 17, 2018, 7:11pm UTC](https://forum.ansible.com/t/json-query-on-getent-group-data-resolving-the-group-name-for-a-given-group-id/26506 "2018-01-17T19:11:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henning\_Rohde](https://avatars.discourse-cdn.com/v4/letter/h/3be4f8/32.png) [@Henning\_Rohde](https://forum.ansible.com/u/Henning_Rohde)\
**Post date:** [January 17, 2018, 7:11pm UTC](https://forum.ansible.com/t/json-query-on-getent-group-data-resolving-the-group-name-for-a-given-group-id/26506/1 "2018-01-17T19:11:09Z")

</div>

Hello!

Is there an elegant way to deduce from given gid to group-name in the  
dictionary that's provided by the getent-module of ansible?

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [January 17, 2018, 8:26pm UTC](https://forum.ansible.com/t/json-query-on-getent-group-data-resolving-the-group-name-for-a-given-group-id/26506/2 "2018-01-17T20:26:34Z")

</div>

I’m not a huge fan of json\_query for most cases. I think using the native jinja2 filters are often more readable, although in this case, not 100% more readable.

```auto
getent_group|dictsort|selectattr('1.1', 'equalto', '100')|first|first

```

That turns `getent_group` into a list of tuples, selects where item[1][1] (the ID) is equal to 100, returns the first match, and the first element, which in my case is the groupname of “users”

---

<div class="post-metadata">

**Author:** ![Henning\_Rohde](https://avatars.discourse-cdn.com/v4/letter/h/3be4f8/32.png) [@Henning\_Rohde](https://forum.ansible.com/u/Henning_Rohde)\
**Post date:** [January 18, 2018, 8:15am UTC](https://forum.ansible.com/t/json-query-on-getent-group-data-resolving-the-group-name-for-a-given-group-id/26506/3 "2018-01-18T08:15:56Z")

</div>

Hello Matt,

thank you for having a look into this!

I’d love to use your suggestion - but apparently our setup is too old:

`TASK [debug : debug] *********************************************************** fatal: [localhost]: FAILED! => {"failed": true, "msg": "template error while templating string: no filter named 'selectattr'. String: {{ getent_group | dictsort | selectattr('1.1', 'equalto', group_ID ) | first | first }}"}`

It’s RHEL6:

`$ rpm -qa | grep jinj python-jinja2-26-2.6-3.el6.noarch`

May i ask for another try?

Thanks & best regards  
Henning

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 18, 2018, 2:16pm UTC](https://forum.ansible.com/t/json-query-on-getent-group-data-resolving-the-group-name-for-a-given-group-id/26506/4 "2018-01-18T14:16:47Z")

</div>

One option is to use a loop

- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;msg: '{% for k, v in getent\_group.iteritems() %}{% if v.1 == "1000" %}{{ k }}{% endif %}{% endfor %}'

---

<div class="post-metadata">

**Author:** ![Henning\_Rohde](https://avatars.discourse-cdn.com/v4/letter/h/3be4f8/32.png) [@Henning\_Rohde](https://forum.ansible.com/u/Henning_Rohde)\
**Post date:** [January 19, 2018, 6:59pm UTC](https://forum.ansible.com/t/json-query-on-getent-group-data-resolving-the-group-name-for-a-given-group-id/26506/5 "2018-01-19T18:59:21Z")

</div>

Hello Kai Stian,

thank you to have a look into this!

> - debug:  
> msg: ‘{% for k, v in getent\_group.iteritems() %}{% if v.1 == “1000” %}{{ k }}{% endif %}{% endfor %}’

Whow! This works!

- debug:  
msg: “Primary group: {{ group\_name | default( ‘not found’, true ) }}”  
vars:  
group\_name: \>-  
{% for k, v in getent\_group.iteritems() %}{% if v.1 == group\_id. **str** () %}{{ k }}{% endif %}{% endfor %}

While I had loved to learn some dark sides of json\_query, this solves my current issue - thank you very much, both!

Have a nice weekend  
best regards  
Henning

PS: **str** () may seem redundant, but in one of my testcases, the comparison failed due to group\_id being defined numerically.
