Handle 'groups=' (empty string) in user module (Linux)

Makes it possible to remove a user from all but the primary group.

Setting 'groups=' is the equivalent to passing '-G ""' to
useradd/usermod, which is interpreted as "no group" on Linux.
reviewable/pr18780/r1
Stanis Trendelenburg 12 years ago
parent 6c8b2c4b70
commit e225a90abc

13
user

@ -48,7 +48,9 @@ options:
groups: groups:
required: false required: false
description: description:
- Puts the user in this comma-delimited list of groups. - Puts the user in this comma-delimited list of groups. When set to
the empty string ('groups='), the user is removed from all groups
except the primary group.
append: append:
required: false required: false
description: description:
@ -249,6 +251,7 @@ class User(object):
cmd.append(self.group) cmd.append(self.group)
if self.groups is not None: if self.groups is not None:
if self.groups != '':
for g in self.groups.split(','): for g in self.groups.split(','):
if not self.group_exists(g): if not self.group_exists(g):
self.module.fail_json(msg="Group %s does not exist" % (g)) self.module.fail_json(msg="Group %s does not exist" % (g))
@ -301,13 +304,19 @@ class User(object):
if self.groups is not None: if self.groups is not None:
current_groups = self.user_group_membership() current_groups = self.user_group_membership()
groups_need_mod = False
groups = []
if self.groups == '':
if current_groups and not self.append:
groups_need_mod = True
else:
groups = self.groups.split(',') groups = self.groups.split(',')
for g in groups: for g in groups:
if not self.group_exists(g): if not self.group_exists(g):
self.module.fail_json(msg="Group %s does not exist" % (g)) self.module.fail_json(msg="Group %s does not exist" % (g))
group_diff = set(sorted(current_groups)).symmetric_difference(set(sorted(groups))) group_diff = set(sorted(current_groups)).symmetric_difference(set(sorted(groups)))
groups_need_mod = False
if group_diff: if group_diff:
if self.append: if self.append:

Loading…
Cancel
Save