From d2e457f81f9432f3f4637af364550ee64528b319 Mon Sep 17 00:00:00 2001 From: Aaron Brady Date: Tue, 23 Apr 2013 14:05:01 +0100 Subject: [PATCH 1/2] Accept numeric user and group parameters --- lib/ansible/module_common.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/ansible/module_common.py b/lib/ansible/module_common.py index c5bb9d46b4..a57033910b 100644 --- a/lib/ansible/module_common.py +++ b/lib/ansible/module_common.py @@ -369,9 +369,12 @@ class AnsibleModule(object): user, group = self.user_and_group(path) if owner != user: try: - uid = pwd.getpwnam(owner).pw_uid - except KeyError: - self.fail_json(path=path, msg='chown failed: failed to look up user %s' % owner) + uid = int(owner) + except ValueError: + try: + uid = pwd.getpwnam(owner).pw_uid + except KeyError: + self.fail_json(path=path, msg='chown failed: failed to look up user %s' % owner) if self.check_mode: return True try: @@ -390,9 +393,12 @@ class AnsibleModule(object): if self.check_mode: return True try: - gid = grp.getgrnam(group).gr_gid - except KeyError: - self.fail_json(path=path, msg='chgrp failed: failed to look up group %s' % group) + gid = int(group) + except ValueError: + try: + gid = grp.getgrnam(group).gr_gid + except KeyError: + self.fail_json(path=path, msg='chgrp failed: failed to look up group %s' % group) try: os.chown(path, -1, gid) except OSError: From bd226eb826a396e5cab60330b0ce1ed9741a7f3c Mon Sep 17 00:00:00 2001 From: Aaron Brady Date: Tue, 23 Apr 2013 15:29:35 +0100 Subject: [PATCH 2/2] 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. --- lib/ansible/module_common.py | 64 +++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/lib/ansible/module_common.py b/lib/ansible/module_common.py index a57033910b..9d5efdef7f 100644 --- a/lib/ansible/module_common.py +++ b/lib/ansible/module_common.py @@ -319,15 +319,7 @@ class AnsibleModule(object): st = os.stat(filename) uid = st.st_uid gid = st.st_gid - try: - user = pwd.getpwuid(uid)[0] - except KeyError: - user = str(uid) - try: - group = grp.getgrgid(gid)[0] - except KeyError: - group = str(gid) - return (user, group) + return (uid, gid) def set_default_selinux_context(self, path, changed): if not HAVE_SELINUX or not self.selinux_enabled(): @@ -366,17 +358,17 @@ class AnsibleModule(object): path = os.path.expanduser(path) if owner is None: return changed - user, group = self.user_and_group(path) - if owner != user: + orig_uid, orig_gid = self.user_and_group(path) + try: + uid = int(owner) + except ValueError: try: - uid = int(owner) - except ValueError: - try: - uid = pwd.getpwnam(owner).pw_uid - except KeyError: - self.fail_json(path=path, msg='chown failed: failed to look up user %s' % owner) - if self.check_mode: - return True + uid = pwd.getpwnam(owner).pw_uid + except KeyError: + self.fail_json(path=path, msg='chown failed: failed to look up user %s' % owner) + if self.check_mode: + return True + if orig_uid != uid: try: os.chown(path, uid, -1) except OSError: @@ -388,17 +380,17 @@ class AnsibleModule(object): path = os.path.expanduser(path) if group is None: return changed - old_user, old_group = self.user_and_group(path) - if old_group != group: - if self.check_mode: - return True + orig_uid, orig_gid = self.user_and_group(path) + try: + gid = int(group) + except ValueError: try: - gid = int(group) - except ValueError: - try: - gid = grp.getgrnam(group).gr_gid - except KeyError: - self.fail_json(path=path, msg='chgrp failed: failed to look up group %s' % group) + gid = grp.getgrnam(group).gr_gid + except KeyError: + self.fail_json(path=path, msg='chgrp failed: failed to look up group %s' % group) + if self.check_mode: + return True + if orig_gid != gid: try: os.chown(path, -1, gid) except OSError: @@ -478,8 +470,18 @@ class AnsibleModule(object): if path is None: return kwargs if os.path.exists(path): - (user, group) = self.user_and_group(path) - kwargs['owner'] = user + (uid, gid) = self.user_and_group(path) + 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 st = os.stat(path) kwargs['mode'] = oct(stat.S_IMODE(st[stat.ST_MODE]))