| 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_fs |
| 20 |
version_added: '1.5.0' |
| 21 |
short_description: Manage FlashArray File Systems |
| 22 |
description: |
| 23 |
- Create/Delete FlashArray File Systems |
| 24 |
author: |
| 25 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 26 |
options: |
| 27 |
name: |
| 28 |
description: |
| 29 |
- Name of the file system |
| 30 |
type: str |
| 31 |
required: true |
| 32 |
state: |
| 33 |
description: |
| 34 |
- Define whether the file system should exist or not. |
| 35 |
default: present |
| 36 |
choices: [ absent, present ] |
| 37 |
type: str |
| 38 |
eradicate: |
| 39 |
description: |
| 40 |
- Define whether to eradicate the file system on delete or leave in trash. |
| 41 |
type: bool |
| 42 |
default: false |
| 43 |
rename: |
| 44 |
description: |
| 45 |
- Value to rename the specified file system to |
| 46 |
- Rename only applies to the container the current filesystem is in. |
| 47 |
- There is no requirement to specify the pod name as this is implied. |
| 48 |
type: str |
| 49 |
move: |
| 50 |
description: |
| 51 |
- Move a filesystem in and out of a pod |
| 52 |
- Provide the name of pod to move the filesystem to |
| 53 |
- Pod names must be unique in the array |
| 54 |
- To move to the local array, specify C(local) |
| 55 |
- This is not idempotent - use C(ignore_errors) in the play |
| 56 |
type: str |
| 57 |
version_added: '1.13.0' |
| 58 |
context: |
| 59 |
description: |
| 60 |
- Name of fleet member on which to perform the operation. |
| 61 |
- This requires the array receiving the request is a member of a fleet |
| 62 |
and the context name to be a member of the same fleet. |
| 63 |
type: str |
| 64 |
default: "" |
| 65 |
version_added: '1.39.0' |
| 66 |
extends_documentation_fragment: |
| 67 |
- purestorage.flasharray.purestorage.fa |
| 68 |
""" |
| 69 |
|
| 70 |
EXAMPLES = r""" |
| 71 |
- name: Create file system foo |
| 72 |
purestorage.flasharray.purefa_fs: |
| 73 |
name: foo |
| 74 |
fa_url: 10.10.10.2 |
| 75 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 76 |
|
| 77 |
- name: Delete and eradicate file system foo |
| 78 |
purestorage.flasharray.purefa_fs: |
| 79 |
name: foo |
| 80 |
eradicate: true |
| 81 |
state: absent |
| 82 |
fa_url: 10.10.10.2 |
| 83 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 84 |
|
| 85 |
- name: Rename file system foo to bar |
| 86 |
purestorage.flasharray.purefa_fs: |
| 87 |
name: foo |
| 88 |
rename: bar |
| 89 |
fa_url: 10.10.10.2 |
| 90 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 91 |
""" |
| 92 |
|
| 93 |
RETURN = r""" |
| 94 |
""" |
| 95 |
|
| 96 |
HAS_PURESTORAGE = True |
| 97 |
try: |
| 98 |
from pypureclient import flasharray |
| 99 |
except ImportError: |
| 100 |
HAS_PURESTORAGE = False |
| 101 |
|
| 102 |
from ansible.module_utils.basic import AnsibleModule |
| 103 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import ( |
| 104 |
get_array, |
| 105 |
purefa_argument_spec, |
| 106 |
) |
| 107 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.version import ( |
| 108 |
LooseVersion, |
| 109 |
) |
| 110 |
|
| 111 |
MIN_REQUIRED_API_VERSION = "2.2" |
| 112 |
REPL_SUPPORT_API = "2.13" |
| 113 |
CONTEXT_VERSION = "2.38" |
| 114 |
|
| 115 |
|
| 116 |
def delete_fs(module, array): |
| 117 |
"""Delete a file system""" |
| 118 |
changed = True |
| 119 |
api_version = array.get_rest_version() |
| 120 |
if not module.check_mode: |
| 121 |
file_system = flasharray.FileSystemPatch(destroyed=True) |
| 122 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 123 |
res = array.patch_file_systems( |
| 124 |
names=[module.params["name"]], |
| 125 |
file_system=file_system, |
| 126 |
context_names=[module.params["context"]], |
| 127 |
) |
| 128 |
else: |
| 129 |
res = array.patch_file_systems( |
| 130 |
names=[module.params["name"]], file_system=file_system |
| 131 |
) |
| 132 |
if res.status_code != 200: |
| 133 |
module.fail_json( |
| 134 |
msg="Failed to delete file system {0}. Error: {1}".format( |
| 135 |
module.params["name"], res.errors[0].message |
| 136 |
) |
| 137 |
) |
| 138 |
if module.params["eradicate"]: |
| 139 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 140 |
res = array.delete_file_systems( |
| 141 |
names=[module.params["name"]], |
| 142 |
context_names=[module.params["context"]], |
| 143 |
) |
| 144 |
else: |
| 145 |
res = array.delete_file_systems(names=[module.params["name"]]) |
| 146 |
if res.status_code != 200: |
| 147 |
module.fail_json( |
| 148 |
msg="Eradication of file system {0} failed. Error: {1}".format( |
| 149 |
module.params["name"], res.errors[0].message |
| 150 |
) |
| 151 |
) |
| 152 |
module.exit_json(changed=changed) |
| 153 |
|
| 154 |
|
| 155 |
def recover_fs(module, array): |
| 156 |
"""Recover a deleted file system""" |
| 157 |
changed = True |
| 158 |
api_version = array.get_rest_version() |
| 159 |
if not module.check_mode: |
| 160 |
file_system = flasharray.FileSystemPatch(destroyed=False) |
| 161 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 162 |
res = array.patch_file_systems( |
| 163 |
names=[module.params["name"]], |
| 164 |
file_system=file_system, |
| 165 |
context_names=[module.params["context"]], |
| 166 |
) |
| 167 |
else: |
| 168 |
res = array.patch_file_systems( |
| 169 |
names=[module.params["name"]], file_system=file_system |
| 170 |
) |
| 171 |
if res.status_code != 200: |
| 172 |
module.fail_json( |
| 173 |
msg="Failed to recover file system {0}. Error: {1}".format( |
| 174 |
module.params["name"], res.errors[0].message |
| 175 |
) |
| 176 |
) |
| 177 |
module.exit_json(changed=changed) |
| 178 |
|
| 179 |
|
| 180 |
def eradicate_fs(module, array): |
| 181 |
"""Eradicate a file system""" |
| 182 |
changed = True |
| 183 |
api_version = array.get_rest_version() |
| 184 |
if not module.check_mode: |
| 185 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 186 |
res = array.delete_file_systems( |
| 187 |
names=[module.params["name"]], context_names=[module.params["context"]] |
| 188 |
) |
| 189 |
else: |
| 190 |
res = array.delete_file_systems(names=[module.params["name"]]) |
| 191 |
if res.status_code != 200: |
| 192 |
module.fail_json( |
| 193 |
msg="Failed to eradicate file system {0}. Error: {1}".format( |
| 194 |
module.params["name"], res.errors[0].message |
| 195 |
) |
| 196 |
) |
| 197 |
module.exit_json(changed=changed) |
| 198 |
|
| 199 |
|
| 200 |
def rename_fs(module, array): |
| 201 |
"""Rename a file system""" |
| 202 |
changed = False |
| 203 |
target = None |
| 204 |
api_version = array.get_rest_version() |
| 205 |
target_name = module.params["rename"] |
| 206 |
if "::" in module.params["name"]: |
| 207 |
pod_name = module.params["name"].split("::")[0] |
| 208 |
target_name = pod_name + "::" + module.params["rename"] |
| 209 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 210 |
res = array.get_file_systems( |
| 211 |
names=[target_name], context_names=[module.params["context"]] |
| 212 |
) |
| 213 |
else: |
| 214 |
res = array.get_file_systems(names=[target_name]) |
| 215 |
if res.status_code == 200: |
| 216 |
target = list(res.items)[0] |
| 217 |
if not target: |
| 218 |
changed = True |
| 219 |
if not module.check_mode: |
| 220 |
file_system = flasharray.FileSystemPatch(name=target_name) |
| 221 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 222 |
res = array.patch_file_systems( |
| 223 |
names=[module.params["name"]], |
| 224 |
file_system=file_system, |
| 225 |
context_names=[module.params["context"]], |
| 226 |
) |
| 227 |
else: |
| 228 |
res = array.patch_file_systems( |
| 229 |
names=[module.params["name"]], file_system=file_system |
| 230 |
) |
| 231 |
if res.status_code != 200: |
| 232 |
module.fail_json( |
| 233 |
msg="Failed to rename file system {0}. Error: {1}".format( |
| 234 |
module.params["name"], res.errors[0].message |
| 235 |
) |
| 236 |
) |
| 237 |
else: |
| 238 |
module.fail_json( |
| 239 |
msg="Target file system {0} already exists".format(module.params["rename"]) |
| 240 |
) |
| 241 |
module.exit_json(changed=changed) |
| 242 |
|
| 243 |
|
| 244 |
def create_fs(module, array): |
| 245 |
"""Create a file system""" |
| 246 |
changed = True |
| 247 |
api_version = array.get_rest_version() |
| 248 |
if "::" in module.params["name"]: |
| 249 |
pod_name = module.params["name"].split("::")[0] |
| 250 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 251 |
res = array.get_pods( |
| 252 |
names=[pod_name], context_names=[module.params["context"]] |
| 253 |
) |
| 254 |
else: |
| 255 |
res = array.get_pods(names=[pod_name]) |
| 256 |
if res.status_code == 200: |
| 257 |
pod = list(array.get_pods(names=[pod_name]).items)[0] |
| 258 |
else: |
| 259 |
module.fail_json( |
| 260 |
msg="Failed to create filesystem. Pod {0} does not exist".format( |
| 261 |
pod_name |
| 262 |
) |
| 263 |
) |
| 264 |
if pod.promotion_status == "demoted": |
| 265 |
module.fail_json(msg="Filesystem cannot be created in a demoted pod") |
| 266 |
if not module.check_mode: |
| 267 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 268 |
res = array.post_file_systems( |
| 269 |
names=[module.params["name"]], context_names=[module.params["context"]] |
| 270 |
) |
| 271 |
else: |
| 272 |
res = array.post_file_systems(names=[module.params["name"]]) |
| 273 |
if res.status_code != 200: |
| 274 |
module.fail_json( |
| 275 |
msg="Failed to create file system {0}. Error: {1}".format( |
| 276 |
module.params["name"], res.errors[0].message |
| 277 |
) |
| 278 |
) |
| 279 |
module.exit_json(changed=changed) |
| 280 |
|
| 281 |
|
| 282 |
def move_fs(module, array): |
| 283 |
"""Move filesystem between pods or local array""" |
| 284 |
changed = False |
| 285 |
api_version = array.get_rest_version() |
| 286 |
pod_name = "" |
| 287 |
fs_name = module.params["name"] |
| 288 |
if "::" in module.params["name"]: |
| 289 |
fs_name = module.params["name"].split("::")[1] |
| 290 |
pod_name = module.params["name"].split("::")[0] |
| 291 |
if module.params["move"] == "local": |
| 292 |
target_location = "" |
| 293 |
if "::" not in module.params["name"]: |
| 294 |
module.fail_json(msg="Source and destination [local] cannot be the same.") |
| 295 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 296 |
res = array.get_file_systems( |
| 297 |
names=[fs_name], context_names=[module.params["context"]] |
| 298 |
) |
| 299 |
else: |
| 300 |
res = array.get_file_systems(names=[fs_name]) |
| 301 |
if res.status_code == 200: |
| 302 |
module.fail_json(msg="Target filesystem {0} already exists".format(fs_name)) |
| 303 |
else: |
| 304 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 305 |
res = array.get_pods( |
| 306 |
names=[module.params["move"]], context_names=[module.params["context"]] |
| 307 |
) |
| 308 |
else: |
| 309 |
res = array.get_pods(names=[module.params["move"]]) |
| 310 |
if res.status_code == 200: |
| 311 |
pod = list(res.items)[0] |
| 312 |
if len(pod.arrays) > 1: |
| 313 |
module.fail_json(msg="Filesystem cannot be moved into a stretched pod") |
| 314 |
if pod.link_target_count != 0: |
| 315 |
module.fail_json( |
| 316 |
msg="Filesystem cannot be moved into a linked source pod" |
| 317 |
) |
| 318 |
if pod.promotion_status == "demoted": |
| 319 |
module.fail_json(msg="Volume cannot be moved into a demoted pod") |
| 320 |
else: |
| 321 |
module.fail_json( |
| 322 |
msg="Failed to move filesystem. Pod {0} does not exist".format(pod_name) |
| 323 |
) |
| 324 |
if "::" in module.params["name"]: |
| 325 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 326 |
res = array.get_pods( |
| 327 |
names=[module.params["move"]], |
| 328 |
context_names=[module.params["context"]], |
| 329 |
) |
| 330 |
else: |
| 331 |
res = array.get_pods(names=[module.params["move"]]) |
| 332 |
if res.status_code == 200: |
| 333 |
pod = list(res.items)[0] |
| 334 |
else: |
| 335 |
module.fail_json( |
| 336 |
msg="Failed to move filesystem. Pod {0} does not exist".format( |
| 337 |
pod_name |
| 338 |
) |
| 339 |
) |
| 340 |
if len(pod.arrays) > 1: |
| 341 |
module.fail_json( |
| 342 |
msg="Filesystem cannot be moved out of a stretched pod" |
| 343 |
) |
| 344 |
if pod.linked_target_count != 0: |
| 345 |
module.fail_json( |
| 346 |
msg="Filesystem cannot be moved out of a linked source pod" |
| 347 |
) |
| 348 |
if pod.promotion_status == "demoted": |
| 349 |
module.fail_json(msg="Volume cannot be moved out of a demoted pod") |
| 350 |
target_location = module.params["move"] |
| 351 |
changed = True |
| 352 |
if not module.check_mode: |
| 353 |
file_system = flasharray.FileSystemPatch( |
| 354 |
pod=flasharray.Reference(name=target_location) |
| 355 |
) |
| 356 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 357 |
move_res = array.patch_file_systems( |
| 358 |
names=[module.params["name"]], |
| 359 |
file_system=file_system, |
| 360 |
context_names=[module.params["context"]], |
| 361 |
) |
| 362 |
else: |
| 363 |
move_res = array.patch_file_systems( |
| 364 |
names=[module.params["name"]], file_system=file_system |
| 365 |
) |
| 366 |
if move_res.status_code != 200: |
| 367 |
module.fail_json( |
| 368 |
msg="Move of filesystem {0} failed. Error: {1}".format( |
| 369 |
module.params["name"], move_res.errors[0].message |
| 370 |
) |
| 371 |
) |
| 372 |
module.exit_json(changed=changed) |
| 373 |
|
| 374 |
|
| 375 |
def main(): |
| 376 |
argument_spec = purefa_argument_spec() |
| 377 |
argument_spec.update( |
| 378 |
dict( |
| 379 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 380 |
eradicate=dict(type="bool", default=False), |
| 381 |
name=dict(type="str", required=True), |
| 382 |
move=dict(type="str"), |
| 383 |
rename=dict(type="str"), |
| 384 |
context=dict(type="str", default=""), |
| 385 |
) |
| 386 |
) |
| 387 |
|
| 388 |
mutually_exclusive = [["move", "rename"]] |
| 389 |
module = AnsibleModule( |
| 390 |
argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True |
| 391 |
) |
| 392 |
|
| 393 |
if not HAS_PURESTORAGE: |
| 394 |
module.fail_json(msg="py-pure-client sdk is required for this module") |
| 395 |
|
| 396 |
array = get_array(module) |
| 397 |
api_version = array.get_rest_version() |
| 398 |
|
| 399 |
if LooseVersion(MIN_REQUIRED_API_VERSION) > LooseVersion(api_version): |
| 400 |
module.fail_json( |
| 401 |
msg="FlashArray REST version not supported. " |
| 402 |
"Minimum version required: {0}".format(MIN_REQUIRED_API_VERSION) |
| 403 |
) |
| 404 |
if ( |
| 405 |
LooseVersion(REPL_SUPPORT_API) > LooseVersion(api_version) |
| 406 |
and "::" in module.params["name"] |
| 407 |
): |
| 408 |
module.fail_json( |
| 409 |
msg="Filesystem Replication is only supported in Purity//FA 6.3.0 or higher" |
| 410 |
) |
| 411 |
state = module.params["state"] |
| 412 |
exists = False |
| 413 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 414 |
res = array.get_file_systems( |
| 415 |
names=[module.params["name"]], context_names=[module.params["context"]] |
| 416 |
) |
| 417 |
else: |
| 418 |
res = array.get_file_systems(names=[module.params["name"]]) |
| 419 |
if res.status_code == 200: |
| 420 |
filesystem = list(res.items)[0] |
| 421 |
exists = True |
| 422 |
|
| 423 |
if state == "present" and not exists and not module.params["move"]: |
| 424 |
create_fs(module, array) |
| 425 |
elif ( |
| 426 |
state == "present" |
| 427 |
and exists |
| 428 |
and module.params["move"] |
| 429 |
and not filesystem.destroyed |
| 430 |
): |
| 431 |
move_fs(module, array) |
| 432 |
elif ( |
| 433 |
state == "present" |
| 434 |
and exists |
| 435 |
and module.params["rename"] |
| 436 |
and not filesystem.destroyed |
| 437 |
): |
| 438 |
rename_fs(module, array) |
| 439 |
elif ( |
| 440 |
state == "present" |
| 441 |
and exists |
| 442 |
and filesystem.destroyed |
| 443 |
and not module.params["rename"] |
| 444 |
and not module.params["move"] |
| 445 |
): |
| 446 |
recover_fs(module, array) |
| 447 |
elif ( |
| 448 |
state == "present" and exists and filesystem.destroyed and module.params["move"] |
| 449 |
): |
| 450 |
module.fail_json( |
| 451 |
msg="Filesystem {0} exists, but in destroyed state".format( |
| 452 |
module.params["name"] |
| 453 |
) |
| 454 |
) |
| 455 |
elif state == "absent" and exists and not filesystem.destroyed: |
| 456 |
delete_fs(module, array) |
| 457 |
elif ( |
| 458 |
state == "absent" |
| 459 |
and exists |
| 460 |
and module.params["eradicate"] |
| 461 |
and filesystem.destroyed |
| 462 |
): |
| 463 |
eradicate_fs(module, array) |
| 464 |
|
| 465 |
module.exit_json(changed=False) |
| 466 |
|
| 467 |
|
| 468 |
if __name__ == "__main__": |
| 469 |
main() |