Update user module to use new shared module code
This commit is contained in:
parent
08ece6c54a
commit
f068bedfa1
1 changed files with 112 additions and 160 deletions
272
library/user
272
library/user
|
@ -17,65 +17,26 @@
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
try:
|
|
||||||
import json
|
|
||||||
except ImportError:
|
|
||||||
import simplejson as json
|
|
||||||
import os
|
import os
|
||||||
import re
|
|
||||||
import pwd
|
import pwd
|
||||||
import grp
|
import grp
|
||||||
import shlex
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
|
||||||
import syslog
|
|
||||||
try:
|
try:
|
||||||
import spwd
|
import spwd
|
||||||
HAVE_SPWD=True
|
HAVE_SPWD=True
|
||||||
except:
|
except:
|
||||||
HAVE_SPWD=False
|
HAVE_SPWD=False
|
||||||
|
|
||||||
USERADD = "/usr/sbin/useradd"
|
def get_bin_path(module, arg):
|
||||||
USERMOD = "/usr/sbin/usermod"
|
if os.path.exists('/usr/sbin/%s' % arg):
|
||||||
USERDEL = "/usr/sbin/userdel"
|
return '/usr/sbin/%s' % arg
|
||||||
|
elif os.path.exists('/sbin/%s' % arg):
|
||||||
def exit_json(rc=0, **kwargs):
|
return '/sbin/%s' % arg
|
||||||
if 'name' in kwargs:
|
|
||||||
add_user_info(kwargs)
|
|
||||||
print json.dumps(kwargs)
|
|
||||||
sys.exit(rc)
|
|
||||||
|
|
||||||
def fail_json(**kwargs):
|
|
||||||
kwargs['failed'] = True
|
|
||||||
exit_json(rc=1, **kwargs)
|
|
||||||
|
|
||||||
def add_user_info(kwargs):
|
|
||||||
name = kwargs['name']
|
|
||||||
if user_exists(name):
|
|
||||||
kwargs['state'] = 'present'
|
|
||||||
info = user_info(name)
|
|
||||||
if info == False:
|
|
||||||
if 'failed' in kwargs:
|
|
||||||
kwargs['notice'] = "failed to look up user name: %s" % name
|
|
||||||
else:
|
|
||||||
kwargs['msg'] = "failed to look up user name: %s" % name
|
|
||||||
kwargs['failed'] = True
|
|
||||||
return kwargs
|
|
||||||
kwargs['uid'] = info[2]
|
|
||||||
kwargs['group'] = info[3]
|
|
||||||
kwargs['comment'] = info[4]
|
|
||||||
kwargs['home'] = info[5]
|
|
||||||
kwargs['shell'] = info[6]
|
|
||||||
kwargs['createhome'] = os.path.exists(info[5])
|
|
||||||
groups = user_group_membership(name)
|
|
||||||
if len(groups) > 0:
|
|
||||||
kwargs['groups'] = groups
|
|
||||||
else:
|
else:
|
||||||
kwargs['state'] = 'absent'
|
module.fail_json(msg="Cannot find %s" % arg)
|
||||||
return kwargs
|
|
||||||
|
|
||||||
def user_del(user, **kwargs):
|
def user_del(module, user, **kwargs):
|
||||||
cmd = [USERDEL]
|
cmd = [get_bin_path(module, 'userdel')]
|
||||||
for key in kwargs:
|
for key in kwargs:
|
||||||
if key == 'force' and kwargs[key] == 'yes':
|
if key == 'force' and kwargs[key] == 'yes':
|
||||||
cmd.append('-f')
|
cmd.append('-f')
|
||||||
|
@ -87,21 +48,21 @@ def user_del(user, **kwargs):
|
||||||
rc = p.returncode
|
rc = p.returncode
|
||||||
return (rc, out, err)
|
return (rc, out, err)
|
||||||
|
|
||||||
def user_add(user, **kwargs):
|
def user_add(module, user, **kwargs):
|
||||||
cmd = [USERADD]
|
cmd = [get_bin_path(module, 'useradd')]
|
||||||
for key in kwargs:
|
for key in kwargs:
|
||||||
if key == 'uid' and kwargs[key] is not None:
|
if key == 'uid' and kwargs[key] is not None:
|
||||||
cmd.append('-u')
|
cmd.append('-u')
|
||||||
cmd.append(kwargs[key])
|
cmd.append(kwargs[key])
|
||||||
elif key == 'group' and kwargs[key] is not None:
|
elif key == 'group' and kwargs[key] is not None:
|
||||||
if not group_exists(kwargs[key]):
|
if not group_exists(kwargs[key]):
|
||||||
fail_json(msg="Group %s does not exist" % (kwargs[key]))
|
module.fail_json(msg="Group %s does not exist" % (kwargs[key]))
|
||||||
cmd.append('-g')
|
cmd.append('-g')
|
||||||
cmd.append(kwargs[key])
|
cmd.append(kwargs[key])
|
||||||
elif key == 'groups' and kwargs[key] is not None:
|
elif key == 'groups' and kwargs[key] is not None:
|
||||||
for g in kwargs[key].split(','):
|
for g in kwargs[key].split(','):
|
||||||
if not group_exists(g):
|
if not group_exists(g):
|
||||||
fail_json(msg="Group %s does not exist" % (g))
|
module.fail_json(msg="Group %s does not exist" % (g))
|
||||||
cmd.append('-G')
|
cmd.append('-G')
|
||||||
cmd.append(kwargs[key])
|
cmd.append(kwargs[key])
|
||||||
elif key == 'comment' and kwargs[key] is not None:
|
elif key == 'comment' and kwargs[key] is not None:
|
||||||
|
@ -134,8 +95,8 @@ def user_add(user, **kwargs):
|
||||||
Without spwd, we would have to resort to reading /etc/shadow
|
Without spwd, we would have to resort to reading /etc/shadow
|
||||||
to get the encrypted string. For now, punt on idempotent password changes.
|
to get the encrypted string. For now, punt on idempotent password changes.
|
||||||
"""
|
"""
|
||||||
def user_mod(user, **kwargs):
|
def user_mod(module, user, **kwargs):
|
||||||
cmd = [USERMOD]
|
cmd = [get_bin_path(module, 'usermod')]
|
||||||
info = user_info(user)
|
info = user_info(user)
|
||||||
for key in kwargs:
|
for key in kwargs:
|
||||||
if key == 'uid':
|
if key == 'uid':
|
||||||
|
@ -144,7 +105,7 @@ def user_mod(user, **kwargs):
|
||||||
cmd.append(kwargs[key])
|
cmd.append(kwargs[key])
|
||||||
elif key == 'group' and kwargs[key] is not None:
|
elif key == 'group' and kwargs[key] is not None:
|
||||||
if not group_exists(kwargs[key]):
|
if not group_exists(kwargs[key]):
|
||||||
fail_json(msg="Group %s does not exist" % (kwargs[key]))
|
module.fail_json(msg="Group %s does not exist" % (kwargs[key]))
|
||||||
ginfo = group_info(group)
|
ginfo = group_info(group)
|
||||||
if info[3] != ginfo[2]:
|
if info[3] != ginfo[2]:
|
||||||
cmd.append('-g')
|
cmd.append('-g')
|
||||||
|
@ -154,7 +115,7 @@ def user_mod(user, **kwargs):
|
||||||
groups = kwargs[key].split(',')
|
groups = kwargs[key].split(',')
|
||||||
for g in groups:
|
for g in groups:
|
||||||
if not group_exists(g):
|
if not group_exists(g):
|
||||||
fail_json(msg="Group %s does not exist" % (g))
|
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
|
groups_need_mod = False
|
||||||
|
|
||||||
|
@ -250,111 +211,102 @@ def user_info(user):
|
||||||
|
|
||||||
# ===========================================
|
# ===========================================
|
||||||
|
|
||||||
if not os.path.exists(USERADD):
|
def main():
|
||||||
if os.path.exists("/sbin/useradd"):
|
module = AnsibleModule(
|
||||||
USERADD = "/sbin/useradd"
|
argument_spec = dict(
|
||||||
|
state=dict(default='present', choices=['present', 'absent']),
|
||||||
|
name=dict(required=True),
|
||||||
|
uid=dict(default=None),
|
||||||
|
group=dict(default=None),
|
||||||
|
groups=dict(default=None),
|
||||||
|
comment=dict(default=None),
|
||||||
|
home=dict(default=None),
|
||||||
|
shell=dict(default=None),
|
||||||
|
password=dict(default=None),
|
||||||
|
# following options are specific to userdel
|
||||||
|
force=dict(default='no', choices=['yes', 'no']),
|
||||||
|
remove=dict(default='no', choices=['yes', 'no']),
|
||||||
|
# following options are specific to useradd
|
||||||
|
createhome=dict(default='yes', choices=['yes', 'no']),
|
||||||
|
system=dict(default='no', choices=['yes', 'no']),
|
||||||
|
# following options are specific to usermod
|
||||||
|
append=dict(default='no', choices=['yes', 'no']),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
state = module.params['state']
|
||||||
|
name = module.params['name']
|
||||||
|
uid = module.params['uid']
|
||||||
|
group = module.params['group']
|
||||||
|
groups = module.params['groups']
|
||||||
|
comment = module.params['comment']
|
||||||
|
home = module.params['home']
|
||||||
|
shell = module.params['shell']
|
||||||
|
password = module.params['password']
|
||||||
|
force = module.params['force']
|
||||||
|
remove = module.params['remove']
|
||||||
|
createhome = module.params['createhome']
|
||||||
|
system = module.params['system']
|
||||||
|
append = module.params['append']
|
||||||
|
|
||||||
|
rc = None
|
||||||
|
out = ''
|
||||||
|
err = ''
|
||||||
|
result = {}
|
||||||
|
result['name'] = name
|
||||||
|
result['state'] = state
|
||||||
|
if state == 'absent':
|
||||||
|
if user_exists(name):
|
||||||
|
(rc, out, err) = user_del(module, name, force=force, remove=remove)
|
||||||
|
if rc != 0:
|
||||||
|
module.fail_json(name=name, msg=err, rc=rc)
|
||||||
|
result['force'] = force
|
||||||
|
result['remove'] = remove
|
||||||
|
elif state == 'present':
|
||||||
|
if not user_exists(name):
|
||||||
|
(rc, out, err) = user_add(module,
|
||||||
|
name, uid=uid, group=group, groups=groups,
|
||||||
|
comment=comment, home=home, shell=shell,
|
||||||
|
password=password, createhome=createhome,
|
||||||
|
system=system)
|
||||||
|
result['system'] = system
|
||||||
|
result['createhome'] = createhome
|
||||||
|
else:
|
||||||
|
(rc, out, err) = user_mod(module,
|
||||||
|
name, uid=uid, group=group, groups=groups,
|
||||||
|
comment=comment, home=home, shell=shell,
|
||||||
|
password=password, append=append)
|
||||||
|
result['append'] = append
|
||||||
|
if rc is not None and rc != 0:
|
||||||
|
module.fail_json(name=name, msg=err, rc=rc)
|
||||||
|
if password is not None:
|
||||||
|
result['password'] = 'NOT_LOGGING_PASSWORD'
|
||||||
|
|
||||||
|
if rc is None:
|
||||||
|
result['changed'] = False
|
||||||
else:
|
else:
|
||||||
fail_json(msg="Cannot find useradd")
|
result['changed'] = True
|
||||||
if not os.path.exists(USERMOD):
|
if out:
|
||||||
if os.path.exists("/sbin/usermod"):
|
result['stdout'] = out
|
||||||
USERMOD = "/sbin/usermod"
|
if err:
|
||||||
else:
|
result['stderr'] = err
|
||||||
fail_json(msg="Cannot find usermod")
|
|
||||||
if not os.path.exists(USERDEL):
|
|
||||||
if os.path.exists("/sbin/userdel"):
|
|
||||||
USERDEL = "/sbin/userdel"
|
|
||||||
else:
|
|
||||||
fail_json(msg="Cannot find userdel")
|
|
||||||
|
|
||||||
argfile = sys.argv[1]
|
|
||||||
args = open(argfile, 'r').read()
|
|
||||||
items = shlex.split(args)
|
|
||||||
syslog.openlog('ansible-%s' % os.path.basename(__file__))
|
|
||||||
log_args = re.sub(r'password=.+ (.*)', r"password=NOT_LOGGING_PASSWORD \1", args)
|
|
||||||
syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % log_args)
|
|
||||||
|
|
||||||
if not len(items):
|
|
||||||
fail_json(msg='the module requires arguments -a')
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
params = {}
|
|
||||||
for x in items:
|
|
||||||
(k, v) = x.split("=")
|
|
||||||
params[k] = v
|
|
||||||
|
|
||||||
state = params.get('state','present')
|
|
||||||
name = params.get('name', None)
|
|
||||||
uid = params.get('uid', None)
|
|
||||||
group = params.get('group', None)
|
|
||||||
groups = params.get('groups', None)
|
|
||||||
comment = params.get('comment', None)
|
|
||||||
home = params.get('home', None)
|
|
||||||
shell = params.get('shell', None)
|
|
||||||
password = params.get('password', None)
|
|
||||||
|
|
||||||
# ===========================================
|
|
||||||
# following options are specific to userdel
|
|
||||||
force = params.get('force', 'no')
|
|
||||||
remove = params.get('remove', 'no')
|
|
||||||
|
|
||||||
# ===========================================
|
|
||||||
# following options are specific to useradd
|
|
||||||
createhome = params.get('createhome', 'yes')
|
|
||||||
system = params.get('system', 'no')
|
|
||||||
|
|
||||||
# ===========================================
|
|
||||||
# following options are specific to usermod
|
|
||||||
append = params.get('append', 'no')
|
|
||||||
|
|
||||||
if state not in [ 'present', 'absent' ]:
|
|
||||||
fail_json(msg='invalid state')
|
|
||||||
if createhome not in [ 'yes', 'no' ]:
|
|
||||||
fail_json(msg='invalid createhome')
|
|
||||||
if system not in ['yes', 'no']:
|
|
||||||
fail_json(msg='invalid system')
|
|
||||||
if append not in [ 'yes', 'no' ]:
|
|
||||||
fail_json(msg='invalid append')
|
|
||||||
if force not in ['yes', 'no']:
|
|
||||||
fail_json(msg="invalid option for force, requires yes or no (defaults to no)")
|
|
||||||
if remove not in ['yes', 'no']:
|
|
||||||
fail_json(msg="invalid option for remove, requires yes or no (defaults to no)")
|
|
||||||
if name is None:
|
|
||||||
fail_json(msg='name is required')
|
|
||||||
|
|
||||||
rc = None
|
|
||||||
out = ''
|
|
||||||
err = ''
|
|
||||||
result = {}
|
|
||||||
result['name'] = name
|
|
||||||
if state == 'absent':
|
|
||||||
if user_exists(name):
|
if user_exists(name):
|
||||||
(rc, out, err) = user_del(name, force=force, remove=remove)
|
info = user_info(name)
|
||||||
if rc != 0:
|
if info == False:
|
||||||
fail_json(name=name, msg=err)
|
result['msg'] = "failed to look up user name: %s" % name
|
||||||
result['force'] = force
|
result['failed'] = True
|
||||||
result['remove'] = remove
|
result['uid'] = info[2]
|
||||||
elif state == 'present':
|
result['group'] = info[3]
|
||||||
if not user_exists(name):
|
result['comment'] = info[4]
|
||||||
(rc, out, err) = user_add(name, uid=uid, group=group, groups=groups,
|
result['home'] = info[5]
|
||||||
comment=comment, home=home, shell=shell,
|
result['shell'] = info[6]
|
||||||
password=password, createhome=createhome,
|
groups = user_group_membership(name)
|
||||||
system=system)
|
result['uid'] = info[2]
|
||||||
else:
|
if len(groups) > 0:
|
||||||
(rc, out, err) = user_mod(name, uid=uid, group=group, groups=groups,
|
result['groups'] = groups
|
||||||
comment=comment, home=home, shell=shell,
|
|
||||||
password=password, append=append)
|
|
||||||
if rc is not None and rc != 0:
|
|
||||||
fail_json(name=name, msg=err)
|
|
||||||
if password is not None:
|
|
||||||
result['password'] = 'NOTLOGGINGPASSWORD'
|
|
||||||
|
|
||||||
if rc is None:
|
module.exit_json(**result)
|
||||||
result['changed'] = False
|
|
||||||
else:
|
# include magic from lib/ansible/module_common.py
|
||||||
result['changed'] = True
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
||||||
if out:
|
main()
|
||||||
result['stdout'] = out
|
|
||||||
if err:
|
|
||||||
result['stderr'] = err
|
|
||||||
exit_json(**result)
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
Loading…
Reference in a new issue