mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Merge pull request #1635 from romeotheriault/classify-group-module
convert group module to be platform sub-classable
This commit is contained in:
commit
e13d67bbf1
1 changed files with 100 additions and 65 deletions
113
library/group
113
library/group
|
@ -55,31 +55,65 @@ examples:
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import grp
|
import grp
|
||||||
|
import syslog
|
||||||
|
import platform
|
||||||
|
|
||||||
|
class Group(object):
|
||||||
|
"""
|
||||||
|
This is a generic Group manipulation class that is subclassed
|
||||||
|
based on platform.
|
||||||
|
|
||||||
|
A subclass may wish to override the following action methods:-
|
||||||
|
- group_del()
|
||||||
|
- group_add()
|
||||||
|
- group_mod()
|
||||||
|
|
||||||
|
All subclasses MUST define platform and distribution (which may be None).
|
||||||
|
"""
|
||||||
|
|
||||||
|
platform = 'Generic'
|
||||||
|
distribution = None
|
||||||
|
GROUPFILE = '/etc/group'
|
||||||
|
|
||||||
|
def __new__(cls, *args, **kwargs):
|
||||||
|
return load_platform_subclass(Group, args, kwargs)
|
||||||
|
|
||||||
|
def __init__(self, module):
|
||||||
|
self.module = module
|
||||||
|
self.state = module.params['state']
|
||||||
|
self.name = module.params['name']
|
||||||
|
self.gid = module.params['gid']
|
||||||
|
self.system = module.params['system']
|
||||||
|
self.syslogging = False
|
||||||
|
|
||||||
|
def execute_command(self, cmd):
|
||||||
|
if self.syslogging:
|
||||||
|
syslog.openlog('ansible-%s' % os.path.basename(__file__))
|
||||||
|
syslog.syslog(syslog.LOG_NOTICE, 'Command %s' % '|'.join(cmd))
|
||||||
|
|
||||||
def group_del(module, group):
|
|
||||||
cmd = [module.get_bin_path('groupdel', True), group]
|
|
||||||
p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
(out, err) = p.communicate()
|
(out, err) = p.communicate()
|
||||||
rc = p.returncode
|
rc = p.returncode
|
||||||
return (rc, out, err)
|
return (rc, out, err)
|
||||||
|
|
||||||
def group_add(module, group, **kwargs):
|
def group_del(self):
|
||||||
cmd = [module.get_bin_path('groupadd', True)]
|
cmd = [self.module.get_bin_path('groupdel', True), self.name]
|
||||||
|
return self.execute_command(cmd)
|
||||||
|
|
||||||
|
def group_add(self, **kwargs):
|
||||||
|
cmd = [self.module.get_bin_path('groupadd', True)]
|
||||||
for key in kwargs:
|
for key in kwargs:
|
||||||
if key == 'gid' and kwargs[key] is not None:
|
if key == 'gid' and kwargs[key] is not None:
|
||||||
cmd.append('-g')
|
cmd.append('-g')
|
||||||
cmd.append(kwargs[key])
|
cmd.append(kwargs[key])
|
||||||
elif key == 'system' and kwargs[key] == 'yes':
|
elif key == 'system' and kwargs[key] == 'yes':
|
||||||
cmd.append('-r')
|
cmd.append('-r')
|
||||||
cmd.append(group)
|
cmd.append(self.name)
|
||||||
p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
return self.execute_command(cmd)
|
||||||
(out, err) = p.communicate()
|
|
||||||
rc = p.returncode
|
|
||||||
return (rc, out, err)
|
|
||||||
|
|
||||||
def group_mod(module, group, **kwargs):
|
def group_mod(self, **kwargs):
|
||||||
cmd = [module.get_bin_path('groupmod', True)]
|
cmd = [self.module.get_bin_path('groupmod', True)]
|
||||||
info = group_info(group)
|
info = self.group_info()
|
||||||
for key in kwargs:
|
for key in kwargs:
|
||||||
if key == 'gid':
|
if key == 'gid':
|
||||||
if kwargs[key] is not None and info[2] != int(kwargs[key]):
|
if kwargs[key] is not None and info[2] != int(kwargs[key]):
|
||||||
|
@ -87,24 +121,21 @@ def group_mod(module, group, **kwargs):
|
||||||
cmd.append(kwargs[key])
|
cmd.append(kwargs[key])
|
||||||
if len(cmd) == 1:
|
if len(cmd) == 1:
|
||||||
return (None, '', '')
|
return (None, '', '')
|
||||||
cmd.append(group)
|
cmd.append(self.name)
|
||||||
p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
return self.execute_command(cmd)
|
||||||
(out, err) = p.communicate()
|
|
||||||
rc = p.returncode
|
|
||||||
return (rc, out, err)
|
|
||||||
|
|
||||||
def group_exists(group):
|
def group_exists(self):
|
||||||
try:
|
try:
|
||||||
if grp.getgrnam(group):
|
if grp.getgrnam(self.name):
|
||||||
return True
|
return True
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def group_info(group):
|
def group_info(self):
|
||||||
if not group_exists(group):
|
if not self.group_exists():
|
||||||
return False
|
return False
|
||||||
try:
|
try:
|
||||||
info = list(grp.getgrnam(group))
|
info = list(grp.getgrnam(self.name))
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return False
|
return False
|
||||||
return info
|
return info
|
||||||
|
@ -121,29 +152,33 @@ def main():
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
state = module.params['state']
|
group = Group(module)
|
||||||
name = module.params['name']
|
|
||||||
gid = module.params['gid']
|
if group.syslogging:
|
||||||
system = module.params['system']
|
syslog.openlog('ansible-%s' % os.path.basename(__file__))
|
||||||
|
syslog.syslog(syslog.LOG_NOTICE, 'Group instantiated - platform %s' % group.platform)
|
||||||
|
if user.distribution:
|
||||||
|
syslog.syslog(syslog.LOG_NOTICE, 'Group instantiated - distribution %s' % group.distribution)
|
||||||
|
|
||||||
rc = None
|
rc = None
|
||||||
out = ''
|
out = ''
|
||||||
err = ''
|
err = ''
|
||||||
result = {}
|
result = {}
|
||||||
result['name'] = name
|
result['name'] = group.name
|
||||||
result['state'] = state
|
result['state'] = group.state
|
||||||
if state == 'absent':
|
if group.state == 'absent':
|
||||||
if group_exists(name):
|
if group.group_exists():
|
||||||
(rc, out, err) = group_del(module, name)
|
(rc, out, err) = group.group_del()
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
module.fail_json(name=name, msg=err)
|
module.fail_json(name=name, msg=err)
|
||||||
elif state == 'present':
|
elif group.state == 'present':
|
||||||
if not group_exists(name):
|
if not group.group_exists():
|
||||||
(rc, out, err) = group_add(module, name, gid=gid, system=system)
|
(rc, out, err) = group.group_add(gid=group.gid, system=group.system)
|
||||||
else:
|
else:
|
||||||
(rc, out, err) = group_mod(module, name, gid=gid)
|
(rc, out, err) = group.group_mod(gid=group.gid)
|
||||||
|
|
||||||
if rc is not None and rc != 0:
|
if rc is not None and rc != 0:
|
||||||
module.fail_json(name=name, msg=err)
|
module.fail_json(name=group.name, msg=err)
|
||||||
|
|
||||||
if rc is None:
|
if rc is None:
|
||||||
result['changed'] = False
|
result['changed'] = False
|
||||||
|
@ -154,9 +189,9 @@ def main():
|
||||||
if err:
|
if err:
|
||||||
result['stderr'] = err
|
result['stderr'] = err
|
||||||
|
|
||||||
if group_exists(name):
|
if group.group_exists():
|
||||||
info = group_info(name)
|
info = group.group_info()
|
||||||
result['system'] = system
|
result['system'] = group.system
|
||||||
result['gid'] = info[2]
|
result['gid'] = info[2]
|
||||||
|
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
Loading…
Reference in a new issue