group: detect duplicate GIDs when local=yes (#58469)

(cherry picked from commit 4898b0a4a2)
This commit is contained in:
Martin Krizek 2019-07-15 18:52:16 +02:00 committed by Toshio Kuratomi
parent 299fff1d5e
commit 0d86a4dbaa
3 changed files with 33 additions and 0 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- group - properly detect duplicate GIDs when local=yes (https://github.com/ansible/ansible/issues/56481)

View file

@ -120,9 +120,14 @@ class Group(object):
cmd = [self.module.get_bin_path(command_name, True), self.name]
return self.execute_command(cmd)
def _local_check_gid_exists(self):
if self.gid and self.gid in [gr.gr_gid for gr in grp.getgrall()]:
self.module.fail_json(msg="GID '{0}' already exists".format(self.gid))
def group_add(self, **kwargs):
if self.local:
command_name = 'lgroupadd'
self._local_check_gid_exists()
else:
command_name = 'groupadd'
cmd = [self.module.get_bin_path(command_name, True)]
@ -140,6 +145,7 @@ class Group(object):
def group_mod(self, **kwargs):
if self.local:
command_name = 'lgroupmod'
self._local_check_gid_exists()
else:
command_name = 'groupmod'
cmd = [self.module.get_bin_path(command_name, True)]

View file

@ -192,3 +192,28 @@
assert:
that:
- not delete_group_again is changed
# https://github.com/ansible/ansible/issues/56481
- block:
- name: Test duplicate GID with local=yes
group:
name: "{{ item }}"
gid: 1337
local: yes
loop:
- group1_local_test
- group2_local_test
ignore_errors: yes
register: local_duplicate_gid_result
- assert:
that:
- local_duplicate_gid_result['results'][0] is success
- local_duplicate_gid_result['results'][1]['msg'] == "GID '1337' already exists"
always:
- name: Cleanup
group:
name: group1_local_test
state: absent
# only applicable to Linux, limit further to CentOS where 'luseradd' is installed
when: ansible_distribution == 'CentOS'