# Get user or group name from GID

**URL:** <https://forum.ansible.com/t/get-user-or-group-name-from-gid/5938>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [May 22, 2024, 4:48pm UTC](https://forum.ansible.com/t/get-user-or-group-name-from-gid/5938 "2024-05-22T16:48:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheSpy0](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/thespy0/32/496_2.png) [@TheSpy0](https://forum.ansible.com/u/TheSpy0)\
**Post date:** [May 22, 2024, 4:48pm UTC](https://forum.ansible.com/t/get-user-or-group-name-from-gid/5938/1 "2024-05-22T16:48:42Z")

</div>

I’m doing some user management tasks and use the `ansible.builtin.getent` module to gather information about the local groups on the systems. The module output is (if I’m not mistaken) a dictionary where they group name is the key and a list of group attributes is the value. I’d like to be able to get the group name by using the GID to “search” for it. However, this isn’t straightforward, since you need to know the key name to be able to access the value. Below is a snippet of the module output. I know that `[*]` can be used to access each item in a list. Is there something similar for dictionaries? I tried using the `map('extract')` [filter](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_filters.html#selecting-values-from-arrays-or-hashtables), but wasn’t figure out how it works well enough. Maybe a different filter or module would be better. Open to suggestions.

```json
ok: [localhost] => {
    "msg": {
        "_ssh": [
            "x",
            "114",
            ""
        ],
        "adm": [
            "x",
            "4",
            "syslog,user-1"
        ],
        "admin": [
            "x",
            "115",
            ""
        ],
        "audio": [
            "x",
            "29",
            "user-1
       ]

```

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [May 22, 2024, 5:53pm UTC](https://forum.ansible.com/t/get-user-or-group-name-from-gid/5938/2 "2024-05-22T17:53:17Z")

</div>

If you could post an example of the format of the results you want I’m sure someone will be able to suggest a method to achieve it.

Perhaps the results you can get from [slurping](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/slurp_module.html) the `/etc/group` files and parsing it with the [jc group filter](https://kellyjonbrazil.github.io/jc/docs/parsers/group) via the [jc filter](https://docs.ansible.com/ansible/latest/collections/community/general/jc_filter.html) might be more like what you want?

```yaml
- name: Slurp /etc/group
  ansible.builtin.slurp:
    src: /etc/group
  register: etc_group_b64encoded

- name: Set fact for groups
  ansible.builtin.set_fact:
    groups: "{{ etc_group_b64encoded['content'] | ansible.builtin.b64decode | community.general.jc('group') }}"

```

Note that you would need [jc](https://github.com/kellyjonbrazil/jc) installed on the Ansible controller to do this.

---

<div class="post-metadata">

**Author:** ![TheSpy0](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/thespy0/32/496_2.png) [@TheSpy0](https://forum.ansible.com/u/TheSpy0)\
**Post date:** [May 22, 2024, 10:59pm UTC](https://forum.ansible.com/t/get-user-or-group-name-from-gid/5938/3 "2024-05-22T22:59:38Z")

</div>

> [@chris](#):
>
> `community.general.jc('group')`

Thanks, @chris. I hadn’t thought of using the slurp module. The jc filter does provide the data in a more straightforward structure (at least in my head). I’ll have to see if we can use it in our environment.

Another (more general) way to ask my question might be; Is there a way to access data inside a dictionary if the name of the dictionary key is unknown or there are multiple keys to look at?

---

<div class="post-metadata">

**Author:** ![markuman](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@markuman](https://forum.ansible.com/u/markuman)\
**Post date:** [May 23, 2024, 8:43am UTC](https://forum.ansible.com/t/get-user-or-group-name-from-gid/5938/4 "2024-05-23T08:43:36Z")

</div>

Yes, you can do it with native ansible filters.

Say you’ve one to get the user of ID 140

```auto
grep usb /etc/passwd
usbmux:x:140:140:usbmux user:/:/usr/bin/nologin

```

You can do something like that

```yml
---
- hosts: localhost
  tasks:
    - name: getent
      ansible.builtin.getent:
        database: passwd
      register: out
    - debug:
        var: out.ansible_facts.getent_passwd | dict2items | selectattr('value', 'search', '140') | map(attribute='key')

```

It will result in

```auto
TASK [debug] ***********************************************************************************************************
ok: [localhost] => {
    "out.ansible_facts.getent_passwd | dict2items | selectattr('value', 'search', '140') | map(attribute='key')": [
        "usbmux"
    ]
}

```

---

<div class="post-metadata">

**Author:** ![TheSpy0](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/thespy0/32/496_2.png) [@TheSpy0](https://forum.ansible.com/u/TheSpy0)\
**Post date:** [May 23, 2024, 3:13pm UTC](https://forum.ansible.com/t/get-user-or-group-name-from-gid/5938/5 "2024-05-23T15:13:17Z")

</div>

Excellent! This is what I was looking for. Thank you.

Do you have some good sources for working with `map` and `selectattr` that isn’t the Jinja documentation? I see these two used often in the Ansible documentation, but haven’t come across a list of available options, or really how to work with them outside of the examples.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [May 23, 2024, 10:07pm UTC](https://forum.ansible.com/t/get-user-or-group-name-from-gid/5938/6 "2024-05-23T22:07:43Z")

</div>

map/select/reject are part of Jinja2 itself, not Ansible additions, we only document the latter.

[Template Designer Documentation — Jinja Documentation (2.10.x)](https://jinja.palletsprojects.com/en/2.10.x/templates/#builtin-filters)

---

<div class="post-metadata">

**Author:** ![TheSpy0](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/thespy0/32/496_2.png) [@TheSpy0](https://forum.ansible.com/u/TheSpy0)\
**Post date:** [May 23, 2024, 10:52pm UTC](https://forum.ansible.com/t/get-user-or-group-name-from-gid/5938/7 "2024-05-23T22:52:45Z")

</div>

I was asking if there was some documentation, guides, etc. that were outside the Jinja documentation, but not Ansible documentation. I understand that the these are not included in the Ansible documentation since they’re built into Jinja and are documented there. However, I have a difficult time wrapping my head around the Jinja documentation.

---

<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:** [June 22, 2024, 10:53pm UTC](https://forum.ansible.com/t/get-user-or-group-name-from-gid/5938/8 "2024-06-22T22:53:08Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
