[stable-2.9] user - compare macOS user properties using same type (#62973)

self._get_user_property returns a string, so when doing a comparison
using this value, cast the second variable to a string so that the
comparison behaves correctly
* Add changelog
* Add to_text import
* Add integration test.
(cherry picked from commit c73288ad53)

Co-authored-by: John Chen <kryptonite303@users.noreply.github.com>
pull/66383/head
John Chen 5 years ago committed by Matt Clay
parent d2c21af3f7
commit 4a05724fd7

@ -0,0 +1,2 @@
bugfixes:
- user - fix comparison on macOS so module does not improperly report a change (https://github.com/ansible/ansible/issues/62969)

@ -420,8 +420,8 @@ import subprocess
import time import time
from ansible.module_utils import distro from ansible.module_utils import distro
from ansible.module_utils._text import to_native, to_bytes, to_text
from ansible.module_utils.basic import load_platform_subclass, AnsibleModule from ansible.module_utils.basic import load_platform_subclass, AnsibleModule
from ansible.module_utils._text import to_bytes, to_native, to_text
try: try:
import spwd import spwd
@ -2303,7 +2303,7 @@ class DarwinUser(User):
for field in self.fields: for field in self.fields:
if field[0] in self.__dict__ and self.__dict__[field[0]]: if field[0] in self.__dict__ and self.__dict__[field[0]]:
current = self._get_user_property(field[1]) current = self._get_user_property(field[1])
if current is None or current != self.__dict__[field[0]]: if current is None or current != to_text(self.__dict__[field[0]]):
cmd = self._get_dscl() cmd = self._get_dscl()
cmd += ['-create', '/Users/%s' % self.name, field[1], self.__dict__[field[0]]] cmd += ['-create', '/Users/%s' % self.name, field[1], self.__dict__[field[0]]]
(rc, _err, _out) = self.execute_command(cmd) (rc, _err, _out) = self.execute_command(cmd)

@ -55,6 +55,33 @@
- user_test0_1 is not changed - user_test0_1 is not changed
- '"ansibulluser" in user_names.stdout_lines' - '"ansibulluser" in user_names.stdout_lines'
# test adding user with uid
# https://github.com/ansible/ansible/issues/62969
- name: remove the test user
user:
name: ansibulluser
state: absent
- name: try to create a user with uid
user:
name: ansibulluser
state: present
uid: 572
register: user_test01_0
- name: create the user again
user:
name: ansibulluser
state: present
uid: 572
register: user_test01_1
- name: validate results for testcase 0
assert:
that:
- user_test01_0 is changed
- user_test01_1 is not changed
# test user add with password # test user add with password
- name: add an encrypted password for user - name: add an encrypted password for user
user: user:

Loading…
Cancel
Save