diff --git a/changelogs/fragments/61965-user-module-fails-to-change-primary-group.yml b/changelogs/fragments/61965-user-module-fails-to-change-primary-group.yml new file mode 100644 index 00000000000..8379f1eedc7 --- /dev/null +++ b/changelogs/fragments/61965-user-module-fails-to-change-primary-group.yml @@ -0,0 +1,2 @@ +bugfixes: +- prevent lusermod from using group name instead of group id (https://github.com/ansible/ansible/pull/77914) diff --git a/lib/ansible/modules/user.py b/lib/ansible/modules/user.py index b247ba3682e..5729de3880a 100644 --- a/lib/ansible/modules/user.py +++ b/lib/ansible/modules/user.py @@ -821,7 +821,7 @@ class User(object): ginfo = self.group_info(self.group) if info[3] != ginfo[2]: cmd.append('-g') - cmd.append(self.group) + cmd.append(ginfo[2]) if self.groups is not None: # get a list of all groups for the user, including the primary diff --git a/test/integration/targets/user/tasks/test_local.yml b/test/integration/targets/user/tasks/test_local.yml index 16c79c57812..5326073935d 100644 --- a/test/integration/targets/user/tasks/test_local.yml +++ b/test/integration/targets/user/tasks/test_local.yml @@ -85,6 +85,7 @@ - testgroup2 - testgroup3 - testgroup4 + - testgroup5 tags: - user_test_local_mode @@ -121,6 +122,28 @@ tags: - user_test_local_mode +- name: Assign named group for local_ansibulluser + user: + name: local_ansibulluser + state: present + local: yes + group: testgroup5 + register: local_user_test_6 + tags: + - user_test_local_mode + +# If we don't re-assign, then "Set user expiration" will +# fail. +- name: Re-assign named group for local_ansibulluser + user: + name: local_ansibulluser + state: present + local: yes + group: local_ansibulluser + ignore_errors: yes + tags: + - user_test_local_mode + - name: Remove local_ansibulluser again user: name: local_ansibulluser @@ -139,6 +162,7 @@ - testgroup2 - testgroup3 - testgroup4 + - testgroup5 tags: - user_test_local_mode @@ -149,6 +173,7 @@ - local_user_test_2 is not changed - local_user_test_3 is changed - local_user_test_4 is changed + - local_user_test_6 is changed - local_user_test_remove_1 is changed - local_user_test_remove_2 is not changed tags: