Merge pull request #100 from sfromm/group-module
Remove support for membership from group module
This commit is contained in:
commit
13df76d3d5
2 changed files with 4 additions and 58 deletions
|
@ -10,11 +10,8 @@
|
|||
- name: create a group
|
||||
action: group name=tset
|
||||
|
||||
- name: add a member to tset
|
||||
action: group name=tset member=nobody memberstate=present
|
||||
# You can only modify the group's gid
|
||||
- action: group name=tset gid=7777
|
||||
|
||||
- name: remove member
|
||||
action: group name=tset member=nobody memberstate=absent
|
||||
|
||||
- name: remove group
|
||||
action: group name=tset state=absent
|
||||
# And finally remove the group
|
||||
- action: group name=tset state=absent
|
||||
|
|
|
@ -30,7 +30,6 @@ import sys
|
|||
GROUPADD = "/usr/sbin/groupadd"
|
||||
GROUPDEL = "/usr/sbin/groupdel"
|
||||
GROUPMOD = "/usr/sbin/groupmod"
|
||||
GPASSWD = "/usr/bin/gpasswd"
|
||||
|
||||
def debug(msg):
|
||||
# ansible ignores stderr, so it's safe to use for debug
|
||||
|
@ -54,7 +53,6 @@ def add_group_info(kwargs):
|
|||
kwargs['state'] = 'present'
|
||||
info = group_info(name)
|
||||
kwargs['gid'] = info[2]
|
||||
kwargs['members'] = info[3]
|
||||
else:
|
||||
kwargs['state'] = 'absent'
|
||||
return kwargs
|
||||
|
@ -100,33 +98,6 @@ def group_mod(group, **kwargs):
|
|||
else:
|
||||
return False
|
||||
|
||||
def group_has_member(group, member):
|
||||
if not group_exists(group):
|
||||
return False
|
||||
info = group_info(group)
|
||||
if member in info[3]:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def group_add_member(group, member):
|
||||
cmd = [GPASSWD, '-a', member, group]
|
||||
debug("Arguments to gpasswd: %s" % (" ".join(cmd)))
|
||||
rc = subprocess.call(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
if rc == 0:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def group_del_member(group, member):
|
||||
cmd = [GPASSWD, '-d', member, group]
|
||||
debug("Arguments to gpasswd: %s" % (" ".join(cmd)))
|
||||
rc = subprocess.call(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
if rc == 0:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def group_exists(group):
|
||||
try:
|
||||
if grp.getgrnam(group):
|
||||
|
@ -160,11 +131,6 @@ if not os.path.exists(GROUPMOD):
|
|||
GROUPDEL = "/sbin/groupmod"
|
||||
else:
|
||||
fail_json(msg="Cannot find groupmod")
|
||||
if not os.path.exists(GPASSWD):
|
||||
if os.path.exists("/bin/gpasswd"):
|
||||
GROUPDEL = "/bin/gpasswd"
|
||||
else:
|
||||
fail_json(msg="Cannot find gpasswd")
|
||||
|
||||
if len(sys.argv) == 2 and os.path.exists(sys.argv[1]):
|
||||
argfile = sys.argv[1]
|
||||
|
@ -186,15 +152,8 @@ state = params.get('state','present')
|
|||
name = params.get('name', None)
|
||||
gid = params.get('gid', None)
|
||||
|
||||
# ===========================================
|
||||
# the following controls group membership
|
||||
member = params.get('member', None)
|
||||
memberstate = params.get('memberstate', 'present')
|
||||
|
||||
if state not in [ 'present', 'absent' ]:
|
||||
fail_json(msg='invalid state')
|
||||
if memberstate not in [ 'present', 'absent' ]:
|
||||
fail_json(msg='invalid memberstate')
|
||||
if name is None:
|
||||
fail_json(msg='name is required')
|
||||
|
||||
|
@ -210,16 +169,6 @@ elif state == 'present':
|
|||
else:
|
||||
changed = group_mod(name, gid=gid)
|
||||
|
||||
if member is not None:
|
||||
if memberstate == 'present':
|
||||
if not group_has_member(name, member):
|
||||
changed = group_add_member(name, member)
|
||||
elif memberstate == 'absent':
|
||||
if group_has_member(name, member):
|
||||
changed = group_del_member(name, member)
|
||||
else:
|
||||
fail_json(name=name, msg='Unexpected position reached')
|
||||
|
||||
exit_json(name=name, changed=changed)
|
||||
|
||||
fail_json(name=name, msg='Unexpected position reached')
|
||||
|
|
Loading…
Reference in a new issue