Posted by
ads' corner
on
Sunday, 2022-06-12 Posted in [Ansible]
For one of my Ansible Playbooks I need the group name associated with a specific group id. Turns out there is not much documentation online how to do this, and I had to test it out.
To create a simple example (not the one in my Playbook, which is more complex), I start with a getent passwd entry for a user:
1
2
3
4
5
6
7
8
9
10
- name:Get current usercommand:"whoami"register:whoamibecome:no- name:Get passwd entry for login usergetent:database:passwdkey:"{{ whoami.stdout }}"become:no
After running this, the variable $ansible_facts.getent_passwd holds the entry for whatever username is logged in. Since I add become:no, this will use the login username and not root in case this Playbook runs under become: yes.
Using $ansible_facts.getent_passwd[whoami.stdout][2] gives me the gid: 1000. This can be used for another getent call, this time for the group database:
As you might have seen, the $ansible_facts.getent_group variable returns a dict, not a list. If I don’t know the name, how do I access the entry in the dictionary?