From 507b96ff3031d5d5ee3ec36c7c0e2f00b674ab76 Mon Sep 17 00:00:00 2001 From: Rezart Qelibari Date: Mon, 19 Dec 2016 22:35:56 +0100 Subject: [PATCH] user: make system=yes work on Darwin systems. (#19464) * Update system/user.py module. Add ability to add real system users with next free system uid (< 500) on macOS. * Improve syntax in system/user.py module. Remove complex if else line and replace by simple comparison which yields the same boolean value. * Remove "True" comparison of user.py. Remove comparison to true, as it is not pep8 conform. --- lib/ansible/modules/system/user.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/ansible/modules/system/user.py b/lib/ansible/modules/system/user.py index 32e62e0dcc3..5ef58322306 100644 --- a/lib/ansible/modules/system/user.py +++ b/lib/ansible/modules/system/user.py @@ -1537,8 +1537,11 @@ class DarwinUser(User): else: return None - def _get_next_uid(self): - '''Return the next available uid''' + def _get_next_uid(self, system=None): + ''' + Return the next available uid. If system=True, then + uid should be below of 500, if possible. + ''' cmd = self._get_dscl() cmd += ['-list', '/Users', 'UniqueID'] (rc, out, err) = self.execute_command(cmd, obey_checkmode=False) @@ -1549,10 +1552,18 @@ class DarwinUser(User): out=out, err=err ) + max_uid = 0 + max_system_uid = 0 for line in out.splitlines(): - if max_uid < int(line.split()[1]): - max_uid = int(line.split()[1]) + current_uid = int(line.split(' ')[-1]) + if max_uid < current_uid: + max_uid = current_uid + if max_system_uid < current_uid and current_uid < 500: + max_system_uid = current_uid + + if system and (0 < max_system_uid < 499): + return max_system_uid + 1 return max_uid + 1 def _change_user_password(self): @@ -1711,7 +1722,7 @@ class DarwinUser(User): self._make_group_numerical() if self.uid is None: - self.uid = str(self._get_next_uid()) + self.uid = str(self._get_next_uid(self.system)) # Homedir is not created by default if self.createhome: