Merge branch 'devel' of git://github.com/trbs/ansible into groups

reviewable/pr18780/r1
Michael DeHaan 12 years ago
commit dc1242d7eb

@ -229,7 +229,7 @@ class User(object):
# select whether we dump additional debug info through syslog # select whether we dump additional debug info through syslog
self.syslogging = False self.syslogging = False
def execute_command(self,cmd): def execute_command(self, cmd):
if self.syslogging: if self.syslogging:
syslog.openlog('ansible-%s' % os.path.basename(__file__)) syslog.openlog('ansible-%s' % os.path.basename(__file__))
syslog.syslog(syslog.LOG_NOTICE, 'Command %s' % '|'.join(cmd)) syslog.syslog(syslog.LOG_NOTICE, 'Command %s' % '|'.join(cmd))
@ -263,12 +263,9 @@ 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 != '': groups = self.get_groups_set()
for g in self.groups.split(','):
if not self.group_exists(g):
self.module.fail_json(msg="Group %s does not exist" % (g))
cmd.append('-G') cmd.append('-G')
cmd.append(self.groups) cmd.append(','.join(groups))
if self.comment is not None: if self.comment is not None:
cmd.append('-c') cmd.append('-c')
@ -326,12 +323,8 @@ class User(object):
if current_groups and not self.append: if current_groups and not self.append:
groups_need_mod = True groups_need_mod = True
else: else:
groups = self.groups.split(',') groups = self.get_groups_set()
for g in groups: group_diff = set(current_groups).symmetric_difference(groups)
if not self.group_exists(g):
self.module.fail_json(msg="Group %s does not exist" % (g))
group_diff = set(sorted(current_groups)).symmetric_difference(set(sorted(groups)))
if group_diff: if group_diff:
if self.append: if self.append:
@ -370,6 +363,7 @@ class User(object):
return (0, '', '') return (0, '', '')
cmd.append(self.name) cmd.append(self.name)
open("/tmp/xxx.log", "a").write(str(cmd)+"\n")
return self.execute_command(cmd) return self.execute_command(cmd)
def group_exists(self,group): def group_exists(self,group):
@ -391,11 +385,23 @@ class User(object):
else: else:
return list(grp.getgrnam(group)) return list(grp.getgrnam(group))
def get_groups_set(self):
if self.groups is None:
return None
info = self.user_info()
groups = set(self.groups.split(','))
for g in set(groups):
if not self.group_exists(g):
self.module.fail_json(msg="Group %s does not exist" % (g))
if info and self.group_info(g)[2] == info[3]:
groups.remove(g)
return groups
def user_group_membership(self): def user_group_membership(self):
groups = [] groups = []
info = self.get_pwd_info() info = self.get_pwd_info()
for group in grp.getgrall(): for group in grp.getgrall():
if self.name in group.gr_mem and info[3] == group.gr_gid: if self.name in group.gr_mem and not info[3] == group.gr_gid:
groups.append(group[0]) groups.append(group[0])
return groups return groups
@ -574,11 +580,9 @@ class FreeBsdUser(User):
cmd.append(self.group) cmd.append(self.group)
if self.groups is not None: if self.groups is not None:
for g in self.groups.split(','): groups = self.get_groups_set()
if not self.group_exists(g):
self.module.fail_json(msg="Group %s does not exist" % (g))
cmd.append('-G') cmd.append('-G')
cmd.append(self.groups) cmd.append(','.join(groups))
if self.createhome: if self.createhome:
cmd.append('-m') cmd.append('-m')
@ -644,12 +648,9 @@ class FreeBsdUser(User):
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 = self.groups.split(',') groups = self.get_groups_set()
for g in groups:
if not self.group_exists(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(current_groups).symmetric_difference(groups)
groups_need_mod = False groups_need_mod = False
if group_diff: if group_diff:
@ -732,11 +733,9 @@ class SunOS(User):
cmd.append(self.group) cmd.append(self.group)
if self.groups is not None: if self.groups is not None:
for g in self.groups.split(','): groups = self.get_groups_set()
if not self.group_exists(g):
self.module.fail_json(msg="Group %s does not exist" % (g))
cmd.append('-G') cmd.append('-G')
cmd.append(self.groups) cmd.append(','.join(groups))
if self.comment is not None: if self.comment is not None:
cmd.append('-c') cmd.append('-c')
@ -799,12 +798,8 @@ class SunOS(User):
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 = self.groups.split(',') groups = self.get_groups_set()
for g in groups: group_diff = set(current_groups).symmetric_difference(groups)
if not self.group_exists(g):
self.module.fail_json(msg="Group %s does not exist" % (g))
group_diff = set(sorted(current_groups)).symmetric_difference(set(sorted(groups)))
groups_need_mod = False groups_need_mod = False
if group_diff: if group_diff:
@ -901,11 +896,9 @@ class AIX(User):
cmd.append(self.group) cmd.append(self.group)
if self.groups is not None: if self.groups is not None:
for g in self.groups.split(','): groups = self.get_groups_set()
if not self.group_exists(g):
self.module.fail_json(msg="Group %s does not exist" % (g))
cmd.append('-G') cmd.append('-G')
cmd.append(self.groups) cmd.append(','.join(groups))
if self.comment is not None: if self.comment is not None:
cmd.append('-c') cmd.append('-c')
@ -954,12 +947,8 @@ class AIX(User):
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 = self.groups.split(',') groups = self.get_groups_set()
for g in groups: group_diff = set(current_groups).symmetric_difference(groups)
if not self.group_exists(g):
self.module.fail_json(msg="Group %s does not exist" % (g))
group_diff = set(sorted(current_groups)).symmetric_difference(set(sorted(groups)))
groups_need_mod = False groups_need_mod = False
if group_diff: if group_diff:
@ -1113,7 +1102,6 @@ def main():
result['comment'] = info[4] result['comment'] = info[4]
result['home'] = info[5] result['home'] = info[5]
result['shell'] = info[6] result['shell'] = info[6]
groups = user.user_group_membership()
result['uid'] = info[2] result['uid'] = info[2]
if user.groups is not None: if user.groups is not None:
result['groups'] = user.groups result['groups'] = user.groups

Loading…
Cancel
Save