From 66e392d4e219bf93d690c1fb3d3c854069efa27f Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Fri, 7 Jan 2022 00:48:39 +0530 Subject: [PATCH] user: Update logic to check if user exists in macOS (#76592) 'dscl -list' returns 0 even if the user does not exists. This leads to errorenous condition in user module. Using 'dscl -read UniqueID' can return if user exists or not. Signed-off-by: Abhijeet Kasurde --- changelogs/fragments/user_mac.yaml | 2 ++ lib/ansible/modules/user.py | 15 ++++++--------- 2 files changed, 8 insertions(+), 9 deletions(-) create mode 100644 changelogs/fragments/user_mac.yaml diff --git a/changelogs/fragments/user_mac.yaml b/changelogs/fragments/user_mac.yaml new file mode 100644 index 00000000000..94d1c6d22d8 --- /dev/null +++ b/changelogs/fragments/user_mac.yaml @@ -0,0 +1,2 @@ +bugfixes: +- user - update logic to check if user exists or not in MacOS. diff --git a/lib/ansible/modules/user.py b/lib/ansible/modules/user.py index c129328968d..9f478b25ad4 100644 --- a/lib/ansible/modules/user.py +++ b/lib/ansible/modules/user.py @@ -2283,14 +2283,11 @@ class DarwinUser(User): # sys.stderr.write('*** |%s| %s -> %s\n' % (property, out, lines)) if len(lines) == 1: return lines[0].split(': ')[1] - else: - if len(lines) > 2: - return '\n'.join([lines[1].strip()] + lines[2:]) - else: - if len(lines) == 2: - return lines[1].strip() - else: - return None + if len(lines) > 2: + return '\n'.join([lines[1].strip()] + lines[2:]) + if len(lines) == 2: + return lines[1].strip() + return None def _get_next_uid(self, system=None): ''' @@ -2444,7 +2441,7 @@ class DarwinUser(User): def user_exists(self): '''Check is SELF.NAME is a known user on the system.''' cmd = self._get_dscl() - cmd += ['-list', '/Users/%s' % self.name] + cmd += ['-read', '/Users/%s' % self.name, 'UniqueID'] (rc, out, err) = self.execute_command(cmd, obey_checkmode=False) return rc == 0