1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Merge pull request #100 from sfromm/group-module

Remove support for membership from group module
This commit is contained in:
Michael DeHaan 2012-03-28 16:24:15 -07:00
commit 13df76d3d5
2 changed files with 4 additions and 58 deletions

View file

@ -10,11 +10,8 @@
- name: create a group - name: create a group
action: group name=tset action: group name=tset
- name: add a member to tset # You can only modify the group's gid
action: group name=tset member=nobody memberstate=present - action: group name=tset gid=7777
- name: remove member # And finally remove the group
action: group name=tset member=nobody memberstate=absent - action: group name=tset state=absent
- name: remove group
action: group name=tset state=absent

View file

@ -30,7 +30,6 @@ import sys
GROUPADD = "/usr/sbin/groupadd" GROUPADD = "/usr/sbin/groupadd"
GROUPDEL = "/usr/sbin/groupdel" GROUPDEL = "/usr/sbin/groupdel"
GROUPMOD = "/usr/sbin/groupmod" GROUPMOD = "/usr/sbin/groupmod"
GPASSWD = "/usr/bin/gpasswd"
def debug(msg): def debug(msg):
# ansible ignores stderr, so it's safe to use for debug # ansible ignores stderr, so it's safe to use for debug
@ -54,7 +53,6 @@ def add_group_info(kwargs):
kwargs['state'] = 'present' kwargs['state'] = 'present'
info = group_info(name) info = group_info(name)
kwargs['gid'] = info[2] kwargs['gid'] = info[2]
kwargs['members'] = info[3]
else: else:
kwargs['state'] = 'absent' kwargs['state'] = 'absent'
return kwargs return kwargs
@ -100,33 +98,6 @@ def group_mod(group, **kwargs):
else: else:
return False 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): def group_exists(group):
try: try:
if grp.getgrnam(group): if grp.getgrnam(group):
@ -160,11 +131,6 @@ if not os.path.exists(GROUPMOD):
GROUPDEL = "/sbin/groupmod" GROUPDEL = "/sbin/groupmod"
else: else:
fail_json(msg="Cannot find groupmod") 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]): if len(sys.argv) == 2 and os.path.exists(sys.argv[1]):
argfile = sys.argv[1] argfile = sys.argv[1]
@ -186,15 +152,8 @@ state = params.get('state','present')
name = params.get('name', None) name = params.get('name', None)
gid = params.get('gid', 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' ]: if state not in [ 'present', 'absent' ]:
fail_json(msg='invalid state') fail_json(msg='invalid state')
if memberstate not in [ 'present', 'absent' ]:
fail_json(msg='invalid memberstate')
if name is None: if name is None:
fail_json(msg='name is required') fail_json(msg='name is required')
@ -210,16 +169,6 @@ elif state == 'present':
else: else:
changed = group_mod(name, gid=gid) 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) exit_json(name=name, changed=changed)
fail_json(name=name, msg='Unexpected position reached') fail_json(name=name, msg='Unexpected position reached')