mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Fixes #4350 Create homedirectory if create=yes and directory is missing
This commit is contained in:
parent
8a3f222d41
commit
23cbdba65e
1 changed files with 35 additions and 1 deletions
|
@ -92,7 +92,8 @@ options:
|
||||||
choices: [ "yes", "no" ]
|
choices: [ "yes", "no" ]
|
||||||
description:
|
description:
|
||||||
- Unless set to C(no), a home directory will be made for the user
|
- Unless set to C(no), a home directory will be made for the user
|
||||||
when the account is created.
|
when the account is created or if the home directory does not
|
||||||
|
exist.
|
||||||
system:
|
system:
|
||||||
required: false
|
required: false
|
||||||
default: "no"
|
default: "no"
|
||||||
|
@ -563,6 +564,32 @@ class User(object):
|
||||||
# by default we use the modify_user_usermod method
|
# by default we use the modify_user_usermod method
|
||||||
return self.modify_user_usermod()
|
return self.modify_user_usermod()
|
||||||
|
|
||||||
|
def create_homedir(self, path):
|
||||||
|
if not os.path.exists(path):
|
||||||
|
# use /etc/skel if possible
|
||||||
|
if os.path.exists('/etc/skel'):
|
||||||
|
try:
|
||||||
|
shutil.copytree('/etc/skel', path, symlinks=True)
|
||||||
|
except OSError, e:
|
||||||
|
self.module.exit_json(failed=True, msg="%s" % e)
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
os.makedirs(path)
|
||||||
|
except OSError, e:
|
||||||
|
self.module.exit_json(failed=True, msg="%s" % e)
|
||||||
|
|
||||||
|
def chown_homedir(self, uid, gid, path):
|
||||||
|
try:
|
||||||
|
os.chown(path, uid, gid)
|
||||||
|
for root, dirs, files in os.walk(path):
|
||||||
|
for d in dirs:
|
||||||
|
os.chown(path, uid, gid)
|
||||||
|
for f in files:
|
||||||
|
os.chown(os.path.join(root, f), uid, gid)
|
||||||
|
except OSError, e:
|
||||||
|
self.module.exit_json(failed=True, msg="%s" % e)
|
||||||
|
|
||||||
|
|
||||||
# ===========================================
|
# ===========================================
|
||||||
|
|
||||||
class FreeBsdUser(User):
|
class FreeBsdUser(User):
|
||||||
|
@ -1485,6 +1512,13 @@ def main():
|
||||||
result['ssh_key_file'] = user.get_ssh_key_path()
|
result['ssh_key_file'] = user.get_ssh_key_path()
|
||||||
result['ssh_public_key'] = user.get_ssh_public_key()
|
result['ssh_public_key'] = user.get_ssh_public_key()
|
||||||
|
|
||||||
|
# handle missing homedirs
|
||||||
|
if not os.path.exists(user.home) and user.createhome:
|
||||||
|
if not module.check_mode:
|
||||||
|
info = user.user_info()
|
||||||
|
user.create_homedir(user.home)
|
||||||
|
user.chown_homedir(info[2], info[3], user.home)
|
||||||
|
result['changed'] = True
|
||||||
|
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue