| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
ANSIBLE_METADATA = { |
| 12 |
"metadata_version": "1.1", |
| 13 |
"status": ["preview"], |
| 14 |
"supported_by": "community", |
| 15 |
} |
| 16 |
|
| 17 |
DOCUMENTATION = r""" |
| 18 |
--- |
| 19 |
module: purefb_user |
| 20 |
version_added: '1.0.0' |
| 21 |
short_description: Create, modify or delete FlashBlade user accounts |
| 22 |
description: |
| 23 |
- Modify user on a Pure Stoage FlashBlade. |
| 24 |
author: |
| 25 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 26 |
options: |
| 27 |
state: |
| 28 |
description: |
| 29 |
- Create, delete or update local user account |
| 30 |
default: present |
| 31 |
type: str |
| 32 |
choices: [ absent, present ] |
| 33 |
version_added: "1.21.0" |
| 34 |
name: |
| 35 |
description: |
| 36 |
- The name of the user account |
| 37 |
type: str |
| 38 |
required: true |
| 39 |
role: |
| 40 |
description: |
| 41 |
- Sets the local user's access level to the system |
| 42 |
type: str |
| 43 |
default: readonly |
| 44 |
choices: [ readonly, ops_admin, storage_admin, array_admin ] |
| 45 |
version_added: "1.21.0" |
| 46 |
password: |
| 47 |
description: |
| 48 |
- Password for the local user. |
| 49 |
type: str |
| 50 |
old_password: |
| 51 |
description: |
| 52 |
- If changing an existing password, you must provide the old password for security |
| 53 |
type: str |
| 54 |
api: |
| 55 |
description: |
| 56 |
- Define whether to create an API token for this user |
| 57 |
- Token can be exposed using the I(debug) module |
| 58 |
type: bool |
| 59 |
default: false |
| 60 |
version_added: "1.21.0" |
| 61 |
timeout: |
| 62 |
description: |
| 63 |
- The duration of API token validity. |
| 64 |
- Valid values are weeks (w), days(d), hours(h), minutes(m) and seconds(s). |
| 65 |
type: str |
| 66 |
default: "0" |
| 67 |
version_added: "1.21.0" |
| 68 |
public_key: |
| 69 |
description: |
| 70 |
- The API clients PEM formatted (Base64 encoded) RSA public key. |
| 71 |
- Include the I(—–BEGIN PUBLIC KEY—–) and I(—–END PUBLIC KEY—–) lines |
| 72 |
type: str |
| 73 |
version_added: "1.8.0" |
| 74 |
clear_lock: |
| 75 |
description: |
| 76 |
- Clear user lockout flag |
| 77 |
type: bool |
| 78 |
default: false |
| 79 |
version_added: "1.8.0" |
| 80 |
ad_user: |
| 81 |
description: |
| 82 |
- Whether the user is in the AD system |
| 83 |
- Not required for local users |
| 84 |
type: bool |
| 85 |
default: false |
| 86 |
version_added: "1.21.0" |
| 87 |
extends_documentation_fragment: |
| 88 |
- purestorage.flashblade.purestorage.fb |
| 89 |
""" |
| 90 |
|
| 91 |
EXAMPLES = r""" |
| 92 |
- name: Change password for local user (NOT IDEMPOTENT) |
| 93 |
purestorage.flashblade.purefb_user: |
| 94 |
name: pureuser |
| 95 |
password: anewpassword |
| 96 |
old_password: apassword |
| 97 |
fb_url: 10.10.10.2 |
| 98 |
api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6 |
| 99 |
|
| 100 |
- name: Set public key for user |
| 101 |
purestorage.flashblade.purefb_user: |
| 102 |
name: fred |
| 103 |
public_key: "{{lookup('file', 'public_pem_file') }}" |
| 104 |
fb_url: 10.10.10.2 |
| 105 |
api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6 |
| 106 |
|
| 107 |
- name: Clear user lockout |
| 108 |
purestorage.flashblade.purefb_user: |
| 109 |
name: fred |
| 110 |
clear_lock: true |
| 111 |
fb_url: 10.10.10.2 |
| 112 |
api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6 |
| 113 |
|
| 114 |
- name: Create an API token (TTL of 2 days) and assign a public key to an AD user |
| 115 |
purestorage.flashblade.purefb_user: |
| 116 |
name: ansible-ad |
| 117 |
ad_user: true |
| 118 |
public_key: "{{lookup('file', 'id_rsa.pub') }}" |
| 119 |
api: true |
| 120 |
timeout: 2d |
| 121 |
fb_url: 10.10.10.2 |
| 122 |
api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6 |
| 123 |
""" |
| 124 |
|
| 125 |
RETURN = r""" |
| 126 |
""" |
| 127 |
|
| 128 |
HAS_PURESTORAGE = True |
| 129 |
try: |
| 130 |
from pypureclient.flashblade import ( |
| 131 |
AdminPatch, |
| 132 |
AdminPost, |
| 133 |
AdminRole, |
| 134 |
ReferenceWritable, |
| 135 |
) |
| 136 |
except ImportError: |
| 137 |
HAS_PURESTORAGE = False |
| 138 |
|
| 139 |
from ansible.module_utils.basic import AnsibleModule |
| 140 |
from ansible_collections.purestorage.flashblade.plugins.module_utils.common import ( |
| 141 |
convert_time_to_millisecs, |
| 142 |
) |
| 143 |
from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import ( |
| 144 |
get_system, |
| 145 |
purefb_argument_spec, |
| 146 |
) |
| 147 |
import re |
| 148 |
|
| 149 |
MIN_LOCAL_USER = "2.15" |
| 150 |
MIN_EXPOSE_API = "2.18" |
| 151 |
|
| 152 |
|
| 153 |
def get_user(module, blade): |
| 154 |
"""Return Local User Account or None""" |
| 155 |
user = None |
| 156 |
res = blade.get_admins(names=[module.params["name"]]) |
| 157 |
if res.status_code != 200: |
| 158 |
return None |
| 159 |
else: |
| 160 |
return list(res.items)[0] |
| 161 |
|
| 162 |
|
| 163 |
def create_local_user(module, blade, user): |
| 164 |
"""Create or Update Local User Account""" |
| 165 |
changed = key_changed = api_changed = role_changed = passwd_changed = False |
| 166 |
role = module.params["role"] |
| 167 |
api_token = "No API token created" |
| 168 |
if not user: |
| 169 |
changed = True |
| 170 |
if not module.check_mode: |
| 171 |
res = blade.post_admins( |
| 172 |
names=[module.params["name"]], |
| 173 |
admin=AdminPost( |
| 174 |
role=ReferenceWritable(name=role), |
| 175 |
password=module.params["password"], |
| 176 |
), |
| 177 |
) |
| 178 |
if res.status_code != 200: |
| 179 |
module.fail_json( |
| 180 |
msg="Failed to create user {0}. Error: {1}".format( |
| 181 |
module.params["name"], res.errors[0].message |
| 182 |
) |
| 183 |
) |
| 184 |
if module.params["api"]: |
| 185 |
ttl = convert_time_to_millisecs(module.params["timeout"]) |
| 186 |
res = blade.post_admins_api_tokens( |
| 187 |
admin_names=[module.params["name"]], timeout=ttl |
| 188 |
) |
| 189 |
if res.status_code != 200: |
| 190 |
module.fail_json( |
| 191 |
msg="Failed to create API token. Error: {0}".format( |
| 192 |
res.errors[0].message |
| 193 |
) |
| 194 |
) |
| 195 |
api_token = list(res.items)[0].api_token.token |
| 196 |
if module.params["public_key"]: |
| 197 |
res = blade.patch_admin( |
| 198 |
names=[module.params["name"]], |
| 199 |
admin=AdminPatch(public_key=module.params["public_key"]), |
| 200 |
) |
| 201 |
if res.status_code != 200: |
| 202 |
module.fail_json( |
| 203 |
msg="Failed to add SSH key. Error: {0}".format( |
| 204 |
res.errors[0].message |
| 205 |
) |
| 206 |
) |
| 207 |
else: |
| 208 |
if module.params["password"] and not module.params["old_password"]: |
| 209 |
module.exit_json(changed=changed) |
| 210 |
if ( |
| 211 |
module.params["password"] |
| 212 |
and module.params["old_password"] |
| 213 |
and module.params["password"] != module.params["old_password"] |
| 214 |
): |
| 215 |
passwd_changed = True |
| 216 |
if not module.check_mode: |
| 217 |
res = blade.patch_admins( |
| 218 |
names=[module.params["name"]], |
| 219 |
admin=AdminPatch( |
| 220 |
password=module.params["password"], |
| 221 |
old_password=module.params["old_password"], |
| 222 |
), |
| 223 |
) |
| 224 |
if res.status_code != 200: |
| 225 |
module.fail_json( |
| 226 |
msg="Local User {0} password reset failed. Error: {1}" |
| 227 |
"Check old password.".format( |
| 228 |
module.params["name"], res.errors[0].message |
| 229 |
) |
| 230 |
) |
| 231 |
if module.params["api"]: |
| 232 |
api_changed = True |
| 233 |
ttl = convert_time_to_millisecs(module.params["timeout"]) |
| 234 |
res = blade.delete_admins_api_tokens(admin_names=[module.params["name"]]) |
| 235 |
if res.status_code != 200: |
| 236 |
module.fail_json( |
| 237 |
msg="Failed to delete original API token. Error: {0}".format( |
| 238 |
res.errors[0].message |
| 239 |
) |
| 240 |
) |
| 241 |
res = blade.post_admins_api_tokens( |
| 242 |
admin_names=[module.params["name"]], timeout=ttl |
| 243 |
) |
| 244 |
if res.status_code != 200: |
| 245 |
module.fail_json( |
| 246 |
msg="Failed to recreate API token. Error: {0}".format( |
| 247 |
res.errors[0].message |
| 248 |
) |
| 249 |
) |
| 250 |
api_token = list(res.items)[0].api_token.token |
| 251 |
if ( |
| 252 |
module.params["role"] |
| 253 |
and module.params["role"] != getattr(user.role, "name", None) |
| 254 |
and user.is_local |
| 255 |
): |
| 256 |
if module.params["name"] != "pureuser": |
| 257 |
role_changed = True |
| 258 |
if not module.check_mode: |
| 259 |
res = blade.patch_admins( |
| 260 |
names=[module.params["name"]], |
| 261 |
admin=AdminPatch(role=AdminRole(name=module.params["role"])), |
| 262 |
) |
| 263 |
if res.status_code != 200: |
| 264 |
module.fail_json( |
| 265 |
msg="Local User {0} role changed failed. Error: {1}".format( |
| 266 |
module.params["name"], res.errors[0].message |
| 267 |
) |
| 268 |
) |
| 269 |
else: |
| 270 |
module.warn("Role for 'pureuser' cannot be modified.") |
| 271 |
if module.params["public_key"] is not None and module.params[ |
| 272 |
"public_key" |
| 273 |
] != getattr(user, "public_key", ""): |
| 274 |
key_changed = True |
| 275 |
res = blade.patch_admins( |
| 276 |
names=[module.params["name"]], |
| 277 |
admin=AdminPatch(public_key=module.params["public_key"]), |
| 278 |
) |
| 279 |
if res.status_code != 200: |
| 280 |
module.fail_json( |
| 281 |
msg="Failed to change SSH key. Error: {0}".format( |
| 282 |
res.errors[0].message |
| 283 |
) |
| 284 |
) |
| 285 |
changed = bool(passwd_changed or role_changed or api_changed or key_changed) |
| 286 |
module.exit_json(changed=changed, user_info=api_token) |
| 287 |
|
| 288 |
|
| 289 |
def update_ad_user(module, blade, user): |
| 290 |
"""Update AD user API token and/or SSH key""" |
| 291 |
api_token = "No API token created" |
| 292 |
api_changed = ssh_changed = False |
| 293 |
if module.params["api"]: |
| 294 |
if user: |
| 295 |
api_changed = True |
| 296 |
ttl = convert_time_to_millisecs(module.params["timeout"]) |
| 297 |
if getattr(user.api_token, "token"): |
| 298 |
res = blade.delete_admins_api_tokens( |
| 299 |
admin_names=[module.params["name"]] |
| 300 |
) |
| 301 |
if res.status_code != 200: |
| 302 |
module.fail_json( |
| 303 |
msg="Failed to delete original API token. Error: {0}".format( |
| 304 |
res.errors[0].message |
| 305 |
) |
| 306 |
) |
| 307 |
res = blade.post_admins_api_tokens( |
| 308 |
admin_names=[module.params["name"]], timeout=ttl |
| 309 |
) |
| 310 |
if res.status_code != 200: |
| 311 |
module.fail_json( |
| 312 |
msg="Failed to recreate API token. Error: {0}".format( |
| 313 |
res.errors[0].message |
| 314 |
) |
| 315 |
) |
| 316 |
api_token = list(res.items)[0].api_token.token |
| 317 |
else: |
| 318 |
api_changed = True |
| 319 |
ttl = convert_time_to_millisecs(module.params["timeout"]) |
| 320 |
res = blade.post_admins_api_tokens( |
| 321 |
admin_names=[module.params["name"]], timeout=ttl |
| 322 |
) |
| 323 |
if res.status_code != 200: |
| 324 |
module.fail_json( |
| 325 |
msg="Failed to create API token. Error: {0}".format( |
| 326 |
res.errors[0].message |
| 327 |
) |
| 328 |
) |
| 329 |
api_token = list(res.items)[0].api_token.token |
| 330 |
if module.params["public_key"]: |
| 331 |
ssh_changed = True |
| 332 |
res = blade.patch_admins( |
| 333 |
names=[module.params["name"]], |
| 334 |
admin=AdminPatch(public_key=module.params["public_key"]), |
| 335 |
) |
| 336 |
if res.status_code != 200: |
| 337 |
module.fail_json( |
| 338 |
msg="Failed to add SSH key. Error: {0}".format(res.errors[0].message) |
| 339 |
) |
| 340 |
changed = bool(api_changed or ssh_changed) |
| 341 |
module.exit_json(changed=changed, user_info=api_token) |
| 342 |
|
| 343 |
|
| 344 |
def delete_ad_user(module, blade, user): |
| 345 |
"""Delete AD User Account references""" |
| 346 |
changed = False |
| 347 |
if not module.check_mode: |
| 348 |
if user: |
| 349 |
changed = True |
| 350 |
res = blade.delete_admins_api_tokens(admin_names=[module.params["name"]]) |
| 351 |
if res.status_code != 200: |
| 352 |
module.fail_json( |
| 353 |
msg="AD Account {0} API token deletion failed. Error: {1}".format( |
| 354 |
module.params["name"], res.errors[0].message |
| 355 |
) |
| 356 |
) |
| 357 |
if hasattr(user, "public_key"): |
| 358 |
res = blade.patch_admins( |
| 359 |
names=[module.params["name"]], |
| 360 |
admin=AdminPatch(public_key=""), |
| 361 |
) |
| 362 |
if res.status_code != 200: |
| 363 |
module.fail_json( |
| 364 |
msg="AD Account {0} public key deletion failed. Error: {1}".format( |
| 365 |
module.params["name"], res.errors[0].message |
| 366 |
) |
| 367 |
) |
| 368 |
res = blade.delete_admins_cache(names=[module.params["name"]]) |
| 369 |
if res.status_code != 200: |
| 370 |
module.fail_json( |
| 371 |
msg="Admin Cache deleteion failed for AD user {0}. Error: {1}".format( |
| 372 |
module.params["name"], res.errors[0].message |
| 373 |
) |
| 374 |
) |
| 375 |
module.exit_json(changed=changed) |
| 376 |
|
| 377 |
|
| 378 |
def delete_local_user(module, blade): |
| 379 |
"""Delete Local User Account""" |
| 380 |
changed = True |
| 381 |
if not module.check_mode: |
| 382 |
res = blade.delete_admins(names=[module.params["name"]]) |
| 383 |
if res.status_code != 200: |
| 384 |
module.fail_json( |
| 385 |
msg="User Account {0} deletion failed. Error: {1}".format( |
| 386 |
module.params["name"], res.errors[0].message |
| 387 |
) |
| 388 |
) |
| 389 |
module.exit_json(changed=changed) |
| 390 |
|
| 391 |
|
| 392 |
def main(): |
| 393 |
argument_spec = purefb_argument_spec() |
| 394 |
argument_spec.update( |
| 395 |
dict( |
| 396 |
name=dict(type="str", required=True), |
| 397 |
public_key=dict(type="str", no_log=True), |
| 398 |
password=dict(type="str", no_log=True), |
| 399 |
old_password=dict(type="str", no_log=True), |
| 400 |
clear_lock=dict(type="bool", default=False), |
| 401 |
role=dict( |
| 402 |
type="str", |
| 403 |
choices=["readonly", "ops_admin", "storage_admin", "array_admin"], |
| 404 |
default="readonly", |
| 405 |
), |
| 406 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 407 |
api=dict(type="bool", default=False), |
| 408 |
ad_user=dict(type="bool", default=False), |
| 409 |
timeout=dict(type="str", default="0"), |
| 410 |
) |
| 411 |
) |
| 412 |
|
| 413 |
module = AnsibleModule( |
| 414 |
argument_spec, |
| 415 |
supports_check_mode=True, |
| 416 |
) |
| 417 |
|
| 418 |
blade = get_system(module) |
| 419 |
api_version = list(blade.get_versions().items) |
| 420 |
if MIN_LOCAL_USER not in api_version: |
| 421 |
module.fail_json( |
| 422 |
msg="Purity//FB must be upgraded to support managing local users." |
| 423 |
) |
| 424 |
|
| 425 |
state = module.params["state"] |
| 426 |
pattern = re.compile("^[a-z0-9]([a-z0-9-]{0,30}[a-z0-9])?$") |
| 427 |
user = get_user(module, blade) |
| 428 |
local_user = getattr(user, "is_local", False) |
| 429 |
if not pattern.match(module.params["name"]): |
| 430 |
module.fail_json( |
| 431 |
msg="name must contain a minimum of 1 and a maximum of 32 characters " |
| 432 |
"(alphanumeric or `-`). All letters must be lowercase." |
| 433 |
) |
| 434 |
|
| 435 |
if state == "present" and not local_user and module.params["ad_user"]: |
| 436 |
update_ad_user(module, blade, user) |
| 437 |
if state == "absent" and local_user: |
| 438 |
delete_local_user(module, blade) |
| 439 |
if state == "absent" and not local_user: |
| 440 |
delete_ad_user(module, blade, user) |
| 441 |
elif state == "present" and not module.params["ad_user"]: |
| 442 |
create_local_user(module, blade, user) |
| 443 |
else: |
| 444 |
module.exit_json(changed=False) |
| 445 |
|
| 446 |
|
| 447 |
if __name__ == "__main__": |
| 448 |
main() |