| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
from __future__ import absolute_import, division, print_function |
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
DOCUMENTATION = ''' |
| 13 |
--- |
| 14 |
module: ibm_svc_manage_user |
| 15 |
short_description: This module manages user on IBM Storage Virtualize family systems |
| 16 |
description: |
| 17 |
- Ansible interface to manage 'mkuser', 'rmuser', and 'chuser' commands. |
| 18 |
version_added: "1.7.0" |
| 19 |
options: |
| 20 |
name: |
| 21 |
description: |
| 22 |
- Specifies the unique username. |
| 23 |
required: true |
| 24 |
type: str |
| 25 |
state: |
| 26 |
description: |
| 27 |
- Creates or updates (C(present)) or removes (C(absent)) a user. |
| 28 |
choices: [ present, absent ] |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
clustername: |
| 32 |
description: |
| 33 |
- The hostname or management IP of the Storage Virtualize system. |
| 34 |
type: str |
| 35 |
required: true |
| 36 |
domain: |
| 37 |
description: |
| 38 |
- Domain for the Storage Virtualize system. |
| 39 |
- Valid when hostname is used for the parameter I(clustername). |
| 40 |
type: str |
| 41 |
username: |
| 42 |
description: |
| 43 |
- REST API username for the Storage Virtualize system. |
| 44 |
- The parameters I(username) and I(password) are required if not using I(token) to authenticate a user. |
| 45 |
type: str |
| 46 |
password: |
| 47 |
description: |
| 48 |
- REST API password for the Storage Virtualize system. |
| 49 |
- The parameters I(username) and I(password) are required if not using I(token) to authenticate a user. |
| 50 |
type: str |
| 51 |
token: |
| 52 |
description: |
| 53 |
- The authentication token to verify a user on the Storage Virtualize system. |
| 54 |
- To generate a token, use the ibm_svc_auth module. |
| 55 |
type: str |
| 56 |
user_password: |
| 57 |
description: |
| 58 |
- Specifies the password associated with the user. |
| 59 |
- Applies when I(state=present). |
| 60 |
type: str |
| 61 |
nopassword: |
| 62 |
description: |
| 63 |
- Specifies that the user's password is to be deleted. |
| 64 |
- Applies when I(state=present), to modify a user. |
| 65 |
type: bool |
| 66 |
keyfile: |
| 67 |
description: |
| 68 |
- Specifies the name of the file containing the Secure Shell (SSH) public key. |
| 69 |
- Applies when I(state=present). |
| 70 |
type: str |
| 71 |
nokey: |
| 72 |
description: |
| 73 |
- Specifies that the user's SSH key is to be deleted. |
| 74 |
- Applies when I(state=present), to modify a user. |
| 75 |
type: bool |
| 76 |
auth_type: |
| 77 |
description: |
| 78 |
- Specifies whether the user authenticates to the system using a remote authentication service or system authentication methods. |
| 79 |
- Only supported value is 'usergrp'. |
| 80 |
- Required when I(state=present), to create a user. |
| 81 |
choices: [ usergrp ] |
| 82 |
type: str |
| 83 |
usergroup: |
| 84 |
description: |
| 85 |
- Specifies the name of the user group with which the local user is to be associated. |
| 86 |
- Applies when I(state=present) and I(auth_type=usergrp). |
| 87 |
type: str |
| 88 |
forcepasswordchange: |
| 89 |
description: |
| 90 |
- Specifies that the password is to be changed on next login. |
| 91 |
- Applies when I(state=present), to modify a user. |
| 92 |
type: bool |
| 93 |
lock: |
| 94 |
description: |
| 95 |
- Specifies to lock the account indefinitely. The user cannot log in unless unlocked again with the parameter I(unlock). |
| 96 |
- Applies when I(state=present), to modify a user. |
| 97 |
- Parameters I(lock) and I(unlock) are mutually exclusive. |
| 98 |
type: bool |
| 99 |
unlock: |
| 100 |
description: |
| 101 |
- Specifies to unlock the account so it can be logged in to again. |
| 102 |
- Applies when I(state=present), to modify a user. |
| 103 |
- Parameters I(lock) and I(unlock) are mutually exclusive. |
| 104 |
type: bool |
| 105 |
validate_certs: |
| 106 |
description: |
| 107 |
- Validates certification. |
| 108 |
default: false |
| 109 |
type: bool |
| 110 |
log_path: |
| 111 |
description: |
| 112 |
- Path of debug log file. |
| 113 |
type: str |
| 114 |
author: |
| 115 |
- Sreshtant Bohidar(@Sreshtant-Bohidar) |
| 116 |
notes: |
| 117 |
- This module supports C(check_mode). |
| 118 |
''' |
| 119 |
|
| 120 |
EXAMPLES = ''' |
| 121 |
- name: Create a user |
| 122 |
ibm.storage_virtualize.ibm_svc_manage_user: |
| 123 |
clustername: "{{ clustername }}" |
| 124 |
domain: "{{ domain }}" |
| 125 |
username: "{{ username }}" |
| 126 |
password: "{{ password }}" |
| 127 |
log_path: /tmp/playbook.debug |
| 128 |
state: present |
| 129 |
name: user-name |
| 130 |
user_password: user-password |
| 131 |
auth_type: usergrp |
| 132 |
usergroup: usergroup-name |
| 133 |
- name: Remove a user |
| 134 |
ibm.storage_virtualize.ibm_svc_manage_user: |
| 135 |
clustername: "{{ clustername }}" |
| 136 |
domain: "{{ domain }}" |
| 137 |
username: "{{ username }}" |
| 138 |
password: "{{ password }}" |
| 139 |
log_path: /tmp/playbook.debug |
| 140 |
state: absent |
| 141 |
name: user-name |
| 142 |
''' |
| 143 |
|
| 144 |
RETURN = '''#''' |
| 145 |
|
| 146 |
from traceback import format_exc |
| 147 |
from ansible.module_utils.basic import AnsibleModule |
| 148 |
from ansible_collections.ibm.storage_virtualize.plugins.module_utils.ibm_svc_utils import IBMSVCRestApi, svc_argument_spec, get_logger |
| 149 |
from ansible.module_utils._text import to_native |
| 150 |
|
| 151 |
|
| 152 |
class IBMSVCUser(object): |
| 153 |
def __init__(self): |
| 154 |
argument_spec = svc_argument_spec() |
| 155 |
argument_spec.update( |
| 156 |
dict( |
| 157 |
name=dict(type='str', required=True), |
| 158 |
state=dict(type='str', required=True, choices=['present', 'absent']), |
| 159 |
auth_type=dict(type='str', required=False, choices=['usergrp']), |
| 160 |
user_password=dict(type='str', required=False, no_log=True), |
| 161 |
nopassword=dict(type='bool', required=False), |
| 162 |
keyfile=dict(type='str', required=False, no_log=True), |
| 163 |
nokey=dict(type='bool', required=False), |
| 164 |
forcepasswordchange=dict(type='bool', required=False), |
| 165 |
lock=dict(type='bool', required=False), |
| 166 |
unlock=dict(type='bool', required=False), |
| 167 |
usergroup=dict(type='str', required=False), |
| 168 |
) |
| 169 |
) |
| 170 |
|
| 171 |
self.module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 172 |
|
| 173 |
|
| 174 |
log_path = self.module.params['log_path'] |
| 175 |
log = get_logger(self.__class__.__name__, log_path) |
| 176 |
self.log = log.info |
| 177 |
|
| 178 |
|
| 179 |
self.name = self.module.params['name'] |
| 180 |
self.state = self.module.params['state'] |
| 181 |
|
| 182 |
|
| 183 |
self.auth_type = self.module.params['auth_type'] |
| 184 |
self.usergroup = self.module.params['usergroup'] |
| 185 |
|
| 186 |
|
| 187 |
self.user_password = self.module.params.get('user_password', False) |
| 188 |
self.nopassword = self.module.params.get('nopassword', False) |
| 189 |
self.keyfile = self.module.params.get('keyfile', False) |
| 190 |
self.nokey = self.module.params.get('nokey', False) |
| 191 |
self.forcepasswordchange = self.module.params.get('forcepasswordchange', False) |
| 192 |
self.lock = self.module.params.get('lock', False) |
| 193 |
self.unlock = self.module.params.get('unlock', False) |
| 194 |
|
| 195 |
|
| 196 |
self.restapi = IBMSVCRestApi( |
| 197 |
module=self.module, |
| 198 |
clustername=self.module.params['clustername'], |
| 199 |
domain=self.module.params['domain'], |
| 200 |
username=self.module.params['username'], |
| 201 |
password=self.module.params['password'], |
| 202 |
validate_certs=self.module.params['validate_certs'], |
| 203 |
log_path=log_path, |
| 204 |
token=self.module.params['token'] |
| 205 |
) |
| 206 |
|
| 207 |
|
| 208 |
def basic_checks(self): |
| 209 |
|
| 210 |
if not self.name: |
| 211 |
self.module.fail_json(msg="Missing mandatory parameter: name") |
| 212 |
|
| 213 |
if not self.state: |
| 214 |
self.module.fail_json(msg="Missing mandatory parameter: state") |
| 215 |
|
| 216 |
if self.user_password and self.nopassword: |
| 217 |
self.module.fail_json(msg="Mutually exclusive parameter: user_password, nopassword") |
| 218 |
if self.lock and self.unlock: |
| 219 |
self.module.fail_json(msg="Mutually exclusive parameter: lock, unlock") |
| 220 |
if self.keyfile and self.nokey: |
| 221 |
self.module.fail_json(msg="Mutually exclusive parameter: keyfile, nokey") |
| 222 |
if self.auth_type == 'usergrp' and not self.usergroup: |
| 223 |
self.module.fail_json(msg="Parameter [usergroup] is required when auth_type is usergrp") |
| 224 |
|
| 225 |
|
| 226 |
def get_existing_user(self): |
| 227 |
merged_result = {} |
| 228 |
data = self.restapi.svc_obj_info(cmd='lsuser', cmdopts=None, cmdargs=[self.name]) |
| 229 |
self.log('GET: user data: %s', data) |
| 230 |
if isinstance(data, list): |
| 231 |
for d in data: |
| 232 |
merged_result.update(d) |
| 233 |
else: |
| 234 |
merged_result = data |
| 235 |
|
| 236 |
return merged_result |
| 237 |
|
| 238 |
|
| 239 |
def create_user(self): |
| 240 |
|
| 241 |
if self.nokey or self.nopassword or self.lock or self.unlock or self.forcepasswordchange: |
| 242 |
self.module.fail_json(msg="Parameters [nokey, nopassword, lock, unlock, forcepasswordchange] not applicable while creating a user") |
| 243 |
|
| 244 |
if not self.auth_type: |
| 245 |
self.module.fail_json(msg="Missing required parameter: auth_type") |
| 246 |
if self.auth_type == 'usergrp' and not self.usergroup: |
| 247 |
self.module.fail_json(msg="Missing required parameter: usergroup") |
| 248 |
if self.module.check_mode: |
| 249 |
self.changed = True |
| 250 |
return |
| 251 |
command = 'mkuser' |
| 252 |
command_options = { |
| 253 |
'name': self.name, |
| 254 |
} |
| 255 |
if self.user_password: |
| 256 |
command_options['password'] = self.user_password |
| 257 |
if self.keyfile: |
| 258 |
command_options['keyfile'] = self.keyfile |
| 259 |
if self.usergroup: |
| 260 |
command_options['usergrp'] = self.usergroup |
| 261 |
if self.forcepasswordchange: |
| 262 |
command_options['forcepasswordchange'] = self.forcepasswordchange |
| 263 |
|
| 264 |
result = self.restapi.svc_run_command(command, command_options, cmdargs=None) |
| 265 |
self.log("create user result %s", result) |
| 266 |
if 'message' in result: |
| 267 |
self.changed = True |
| 268 |
self.log("create user result message %s", result['message']) |
| 269 |
else: |
| 270 |
self.module.fail_json( |
| 271 |
msg="Failed to create user [%s]" % self.name) |
| 272 |
|
| 273 |
|
| 274 |
def probe_user(self, data): |
| 275 |
properties = {} |
| 276 |
|
| 277 |
if self.usergroup: |
| 278 |
if self.usergroup != data['usergrp_name']: |
| 279 |
properties['usergrp'] = self.usergroup |
| 280 |
if self.user_password: |
| 281 |
properties['password'] = self.user_password |
| 282 |
if self.nopassword: |
| 283 |
if data['password'] == 'yes': |
| 284 |
properties['nopassword'] = True |
| 285 |
if self.keyfile: |
| 286 |
properties['keyfile'] = self.keyfile |
| 287 |
if self.nokey: |
| 288 |
if data['ssh_key'] == "yes": |
| 289 |
properties['nokey'] = True |
| 290 |
if self.lock: |
| 291 |
properties['lock'] = True |
| 292 |
if self.unlock: |
| 293 |
properties['unlock'] = True |
| 294 |
if self.forcepasswordchange: |
| 295 |
properties['forcepasswordchange'] = True |
| 296 |
|
| 297 |
return properties |
| 298 |
|
| 299 |
|
| 300 |
def update_user(self, data): |
| 301 |
if self.module.check_mode: |
| 302 |
self.changed = True |
| 303 |
return |
| 304 |
self.log("updating user '%s'", self.name) |
| 305 |
command = 'chuser' |
| 306 |
for parameter in data: |
| 307 |
command_options = { |
| 308 |
parameter: data[parameter] |
| 309 |
} |
| 310 |
self.restapi.svc_run_command(command, command_options, [self.name]) |
| 311 |
self.changed = True |
| 312 |
|
| 313 |
|
| 314 |
def remove_user(self): |
| 315 |
|
| 316 |
if self.nokey or self.nopassword or self.lock or self.unlock or self.forcepasswordchange: |
| 317 |
self.module.fail_json(msg="Parameters [nokey, nopassword, lock, unlock, forcepasswordchange] not applicable while removing a user") |
| 318 |
if self.module.check_mode: |
| 319 |
self.changed = True |
| 320 |
return |
| 321 |
self.log("deleting user '%s'", self.name) |
| 322 |
command = 'rmuser' |
| 323 |
command_options = None |
| 324 |
cmdargs = [self.name] |
| 325 |
self.restapi.svc_run_command(command, command_options, cmdargs) |
| 326 |
self.changed = True |
| 327 |
|
| 328 |
def apply(self): |
| 329 |
changed = False |
| 330 |
msg = None |
| 331 |
modify = {} |
| 332 |
self.basic_checks() |
| 333 |
|
| 334 |
user_data = self.get_existing_user() |
| 335 |
|
| 336 |
if user_data: |
| 337 |
if self.state == 'absent': |
| 338 |
self.log("CHANGED: user exists, but requested state is 'absent'") |
| 339 |
changed = True |
| 340 |
elif self.state == 'present': |
| 341 |
|
| 342 |
modify = self.probe_user(user_data) |
| 343 |
if modify: |
| 344 |
self.log("CHANGED: user exists, but probe detected changes") |
| 345 |
changed = True |
| 346 |
else: |
| 347 |
if self.state == 'present': |
| 348 |
self.log("CHANGED: user does not exist, but requested state is 'present'") |
| 349 |
changed = True |
| 350 |
if changed: |
| 351 |
if self.state == 'present': |
| 352 |
if not user_data: |
| 353 |
|
| 354 |
self.create_user() |
| 355 |
msg = "User [%s] has been created." % self.name |
| 356 |
else: |
| 357 |
|
| 358 |
self.update_user(modify) |
| 359 |
msg = "User [%s] has been modified." % self.name |
| 360 |
elif self.state == 'absent': |
| 361 |
|
| 362 |
self.remove_user() |
| 363 |
msg = "User [%s] has been removed." % self.name |
| 364 |
if self.module.check_mode: |
| 365 |
msg = "Skipping changes due to check mode." |
| 366 |
else: |
| 367 |
if self.state == 'absent': |
| 368 |
msg = "User [%s] does not exist." % self.name |
| 369 |
elif self.state == 'present': |
| 370 |
msg = "User [%s] already exist (no modificationes detected)." % self.name |
| 371 |
|
| 372 |
self.module.exit_json(msg=msg, changed=changed) |
| 373 |
|
| 374 |
|
| 375 |
def main(): |
| 376 |
v = IBMSVCUser() |
| 377 |
try: |
| 378 |
v.apply() |
| 379 |
except Exception as e: |
| 380 |
v.log("Exception in apply(): \n%s", format_exc()) |
| 381 |
v.module.fail_json(msg="Module failed. Error [%s]." % to_native(e)) |
| 382 |
|
| 383 |
|
| 384 |
if __name__ == '__main__': |
| 385 |
main() |