diff --git a/changelogs/fragments/75042-lowercase-dash-n-with-luseradd-on-all-distros.yml b/changelogs/fragments/75042-lowercase-dash-n-with-luseradd-on-all-distros.yml new file mode 100644 index 00000000000..f64b07dcf3e --- /dev/null +++ b/changelogs/fragments/75042-lowercase-dash-n-with-luseradd-on-all-distros.yml @@ -0,0 +1,2 @@ +bugfixes: + - user - fix creating a local user if the user group already exists (https://github.com/ansible/ansible/pull/75042) diff --git a/lib/ansible/modules/user.py b/lib/ansible/modules/user.py index a34bef22fd4..9ad76a90948 100644 --- a/lib/ansible/modules/user.py +++ b/lib/ansible/modules/user.py @@ -693,22 +693,26 @@ class User(object): # exists with the same name as the user to prevent # errors from useradd trying to create a group when # USERGROUPS_ENAB is set in /etc/login.defs. - if os.path.exists('/etc/redhat-release'): - dist = distro.version() - major_release = int(dist.split('.')[0]) - if major_release <= 5 or self.local: - cmd.append('-n') + if self.local: + # luseradd uses -n instead of -N + cmd.append('-n') + else: + if os.path.exists('/etc/redhat-release'): + dist = distro.version() + major_release = int(dist.split('.')[0]) + if major_release <= 5: + cmd.append('-n') + else: + cmd.append('-N') + elif os.path.exists('/etc/SuSE-release'): + # -N did not exist in useradd before SLE 11 and did not + # automatically create a group + dist = distro.version() + major_release = int(dist.split('.')[0]) + if major_release >= 12: + cmd.append('-N') else: cmd.append('-N') - elif os.path.exists('/etc/SuSE-release'): - # -N did not exist in useradd before SLE 11 and did not - # automatically create a group - dist = distro.version() - major_release = int(dist.split('.')[0]) - if major_release >= 12: - cmd.append('-N') - else: - cmd.append('-N') if self.groups is not None and len(self.groups): groups = self.get_groups_set() diff --git a/test/integration/targets/user/tasks/test_local.yml b/test/integration/targets/user/tasks/test_local.yml index 5326073935d..67c24a210cb 100644 --- a/test/integration/targets/user/tasks/test_local.yml +++ b/test/integration/targets/user/tasks/test_local.yml @@ -86,6 +86,7 @@ - testgroup3 - testgroup4 - testgroup5 + - local_ansibulluser tags: - user_test_local_mode @@ -163,6 +164,7 @@ - testgroup3 - testgroup4 - testgroup5 + - local_ansibulluser tags: - user_test_local_mode