Get user or group name from GID

Yes, you can do it with native ansible filters.

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

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

You can do something like that

---
- 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

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

2 Likes