Skip to content

Commit 3883a32

Browse files
committed
common: use fallback for group inference on remote systems
When provisioning remote bare metal systems or VMs with different user configurations, getent group <username> may fail if the user's primary group has a different name. This is common in: - Enterprise environments with LDAP/AD integration - Bare metal systems with pre-existing user configurations - Systems like NixOS with different group naming conventions - Cloud/managed hosting with auto-generated group names Add fallback to extract the primary GID from the passwd entry and look up the group by GID, which is more reliable across diverse system configurations. Signed-off-by: Luis Chamberlain <[email protected]>
1 parent 4f11d3f commit 3883a32

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

playbooks/roles/common/tasks/main.yml

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,34 @@
5555
when:
5656
- infer_uid_and_group|bool
5757

58-
- name: Override user and group with inferred settings if feature is enabled
59-
ansible.builtin.set_fact:
58+
- name: Get primary group ID from user entry
59+
set_fact:
60+
user_primary_gid: "{{ getent_running_user.ansible_facts.getent_passwd[target_user][2] }}"
61+
when:
62+
- infer_uid_and_group|bool
63+
- getent_on_group.rc | default(1) != 0
64+
65+
- name: Run getent against the primary group ID
66+
getent:
67+
database: group
68+
key: "{{ user_primary_gid }}"
69+
register: getent_primary_group
70+
when:
71+
- infer_uid_and_group|bool
72+
- getent_on_group.rc | default(1) != 0
73+
74+
- name: Override user and group with inferred settings if feature is enabled (group found by name)
75+
set_fact:
6076
data_user: "{{ target_user }}"
6177
data_group: "{{ ((getent_on_group.values() | first).values() | first).keys() | first }}"
6278
when:
6379
- infer_uid_and_group|bool
80+
- getent_on_group.rc | default(1) == 0
81+
82+
- name: Override user and group with inferred settings if feature is enabled (group found by GID)
83+
set_fact:
84+
data_user: "{{ target_user }}"
85+
data_group: "{{ getent_primary_group.ansible_facts.getent_group.keys() | first }}"
86+
when:
87+
- infer_uid_and_group|bool
88+
- getent_on_group.rc | default(1) != 0

0 commit comments

Comments
 (0)