| 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: purefa_workload |
| 20 |
version_added: '1.33.0' |
| 21 |
short_description: Manage Fusion Fleet Workloads |
| 22 |
description: |
| 23 |
- Apply/Rename/Delete Fusion fleet workloads |
| 24 |
author: |
| 25 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 26 |
options: |
| 27 |
context: |
| 28 |
description: |
| 29 |
- Name of fleet member on which to perform the workload operation. |
| 30 |
- This requires the array receiving the request is a member of a fleet |
| 31 |
and the context name to be a member of the same fleet. |
| 32 |
type: str |
| 33 |
default: "" |
| 34 |
host: |
| 35 |
type: str |
| 36 |
description: |
| 37 |
- Host to connect to the workload after provisioning |
| 38 |
default: "" |
| 39 |
name: |
| 40 |
description: |
| 41 |
- Name of the workload. |
| 42 |
type: str |
| 43 |
required: true |
| 44 |
state: |
| 45 |
description: |
| 46 |
- Define whether to create or delete a fleet workload. |
| 47 |
- Using the expand option will add volume(s) to the workload. |
| 48 |
- If absent is specified together with a host, rather than deleting the workload, the host will be disconnected from the workload |
| 49 |
default: present |
| 50 |
choices: [ absent, present, expand] |
| 51 |
type: str |
| 52 |
preset: |
| 53 |
description: |
| 54 |
- name of existing preset to use as the basis of the workload |
| 55 |
type: str |
| 56 |
rename: |
| 57 |
description: |
| 58 |
- new name for a workload |
| 59 |
type: str |
| 60 |
eradicate: |
| 61 |
description: |
| 62 |
- whether to eradicate a workload |
| 63 |
type: bool |
| 64 |
default: false |
| 65 |
placement: |
| 66 |
description: |
| 67 |
- name of target on which the workload will be deployed |
| 68 |
type: str |
| 69 |
recommendation: |
| 70 |
description: |
| 71 |
- whether to use the Fusion placement recommendation based |
| 72 |
on the workload preset definitions. |
| 73 |
- This will use the first recommended placement if more than |
| 74 |
one is available |
| 75 |
default: false |
| 76 |
type: bool |
| 77 |
volume_count: |
| 78 |
description: |
| 79 |
- Number of additional volumes to add to an existing workload |
| 80 |
type: int |
| 81 |
volume_configuration: |
| 82 |
description: |
| 83 |
- Name of the volume configuration to use for adding volumes |
| 84 |
to a workload |
| 85 |
type: str |
| 86 |
extends_documentation_fragment: |
| 87 |
- purestorage.flasharray.purestorage.fa |
| 88 |
""" |
| 89 |
|
| 90 |
EXAMPLES = r""" |
| 91 |
- name: Create a workload using an existing preset on a specific placement target and connect to host myhost |
| 92 |
purestorage.flasharray.purefa_workload: |
| 93 |
name: foo |
| 94 |
preset: bar |
| 95 |
host: myhost |
| 96 |
placement: arrayB |
| 97 |
fa_url: 10.10.10.2 |
| 98 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 99 |
|
| 100 |
- name: Create a workload using an existing preset using the recommended target and connect to host myhost |
| 101 |
purestorage.flasharray.purefa_workload: |
| 102 |
name: foo |
| 103 |
preset: bar |
| 104 |
host: myhost |
| 105 |
recommendation: true |
| 106 |
fa_url: 10.10.10.2 |
| 107 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 108 |
|
| 109 |
- name: Add volumes to workload foo based on volume configuration fin and connect to host myhost |
| 110 |
purestorage.flasharray.purefa_workload: |
| 111 |
name: foo |
| 112 |
preset: bar |
| 113 |
volume_configuration: fin |
| 114 |
volume_count: 3 |
| 115 |
host: myhost |
| 116 |
state: expand |
| 117 |
fa_url: 10.10.10.2 |
| 118 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 119 |
|
| 120 |
- name: Rename an existing workload |
| 121 |
purestorage.flasharray.purefa_workload: |
| 122 |
name: foo |
| 123 |
rename: bar |
| 124 |
state: rename |
| 125 |
fa_url: 10.10.10.2 |
| 126 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 127 |
|
| 128 |
- name: Disconnect an existing workload from host |
| 129 |
purestorage.flasharray.purefa_workload: |
| 130 |
name: foo |
| 131 |
host: myhost |
| 132 |
state: absent |
| 133 |
fa_url: 10.10.10.2 |
| 134 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 135 |
|
| 136 |
- name: Delete an existing workload |
| 137 |
purestorage.flasharray.purefa_workload: |
| 138 |
name: foo |
| 139 |
state: absent |
| 140 |
fa_url: 10.10.10.2 |
| 141 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 142 |
|
| 143 |
- name: Eradicate an existing workload |
| 144 |
purestorage.flasharray.purefa_workload: |
| 145 |
name: foo |
| 146 |
state: absent |
| 147 |
eradicate: true |
| 148 |
fa_url: 10.10.10.2 |
| 149 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 150 |
|
| 151 |
- name: Recover a deleted workload |
| 152 |
purestorage.flasharray.purefa_workload: |
| 153 |
name: foo |
| 154 |
state: present |
| 155 |
fa_url: 10.10.10.2 |
| 156 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 157 |
|
| 158 |
- name: Reconnect an existing workload to a host |
| 159 |
purestorage.flasharray.purefa_workload: |
| 160 |
name: foo |
| 161 |
host: myhost |
| 162 |
fa_url: 10.10.10.2 |
| 163 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 164 |
""" |
| 165 |
|
| 166 |
RETURN = r""" |
| 167 |
""" |
| 168 |
|
| 169 |
HAS_PURESTORAGE = True |
| 170 |
try: |
| 171 |
from pypureclient.flasharray import ( |
| 172 |
WorkloadConfigurationReference, |
| 173 |
WorkloadPatch, |
| 174 |
WorkloadPost, |
| 175 |
WorkloadPlacementRecommendation, |
| 176 |
VolumePost, |
| 177 |
ConnectionPost, |
| 178 |
) |
| 179 |
except ImportError: |
| 180 |
HAS_PURESTORAGE = False |
| 181 |
|
| 182 |
import time |
| 183 |
from ansible.module_utils.basic import AnsibleModule |
| 184 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import ( |
| 185 |
get_array, |
| 186 |
purefa_argument_spec, |
| 187 |
) |
| 188 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.version import ( |
| 189 |
LooseVersion, |
| 190 |
) |
| 191 |
|
| 192 |
VERSION = 1.5 |
| 193 |
USER_AGENT_BASE = "Ansible" |
| 194 |
MIN_REQUIRED_API_VERSION = "2.40" |
| 195 |
|
| 196 |
|
| 197 |
def _create_volume(module, array): |
| 198 |
"""Create an actual volume in a workload""" |
| 199 |
res = array.post_volumes( |
| 200 |
volume=VolumePost( |
| 201 |
workload=WorkloadConfigurationReference( |
| 202 |
name=module.params["name"], |
| 203 |
configuration=module.params["volume_configuration"], |
| 204 |
), |
| 205 |
), |
| 206 |
context_names=[module.params["context"]], |
| 207 |
) |
| 208 |
if res.status_code != 200: |
| 209 |
module.fail_json( |
| 210 |
msg="Workload volume creation failed. Error: {0}".format( |
| 211 |
res.errors[0].message |
| 212 |
) |
| 213 |
) |
| 214 |
|
| 215 |
|
| 216 |
def _disconnect_volumes(module, array): |
| 217 |
"""Disconnect host from volumes in the workload""" |
| 218 |
volumes = list( |
| 219 |
array.get_volumes( |
| 220 |
filter="workload.name='{0}'".format(module.params["name"]), |
| 221 |
context_names=[module.params["context"]], |
| 222 |
).items |
| 223 |
) |
| 224 |
volNames = [vol.name for vol in volumes] |
| 225 |
|
| 226 |
res = array.delete_connections( |
| 227 |
host_names=[module.params["host"]], |
| 228 |
context_names=[module.params["context"]], |
| 229 |
volume_names=volNames, |
| 230 |
) |
| 231 |
if res.status_code != 200: |
| 232 |
module.fail_json( |
| 233 |
msg="Failed to disconnect volumes from host. Error: {0}:{1}".format( |
| 234 |
res.errors[0].message, res.errors[0].context |
| 235 |
) |
| 236 |
) |
| 237 |
|
| 238 |
|
| 239 |
def _connect_volumes(module, array): |
| 240 |
"""Connect host to volumes in the workload""" |
| 241 |
volumes = list( |
| 242 |
array.get_volumes( |
| 243 |
filter="workload.name='{0}'".format(module.params["name"]), |
| 244 |
context_names=[module.params["context"]], |
| 245 |
).items |
| 246 |
) |
| 247 |
volNames = [vol.name for vol in volumes] |
| 248 |
|
| 249 |
res = array.post_connections( |
| 250 |
host_names=[module.params["host"]], |
| 251 |
context_names=[module.params["context"]], |
| 252 |
volume_names=volNames, |
| 253 |
connection=ConnectionPost(), |
| 254 |
) |
| 255 |
if res.status_code != 200: |
| 256 |
module.fail_json( |
| 257 |
msg="Failed to connect volumes to host. Error: {0}:{1}".format( |
| 258 |
res.errors[0].message, res.errors[0].context |
| 259 |
) |
| 260 |
) |
| 261 |
|
| 262 |
|
| 263 |
def create_workload(module, array, fleet, preset_config): |
| 264 |
"""Create fleet workload using existing preset""" |
| 265 |
changed = True |
| 266 |
parameters = preset_config.parameters |
| 267 |
repl_config = preset_config.periodic_replication_configurations |
| 268 |
placement_config = preset_config.placement_configurations |
| 269 |
qos_config = preset_config.qos_configurations |
| 270 |
snap_config = preset_config.snapshot_configurations |
| 271 |
vol_config = preset_config.volume_configurations |
| 272 |
tags = preset_config.workload_tags |
| 273 |
if module.params["recommendation"]: |
| 274 |
|
| 275 |
res = array.post_workloads_placement_recommendations( |
| 276 |
inputs=WorkloadPlacementRecommendation(), |
| 277 |
preset_names=[module.params["preset"]], |
| 278 |
context_names=[module.params["context"]], |
| 279 |
) |
| 280 |
if res.status_code != 200: |
| 281 |
module.fail_json( |
| 282 |
msg="Recommendation calculation failure. Error: {0}".format( |
| 283 |
res.errors[0].message |
| 284 |
) |
| 285 |
) |
| 286 |
workload_calc = list(res.items)[0].name |
| 287 |
|
| 288 |
result = list( |
| 289 |
array.get_workloads_placement_recommendations( |
| 290 |
names=[workload_calc], context_names=[module.params["context"]] |
| 291 |
).items |
| 292 |
)[0] |
| 293 |
while result.status != "completed": |
| 294 |
time.sleep(1) |
| 295 |
result = list( |
| 296 |
array.get_workloads_placement_recommendations( |
| 297 |
names=[workload_calc], context_names=[module.params["context"]] |
| 298 |
).items |
| 299 |
)[0] |
| 300 |
|
| 301 |
module.params["placement"] = result.results[0].placements[0].targets[0].name |
| 302 |
module.params["context"] = module.params["placement"] |
| 303 |
if not module.check_mode: |
| 304 |
res = array.post_workloads( |
| 305 |
names=[module.params["name"]], |
| 306 |
preset_names=[module.params["preset"]], |
| 307 |
workload=WorkloadPost(), |
| 308 |
context_names=[module.params["context"]], |
| 309 |
) |
| 310 |
if res.status_code != 200: |
| 311 |
module.fail_json( |
| 312 |
msg="Failed to create workload {0}. Error: {1}:{2}".format( |
| 313 |
module.params["name"], res.errors[0].message, res.errors[0].context |
| 314 |
) |
| 315 |
) |
| 316 |
if module.params["host"] != "": |
| 317 |
_connect_volumes(module, array) |
| 318 |
|
| 319 |
module.exit_json(changed=changed) |
| 320 |
|
| 321 |
|
| 322 |
def expand_workload(module, array, fleet, volume_configs): |
| 323 |
"""Add new volumes to workload""" |
| 324 |
changed = False |
| 325 |
for vol_config in volume_configs: |
| 326 |
if vol_config.name == module.params["volume_configuration"]: |
| 327 |
for x in range(module.params["volume_count"]): |
| 328 |
changed = True |
| 329 |
_create_volume(module, array) |
| 330 |
if changed: |
| 331 |
if module.params["host"] != "": |
| 332 |
_connect_volumes(module, array) |
| 333 |
else: |
| 334 |
module.fail_json( |
| 335 |
msg="Volume Configuration {0} does not exist for preset {1}.".format( |
| 336 |
module.params["volume_configuration"], module.params["preset"] |
| 337 |
) |
| 338 |
) |
| 339 |
|
| 340 |
module.exit_json(changed=changed) |
| 341 |
|
| 342 |
|
| 343 |
def delete_workload(module, array): |
| 344 |
"""Delete the workload""" |
| 345 |
changed = True |
| 346 |
if not module.check_mode: |
| 347 |
res = array.patch_workloads( |
| 348 |
names=[module.params["name"]], |
| 349 |
workload=WorkloadPatch(destroyed=True), |
| 350 |
context_names=[module.params["context"]], |
| 351 |
) |
| 352 |
if res.status_code != 200: |
| 353 |
module.fail_json( |
| 354 |
msg="Workload deletion failed. Error: {0}".format(res.errors[0].message) |
| 355 |
) |
| 356 |
if module.params["eradicate"]: |
| 357 |
eradicate_workload(module, array) |
| 358 |
module.exit_json(changed=changed) |
| 359 |
|
| 360 |
|
| 361 |
def eradicate_workload(module, array): |
| 362 |
"""Eradicate the workload""" |
| 363 |
changed = True |
| 364 |
if not module.check_mode: |
| 365 |
res = array.delete_workloads( |
| 366 |
names=[module.params["name"]], |
| 367 |
context_names=[module.params["context"]], |
| 368 |
) |
| 369 |
if res.status_code != 200: |
| 370 |
module.fail_json( |
| 371 |
msg="Workload eradication failed. Error: {0}".format( |
| 372 |
res.errors[0].message |
| 373 |
) |
| 374 |
) |
| 375 |
module.exit_json(changed=changed) |
| 376 |
|
| 377 |
|
| 378 |
def recover_workload(module, array): |
| 379 |
"""Recover the workload and optionally reconnect to host""" |
| 380 |
changed = True |
| 381 |
if not module.check_mode: |
| 382 |
res = array.patch_workloads( |
| 383 |
names=[module.params["name"]], |
| 384 |
workload=WorkloadPatch(destroyed=False), |
| 385 |
context_names=[module.params["context"]], |
| 386 |
) |
| 387 |
if res.status_code != 200: |
| 388 |
module.fail_json( |
| 389 |
msg="Workload recovery failed. Error: {0}".format(res.errors[0].message) |
| 390 |
) |
| 391 |
if module.params["host"] != "": |
| 392 |
_connect_volumes(module, array) |
| 393 |
|
| 394 |
module.exit_json(changed=changed) |
| 395 |
|
| 396 |
|
| 397 |
def rename_workload(module, array): |
| 398 |
"""Rename the workload""" |
| 399 |
changed = True |
| 400 |
if not module.check_mode: |
| 401 |
res = array.patch_workloads( |
| 402 |
names=[module.params["name"]], |
| 403 |
workload=WorkloadPatch(name=module.params["rename"]), |
| 404 |
context_names=[module.params["context"]], |
| 405 |
) |
| 406 |
if res.status_code != 200: |
| 407 |
module.fail_json( |
| 408 |
msg="Workload rename failed. Error: {0}".format(res.errors[0].message) |
| 409 |
) |
| 410 |
module.exit_json(changed=changed) |
| 411 |
|
| 412 |
|
| 413 |
def connect_or_disconnect_volumes(module, array, mode): |
| 414 |
"""Connect or disconnect volumes in the workload to a host""" |
| 415 |
changed = False |
| 416 |
|
| 417 |
res = array.get_connections( |
| 418 |
host_names=[module.params["host"]], |
| 419 |
context_names=[module.params["context"]], |
| 420 |
) |
| 421 |
if res.status_code != 200: |
| 422 |
module.fail_json( |
| 423 |
msg="Failed to get volume connection for host {0}. Error: {1}".format( |
| 424 |
module.params["host"], res.errors[0].message |
| 425 |
) |
| 426 |
) |
| 427 |
volume_connections = [conn.volume.name for conn in list(res.items)] |
| 428 |
|
| 429 |
res = array.get_volumes( |
| 430 |
filter="workload.name='{0}'".format(module.params["name"]), |
| 431 |
context_names=[module.params["context"]], |
| 432 |
) |
| 433 |
if res.status_code != 200: |
| 434 |
module.fail_json( |
| 435 |
msg="Failed to get volumes for workload {0}. Error: {1}".format( |
| 436 |
module.params["name"], res.errors[0].message |
| 437 |
) |
| 438 |
) |
| 439 |
volumes = list(res.items) |
| 440 |
|
| 441 |
if mode == "connect": |
| 442 |
for volume in volumes: |
| 443 |
if volume.name not in volume_connections: |
| 444 |
changed = True |
| 445 |
elif mode == "disconnect": |
| 446 |
for volume in volumes: |
| 447 |
if volume.name in volume_connections: |
| 448 |
changed = True |
| 449 |
|
| 450 |
if not module.check_mode and changed: |
| 451 |
if mode == "connect": |
| 452 |
_connect_volumes(module, array) |
| 453 |
elif mode == "disconnect": |
| 454 |
_disconnect_volumes(module, array) |
| 455 |
|
| 456 |
module.exit_json(changed=changed) |
| 457 |
|
| 458 |
|
| 459 |
def main(): |
| 460 |
argument_spec = purefa_argument_spec() |
| 461 |
argument_spec.update( |
| 462 |
dict( |
| 463 |
name=dict(type="str", required=True), |
| 464 |
state=dict( |
| 465 |
type="str", |
| 466 |
default="present", |
| 467 |
choices=["absent", "present", "expand"], |
| 468 |
), |
| 469 |
preset=dict(type="str"), |
| 470 |
rename=dict(type="str"), |
| 471 |
eradicate=dict(type="bool", default=False), |
| 472 |
placement=dict(type="str"), |
| 473 |
volume_count=dict(type="int"), |
| 474 |
volume_configuration=dict(type="str"), |
| 475 |
recommendation=dict(type="bool", default=False), |
| 476 |
context=dict(type="str", default=""), |
| 477 |
host=dict(type="str", default=""), |
| 478 |
) |
| 479 |
) |
| 480 |
|
| 481 |
required_if = [["state", "expand", ["volume_count", "volume_configuration"]]] |
| 482 |
|
| 483 |
module = AnsibleModule( |
| 484 |
argument_spec, supports_check_mode=True, required_if=required_if |
| 485 |
) |
| 486 |
|
| 487 |
if not HAS_PURESTORAGE: |
| 488 |
module.fail_json(msg="py-pure-client sdk is required for this module") |
| 489 |
|
| 490 |
array = get_array(module) |
| 491 |
api_version = array.get_rest_version() |
| 492 |
if LooseVersion(MIN_REQUIRED_API_VERSION) > LooseVersion(api_version): |
| 493 |
module.fail_json( |
| 494 |
msg="FlashArray REST version not supported. " |
| 495 |
"Minimum version required: {0}".format(MIN_REQUIRED_API_VERSION) |
| 496 |
) |
| 497 |
state = module.params["state"] |
| 498 |
if module.params["volume_count"] and module.params["volume_count"] <= 0: |
| 499 |
module.fail_json(msg="volume_count must be a positive integer.") |
| 500 |
fleet_res = array.get_fleets() |
| 501 |
if fleet_res.status_code != 200: |
| 502 |
module.fail_json( |
| 503 |
msg="Fusion is not enabled on this system " |
| 504 |
"or the array is not a member of a fleet." |
| 505 |
) |
| 506 |
fleet = list(fleet_res.items)[0].name |
| 507 |
|
| 508 |
workload_destroyed = False |
| 509 |
workload_exists = False |
| 510 |
preset_config = {} |
| 511 |
|
| 512 |
module.params["preset"] = fleet + ":" + module.params["preset"] |
| 513 |
res = array.get_workloads( |
| 514 |
names=[module.params["name"]], context_names=[module.params["context"]] |
| 515 |
) |
| 516 |
if res.status_code == 200: |
| 517 |
workload_exists = True |
| 518 |
workload_destroyed = list(res.items)[0].destroyed |
| 519 |
|
| 520 |
if (state == "present" and not workload_destroyed and not workload_exists) or ( |
| 521 |
state == "expand" and not workload_destroyed |
| 522 |
): |
| 523 |
res = array.get_presets_workload( |
| 524 |
names=[module.params["preset"]], |
| 525 |
) |
| 526 |
if res.status_code != 200: |
| 527 |
module.fail_json( |
| 528 |
msg="Preset {0} does not exist in fleet {1}".format( |
| 529 |
module.params["preset"], fleet |
| 530 |
) |
| 531 |
) |
| 532 |
preset_config = list(res.items)[0] |
| 533 |
if ( |
| 534 |
state == "present" |
| 535 |
and workload_exists |
| 536 |
and module.params["rename"] |
| 537 |
and not workload_destroyed |
| 538 |
): |
| 539 |
rename_workload(module, array) |
| 540 |
elif state == "present" and not workload_exists: |
| 541 |
create_workload(module, array, fleet, preset_config) |
| 542 |
elif state == "expand" and workload_exists and not workload_destroyed: |
| 543 |
expand_workload(module, array, fleet, preset_config.volume_configurations) |
| 544 |
elif state == "present" and workload_exists and workload_destroyed: |
| 545 |
recover_workload(module, array) |
| 546 |
elif ( |
| 547 |
state == "present" |
| 548 |
and workload_exists |
| 549 |
and not workload_destroyed |
| 550 |
and module.params["host"] != "" |
| 551 |
): |
| 552 |
connect_or_disconnect_volumes(module, array, "connect") |
| 553 |
elif ( |
| 554 |
state == "absent" |
| 555 |
and workload_exists |
| 556 |
and not workload_destroyed |
| 557 |
and module.params["host"] != "" |
| 558 |
): |
| 559 |
connect_or_disconnect_volumes(module, array, "disconnect") |
| 560 |
elif state == "absent" and workload_exists and not workload_destroyed: |
| 561 |
delete_workload(module, array) |
| 562 |
elif state == "absent" and workload_destroyed and module.params["eradicate"]: |
| 563 |
eradicate_workload(module, array) |
| 564 |
else: |
| 565 |
module.exit_json(changed=False) |
| 566 |
|
| 567 |
|
| 568 |
if __name__ == "__main__": |
| 569 |
main() |