mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Refactor user_and_group so it works off of numbers
There are various cases where a UID to username to UID mapping breaks down. One UID can be used by two usernames, or no username. If we always use UIDs internally, then these ambiguous cases won't be a problem.
This commit is contained in:
parent
d2e457f81f
commit
bd226eb826
1 changed files with 33 additions and 31 deletions
|
@ -319,15 +319,7 @@ class AnsibleModule(object):
|
||||||
st = os.stat(filename)
|
st = os.stat(filename)
|
||||||
uid = st.st_uid
|
uid = st.st_uid
|
||||||
gid = st.st_gid
|
gid = st.st_gid
|
||||||
try:
|
return (uid, gid)
|
||||||
user = pwd.getpwuid(uid)[0]
|
|
||||||
except KeyError:
|
|
||||||
user = str(uid)
|
|
||||||
try:
|
|
||||||
group = grp.getgrgid(gid)[0]
|
|
||||||
except KeyError:
|
|
||||||
group = str(gid)
|
|
||||||
return (user, group)
|
|
||||||
|
|
||||||
def set_default_selinux_context(self, path, changed):
|
def set_default_selinux_context(self, path, changed):
|
||||||
if not HAVE_SELINUX or not self.selinux_enabled():
|
if not HAVE_SELINUX or not self.selinux_enabled():
|
||||||
|
@ -366,17 +358,17 @@ class AnsibleModule(object):
|
||||||
path = os.path.expanduser(path)
|
path = os.path.expanduser(path)
|
||||||
if owner is None:
|
if owner is None:
|
||||||
return changed
|
return changed
|
||||||
user, group = self.user_and_group(path)
|
orig_uid, orig_gid = self.user_and_group(path)
|
||||||
if owner != user:
|
try:
|
||||||
|
uid = int(owner)
|
||||||
|
except ValueError:
|
||||||
try:
|
try:
|
||||||
uid = int(owner)
|
uid = pwd.getpwnam(owner).pw_uid
|
||||||
except ValueError:
|
except KeyError:
|
||||||
try:
|
self.fail_json(path=path, msg='chown failed: failed to look up user %s' % owner)
|
||||||
uid = pwd.getpwnam(owner).pw_uid
|
if self.check_mode:
|
||||||
except KeyError:
|
return True
|
||||||
self.fail_json(path=path, msg='chown failed: failed to look up user %s' % owner)
|
if orig_uid != uid:
|
||||||
if self.check_mode:
|
|
||||||
return True
|
|
||||||
try:
|
try:
|
||||||
os.chown(path, uid, -1)
|
os.chown(path, uid, -1)
|
||||||
except OSError:
|
except OSError:
|
||||||
|
@ -388,17 +380,17 @@ class AnsibleModule(object):
|
||||||
path = os.path.expanduser(path)
|
path = os.path.expanduser(path)
|
||||||
if group is None:
|
if group is None:
|
||||||
return changed
|
return changed
|
||||||
old_user, old_group = self.user_and_group(path)
|
orig_uid, orig_gid = self.user_and_group(path)
|
||||||
if old_group != group:
|
try:
|
||||||
if self.check_mode:
|
gid = int(group)
|
||||||
return True
|
except ValueError:
|
||||||
try:
|
try:
|
||||||
gid = int(group)
|
gid = grp.getgrnam(group).gr_gid
|
||||||
except ValueError:
|
except KeyError:
|
||||||
try:
|
self.fail_json(path=path, msg='chgrp failed: failed to look up group %s' % group)
|
||||||
gid = grp.getgrnam(group).gr_gid
|
if self.check_mode:
|
||||||
except KeyError:
|
return True
|
||||||
self.fail_json(path=path, msg='chgrp failed: failed to look up group %s' % group)
|
if orig_gid != gid:
|
||||||
try:
|
try:
|
||||||
os.chown(path, -1, gid)
|
os.chown(path, -1, gid)
|
||||||
except OSError:
|
except OSError:
|
||||||
|
@ -478,8 +470,18 @@ class AnsibleModule(object):
|
||||||
if path is None:
|
if path is None:
|
||||||
return kwargs
|
return kwargs
|
||||||
if os.path.exists(path):
|
if os.path.exists(path):
|
||||||
(user, group) = self.user_and_group(path)
|
(uid, gid) = self.user_and_group(path)
|
||||||
kwargs['owner'] = user
|
kwargs['uid'] = uid
|
||||||
|
kwargs['gid'] = gid
|
||||||
|
try:
|
||||||
|
user = pwd.getpwuid(uid)[0]
|
||||||
|
except KeyError:
|
||||||
|
user = str(uid)
|
||||||
|
try:
|
||||||
|
group = grp.getgrgid(gid)[0]
|
||||||
|
except KeyError:
|
||||||
|
group = str(gid)
|
||||||
|
kwargs['owner'] = user
|
||||||
kwargs['group'] = group
|
kwargs['group'] = group
|
||||||
st = os.stat(path)
|
st = os.stat(path)
|
||||||
kwargs['mode'] = oct(stat.S_IMODE(st[stat.ST_MODE]))
|
kwargs['mode'] = oct(stat.S_IMODE(st[stat.ST_MODE]))
|
||||||
|
|
Loading…
Reference in a new issue