Skip to content

Commit 872ac8a

Browse files
authored
User module is now able to create local user even if the user exists in active directory (LDAP) (#297)
Signed-off-by: schamola <[email protected]>
1 parent b425486 commit 872ac8a

File tree

3 files changed

+66
-23
lines changed

3 files changed

+66
-23
lines changed

playbooks/demo_user.yml

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,51 @@
1010
collections:
1111
ibm.power_aix
1212
tasks:
13-
- name: Create user
13+
- name: Create user in LDAP
1414
user:
1515
state: present
1616
name: "{{user_name}}"
1717
change_passwd_on_login: False
1818
password: "{{password_val}}"
19+
load_module: LDAP
1920
attributes:
2021
home: "{{attribute_home}}"
2122
data: 1272
22-
- name: Modify user
23+
- name: Create local user
24+
user:
25+
state: present
26+
name: testuser
27+
change_passwd_on_login: False
28+
password: "{{password_val}}"
29+
load_module: files
30+
attributes:
31+
home: "{{attribute_home}}"
32+
data: 1272
33+
- name: Create a local user even if it is present in LDAP
34+
user:
35+
state: present
36+
name: "{{user_name}}"
37+
change_passwd_on_login: False
38+
password: "{{password_val}}"
39+
load_module: files
40+
attributes:
41+
home: "{{attribute_home}}"
42+
data: 1272
43+
- name: Modify locally present user
44+
user:
45+
state: modify
46+
name: "{{user_name}}"
47+
attributes:
48+
admin: true
49+
- name: Modify User present in LDAP
2350
user:
2451
state: modify
2552
name: "{{user_name}}"
53+
load_module: LDAP
2654
attributes:
2755
admin: true
28-
- name: Delete user
56+
- name: Delete Local user
2957
user:
3058
state: absent
3159
name: "{{user_name}}"
32-
remove_homedir: False
60+
remove_homedir: False

plugins/modules/user.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@
7474
- Specifies the encrypted string for the password to create or change the password.
7575
- Can be used when I(state=present) or I(state=modify).
7676
type: str
77+
load_module:
78+
description:
79+
- Specifies the location where the operations need to be performed on the user.
80+
- C(files) creates/updates/deletes the user present in the Local machine.
81+
- C(LDAP) creates/updates the user present in the LDAP server.
82+
type: str
83+
default: 'files'
84+
choices: [files, LDAP]
7785
notes:
7886
- You can refer to the IBM documentation for additional information on the commands used at
7987
U(https://www.ibm.com/support/knowledgecenter/ssw_aix_72/c_commands/chuser.html),
@@ -134,7 +142,7 @@ def get_chuser_command(module):
134142
return None
135143

136144
# 'user_attrs' contains the key=value pairs that are _currently_ set in AIX
137-
lsuser_cmd = "lsuser -C %s" % module.params['name']
145+
lsuser_cmd = "lsuser -R %s -C %s" % (module.params['load_module'], module.params['name'])
138146
rc, stdout, stderr = module.run_command(lsuser_cmd)
139147
if rc != 0:
140148
msg = "\nFailed to validate attributes for the user: %s" % module.params['name']
@@ -143,23 +151,23 @@ def get_chuser_command(module):
143151
values = stdout.splitlines()[1].split(':')
144152
user_attrs = dict(zip(keys, values))
145153

154+
# Adding the load module to the command so that the correct user's attributes are changed.
155+
load_module_opts = "-R %s " % module.params['load_module']
156+
146157
# Now loop over every key-value in attributes
147158
opts = ""
148159
cmd = ""
149160
load_module_opts = None
150161
for attr, val in attributes.items():
151-
if attr == 'load_module':
152-
load_module_opts = "-R %s " % val
153-
else:
154-
pattern = re.compile(r'(yes|true|always|no|false|never)', re.IGNORECASE)
155-
if val in [True, False] or re.match(pattern, str(val)):
156-
val = str(val).lower()
157-
# For idempotency, we compare what Anisble whats the value to be
158-
# compared to what is already set
159-
# Only add attr=val to the opts list they're different. No reason to
160-
# if the values are identical!
161-
if user_attrs[attr] != val:
162-
opts += "%s=\"%s\" " % (attr, val)
162+
pattern = re.compile(r'(yes|true|always|no|false|never)', re.IGNORECASE)
163+
if val in [True, False] or re.match(pattern, str(val)):
164+
val = str(val).lower()
165+
# For idempotency, we compare what Anisble whats the value to be
166+
# compared to what is already set
167+
# Only add attr=val to the opts list they're different. No reason to
168+
# if the values are identical!
169+
if user_attrs[attr] != val:
170+
opts += "%s=\"%s\" " % (attr, val)
163171

164172
if load_module_opts is not None:
165173
opts = load_module_opts + opts
@@ -232,12 +240,12 @@ def create_user(module):
232240
load_module_opts = None
233241
msg = ""
234242

243+
# Adding the load module to the command so that the user is created at the right location.
244+
load_module_opts = "-R %s " % module.params['load_module']
245+
235246
if attributes is not None:
236247
for attr, val in attributes.items():
237-
if attr == 'load_module':
238-
load_module_opts = "-R %s " % val
239-
else:
240-
opts += "%s=\"%s\" " % (attr, val)
248+
opts += "%s=\"%s\" " % (attr, val)
241249
if load_module_opts is not None:
242250
opts = load_module_opts + opts
243251
cmd = "mkuser %s %s" % (opts, module.params['name'])
@@ -294,8 +302,12 @@ def user_exists(module):
294302
True if the user exists
295303
False if the user does not exist
296304
'''
297-
cmd = ["lsuser"]
298-
cmd.append(module.params['name'])
305+
cmd = "lsuser "
306+
307+
# Adding the load module to the command so that the user's existence is checked at the right location.
308+
load_module_opts = "-R %s" % module.params['load_module']
309+
cmd += load_module_opts
310+
cmd += " %s" % module.params['name']
299311

300312
rc, out, err = module.run_command(cmd)
301313
if rc == 0:
@@ -325,6 +337,7 @@ def change_password(module):
325337
else:
326338
cmd = "echo \'{user}:{password}\' | chpasswd -e -c".format(user=name, password=passwd)
327339

340+
cmd += " -R %s" % module.params['load_module']
328341
pass_rc, pass_out, pass_err = module.run_command(cmd, use_unsafe_shell=True)
329342
if pass_rc != 0:
330343
msg = "\nFailed to set password for the user: %s" % module.params['name']
@@ -344,6 +357,7 @@ def main():
344357
remove_homedir=dict(type='bool', default=True, no_log=False),
345358
change_passwd_on_login=dict(type='bool', default=False, no_log=False),
346359
password=dict(type='str', no_log=True),
360+
load_module=dict(type='str', default='files', choices=['files', 'LDAP']),
347361
),
348362
supports_check_mode=False
349363
)

tests/unit/plugins/modules/test_user.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def setUp(self):
2626
params["remove_homedir"] = True
2727
params["change_passwd_on_login"] = True
2828
params["password"] = "pass1234"
29+
params["load_module"] = "files"
2930
self.module.params = params
3031
rc, stdout, stderr = 0, "sample stdout", "sample stderr"
3132
self.module.run_command.return_value = (rc, stdout, stderr)

0 commit comments

Comments
 (0)