| 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_connect |
| 20 |
version_added: '1.0.0' |
| 21 |
short_description: Manage replication connections between two FlashArrays |
| 22 |
description: |
| 23 |
- Manage array connections to specified target array |
| 24 |
author: |
| 25 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 26 |
options: |
| 27 |
state: |
| 28 |
description: |
| 29 |
- Create or delete array connection |
| 30 |
default: present |
| 31 |
type: str |
| 32 |
choices: [ absent, present ] |
| 33 |
target_url: |
| 34 |
description: |
| 35 |
- Management IP address of remote array. |
| 36 |
type: str |
| 37 |
required: true |
| 38 |
target_api: |
| 39 |
description: |
| 40 |
- API token for target array |
| 41 |
type: str |
| 42 |
connection: |
| 43 |
description: |
| 44 |
- Type of connection between arrays. |
| 45 |
type: str |
| 46 |
choices: [ sync, async ] |
| 47 |
default: async |
| 48 |
transport: |
| 49 |
description: |
| 50 |
- Type of transport protocol to use for replication |
| 51 |
type: str |
| 52 |
choices: [ ip, fc ] |
| 53 |
default: ip |
| 54 |
encrypted: |
| 55 |
description: |
| 56 |
- Defines if the array connection will be encrypted |
| 57 |
type: bool |
| 58 |
default: false |
| 59 |
version_added: '1.30.0' |
| 60 |
context: |
| 61 |
description: |
| 62 |
- Name of fleet member on which to perform the operation. |
| 63 |
- This requires the array receiving the request is a member of a fleet |
| 64 |
and the context name to be a member of the same fleet. |
| 65 |
type: str |
| 66 |
default: "" |
| 67 |
version_added: '1.39.0' |
| 68 |
renew_key: |
| 69 |
description: |
| 70 |
- Update array connection with a new encryption key. |
| 71 |
- If set to true, other array connection attributes will not be modified. |
| 72 |
type: bool |
| 73 |
default: false |
| 74 |
version_added: '1.40.0' |
| 75 |
refresh: |
| 76 |
description: |
| 77 |
- Array will attempt to communicate with the connection peer in order to update |
| 78 |
the connection attributes on both arrays with any changes that have occurred. |
| 79 |
- If set to true, other array connection attributes will not be modified. |
| 80 |
type: bool |
| 81 |
default: false |
| 82 |
version_added: '1.40.0' |
| 83 |
extends_documentation_fragment: |
| 84 |
- purestorage.flasharray.purestorage.fa |
| 85 |
""" |
| 86 |
|
| 87 |
EXAMPLES = r""" |
| 88 |
- name: Create an IPv4 async connection to remote array |
| 89 |
purestorage.flasharray.purefa_connect: |
| 90 |
target_url: 10.10.10.20 |
| 91 |
target_api: 9c0b56bc-f941-f7a6-9f85-dcc3e9a8f7d6 |
| 92 |
connection: async |
| 93 |
fa_url: 10.10.10.2 |
| 94 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 95 |
- name: Create an IPv6 async connection to remote array |
| 96 |
purestorage.flasharray.purefa_connect: |
| 97 |
target_url: "[2001:db8:abcd:12::10]" |
| 98 |
target_api: 9c0b56bc-f941-f7a6-9f85-dcc3e9a8f7d6 |
| 99 |
connection: async |
| 100 |
fa_url: "[2001:db8:abcd:12::13]" |
| 101 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 102 |
- name: Delete connection to remote array |
| 103 |
purestorage.flasharray.purefa_connect: |
| 104 |
state: absent |
| 105 |
target_url: 10.10.10.20 |
| 106 |
target_api: 9c0b56bc-f941-f7a6-9f85-dcc3e9a8f7d6 |
| 107 |
fa_url: 10.10.10.2 |
| 108 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 109 |
""" |
| 110 |
|
| 111 |
RETURN = r""" |
| 112 |
""" |
| 113 |
|
| 114 |
HAS_PYPURECLIENT = True |
| 115 |
try: |
| 116 |
from pypureclient.flasharray import ( |
| 117 |
ArrayConnectionPost, |
| 118 |
ArrayConnectionPatch, |
| 119 |
Client, |
| 120 |
) |
| 121 |
except ImportError: |
| 122 |
HAS_PYPURECLIENT = False |
| 123 |
|
| 124 |
HAS_DISTRO = True |
| 125 |
try: |
| 126 |
import distro |
| 127 |
except ImportError: |
| 128 |
HAS_DISTRO = False |
| 129 |
|
| 130 |
from ansible.module_utils.basic import AnsibleModule |
| 131 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import ( |
| 132 |
get_array, |
| 133 |
purefa_argument_spec, |
| 134 |
) |
| 135 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.version import ( |
| 136 |
LooseVersion, |
| 137 |
) |
| 138 |
import platform |
| 139 |
import socket |
| 140 |
|
| 141 |
ENCRYPT_VERSION = "2.33" |
| 142 |
CONTEXT_VERSION = "2.38" |
| 143 |
|
| 144 |
|
| 145 |
def _lookup(address): |
| 146 |
"""Perform Reverse DNS lookup on IP address""" |
| 147 |
fqdn = socket.getnameinfo((address, 0), 0)[0] |
| 148 |
shortname = fqdn.split(".")[0] |
| 149 |
return shortname, fqdn |
| 150 |
|
| 151 |
|
| 152 |
def _check_connected(module, array): |
| 153 |
api_version = array.get_rest_version() |
| 154 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 155 |
res = array.get_array_connections(context_names=[module.params["context"]]) |
| 156 |
else: |
| 157 |
res = array.get_array_connections() |
| 158 |
if res.status_code != 200: |
| 159 |
return None |
| 160 |
connected_arrays = list(res.items) |
| 161 |
for target in connected_arrays: |
| 162 |
remote_mgmt_address = target.management_address |
| 163 |
if ( |
| 164 |
remote_mgmt_address == module.params["target_url"].strip("[]") |
| 165 |
or remote_mgmt_address in [_lookup(module.params["target_url"].strip("[]"))] |
| 166 |
and "connected" in target.status |
| 167 |
): |
| 168 |
return target |
| 169 |
return None |
| 170 |
|
| 171 |
|
| 172 |
def break_connection(module, array, target_array): |
| 173 |
"""Break connection between arrays""" |
| 174 |
changed = True |
| 175 |
api_version = array.get_rest_version() |
| 176 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 177 |
source_array = list( |
| 178 |
array.get_arrays(context_names=[module.params["context"]]).items |
| 179 |
)[0].name |
| 180 |
else: |
| 181 |
source_array = list(array.get_arrays().items)[0].name |
| 182 |
if getattr(target_array, "management_address", None) is None: |
| 183 |
module.fail_json( |
| 184 |
msg="disconnect can only happen from the array that formed the connection" |
| 185 |
) |
| 186 |
if not module.check_mode: |
| 187 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 188 |
res = array.delete_array_connections( |
| 189 |
names=[target_array.name], context_names=[module.params["context"]] |
| 190 |
) |
| 191 |
else: |
| 192 |
res = array.delete_array_connections(names=[target_array.name]) |
| 193 |
if res.status_code != 200: |
| 194 |
module.fail_json( |
| 195 |
msg="Failed to disconnect {0} from {1}.Error: {2}".format( |
| 196 |
target_array.name, |
| 197 |
source_array, |
| 198 |
res.errors[0].mesaage, |
| 199 |
) |
| 200 |
) |
| 201 |
module.exit_json(changed=changed) |
| 202 |
|
| 203 |
|
| 204 |
def update_connection(module, array, target_array): |
| 205 |
"""Change existing array connection""" |
| 206 |
changed = False |
| 207 |
if HAS_DISTRO: |
| 208 |
user_agent = "%(base)s %(class)s/%(version)s (%(platform)s)" % { |
| 209 |
"base": "Ansible", |
| 210 |
"class": __name__, |
| 211 |
"version": 1.5, |
| 212 |
"platform": distro.name(pretty=True), |
| 213 |
} |
| 214 |
else: |
| 215 |
user_agent = "%(base)s %(class)s/%(version)s (%(platform)s)" % { |
| 216 |
"base": "Ansible", |
| 217 |
"class": __name__, |
| 218 |
"version": 1.5, |
| 219 |
"platform": platform.platform(), |
| 220 |
} |
| 221 |
api_version = array.get_rest_version() |
| 222 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 223 |
source_array = list( |
| 224 |
array.get_arrays(context_names=[module.params["context"]]).items |
| 225 |
)[0].name |
| 226 |
else: |
| 227 |
source_array = list(array.get_arrays().items)[0].name |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
if module.params["renew_key"]: |
| 232 |
|
| 233 |
changed = True |
| 234 |
if not module.check_mode: |
| 235 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 236 |
res = array.patch_array_connections( |
| 237 |
names=[target_array.name], |
| 238 |
renew_encryption_key=True, |
| 239 |
array_connection=ArrayConnectionPatch(), |
| 240 |
context_names=[module.params["context"]], |
| 241 |
) |
| 242 |
else: |
| 243 |
res = array.patch_array_connections( |
| 244 |
names=[target_array.name], |
| 245 |
renew_encryption_key=True, |
| 246 |
array_connection=ArrayConnectionPatch(), |
| 247 |
) |
| 248 |
if res.status_code != 200: |
| 249 |
module.fail_json( |
| 250 |
msg="Failed to renew encryption key for connection to {0}. Error: {1}".format( |
| 251 |
target_array.name, res.errors[0].message |
| 252 |
) |
| 253 |
) |
| 254 |
module.exit_json(changed=changed) |
| 255 |
if module.params["refresh"]: |
| 256 |
|
| 257 |
changed = True |
| 258 |
if not module.check_mode: |
| 259 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 260 |
res = array.patch_array_connections( |
| 261 |
names=[target_array.name], |
| 262 |
refresh=True, |
| 263 |
array_connection=ArrayConnectionPatch(), |
| 264 |
context_names=[module.params["context"]], |
| 265 |
) |
| 266 |
else: |
| 267 |
res = array.patch_array_connections( |
| 268 |
names=[target_array.name], |
| 269 |
refresh=True, |
| 270 |
array_connection=ArrayConnectionPatch(), |
| 271 |
) |
| 272 |
if res.status_code != 200: |
| 273 |
module.fail_json( |
| 274 |
msg="Failed to refresh connection to {0}. Error: {1}".format( |
| 275 |
target_array.name, res.errors[0].message |
| 276 |
) |
| 277 |
) |
| 278 |
module.exit_json(changed=changed) |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
if LooseVersion(ENCRYPT_VERSION) >= LooseVersion(api_version): |
| 283 |
if module.params["encrypted"]: |
| 284 |
encrypted = "encrypted" |
| 285 |
else: |
| 286 |
encrypted = "unencrypted" |
| 287 |
if target_array.encryption != encrypted: |
| 288 |
|
| 289 |
remote_system = Client( |
| 290 |
target=module.params["target_url"], |
| 291 |
api_token=module.params["target_api"], |
| 292 |
user_agent=user_agent, |
| 293 |
) |
| 294 |
connection_key = list( |
| 295 |
remote_system.get_array_connections_connection_key( |
| 296 |
encrypted=module.params["encrypted"] |
| 297 |
).items |
| 298 |
)[0].connection_key |
| 299 |
changed = True |
| 300 |
if not module.check_mode: |
| 301 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 302 |
res = array.patch_array_connections( |
| 303 |
names=[target_array.name], |
| 304 |
array_connection=ArrayConnectionPatch( |
| 305 |
encryption=encrypted, connection_key=connection_key |
| 306 |
), |
| 307 |
context_names=[module.params["context"]], |
| 308 |
) |
| 309 |
else: |
| 310 |
res = array.patch_array_connections( |
| 311 |
names=[target_array.name], |
| 312 |
array_connection=ArrayConnectionPatch( |
| 313 |
encryption=encrypted, connection_key=connection_key |
| 314 |
), |
| 315 |
) |
| 316 |
if res.status_code != 200: |
| 317 |
module.fail_json( |
| 318 |
msg="Failed to change encryption for {0}. Error: {1}".format( |
| 319 |
target_array.name, res.errors[0].message |
| 320 |
) |
| 321 |
) |
| 322 |
if module.params["connection"] != target_array.type: |
| 323 |
changed = True |
| 324 |
if not module.check_mode: |
| 325 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 326 |
res = array.patch_array_connections( |
| 327 |
names=[target_array.name], |
| 328 |
array_connection=ArrayConnectionPatch( |
| 329 |
type=module.params["connection"] |
| 330 |
), |
| 331 |
context_names=[module.params["context"]], |
| 332 |
) |
| 333 |
else: |
| 334 |
res = array.patch_array_connections( |
| 335 |
names=[target_array.name], |
| 336 |
array_connection=ArrayConnectionPatch( |
| 337 |
type=module.params["connection"] |
| 338 |
), |
| 339 |
) |
| 340 |
if res.status_code != 200: |
| 341 |
module.fail_json( |
| 342 |
msg="Failed to change connection type for {0}. Error: {1}".format( |
| 343 |
target_array.name, res.errors[0].message |
| 344 |
) |
| 345 |
) |
| 346 |
module.exit_json(changed=changed) |
| 347 |
|
| 348 |
|
| 349 |
def create_connection(module, array): |
| 350 |
"""Create connection between arrays""" |
| 351 |
changed = True |
| 352 |
api_version = array.get_rest_version() |
| 353 |
if HAS_DISTRO: |
| 354 |
user_agent = "%(base)s %(class)s/%(version)s (%(platform)s)" % { |
| 355 |
"base": "Ansible", |
| 356 |
"class": __name__, |
| 357 |
"version": 1.5, |
| 358 |
"platform": distro.name(pretty=True), |
| 359 |
} |
| 360 |
else: |
| 361 |
user_agent = "%(base)s %(class)s/%(version)s (%(platform)s)" % { |
| 362 |
"base": "Ansible", |
| 363 |
"class": __name__, |
| 364 |
"version": 1.5, |
| 365 |
"platform": platform.platform(), |
| 366 |
} |
| 367 |
remote_system = Client( |
| 368 |
target=module.params["target_url"], |
| 369 |
api_token=module.params["target_api"], |
| 370 |
user_agent=user_agent, |
| 371 |
) |
| 372 |
try: |
| 373 |
connections = remote_system.get_array_connections_connection_key( |
| 374 |
encrypted=module.params["encrypted"] |
| 375 |
) |
| 376 |
except TypeError as exc: |
| 377 |
if module.params["encrypted"] == "encrypted": |
| 378 |
module.warning( |
| 379 |
msg=( |
| 380 |
"Remote array version of Purity//FA does not support " |
| 381 |
"encryption. Continuing without encryption" |
| 382 |
), |
| 383 |
) |
| 384 |
connections = remote_system.get_array_connections_connection_key() |
| 385 |
|
| 386 |
connection_key = list(connections.items)[0].connection_key |
| 387 |
if LooseVersion(ENCRYPT_VERSION) >= LooseVersion(api_version): |
| 388 |
if module.params["encrypted"]: |
| 389 |
encrypted = "encrypted" |
| 390 |
else: |
| 391 |
encrypted = "unencrypted" |
| 392 |
array_connection = ArrayConnectionPost( |
| 393 |
type=module.params["connection"].lower(), |
| 394 |
management_address=module.params["target_url"].strip("[]"), |
| 395 |
replication_transport=module.params["transport"], |
| 396 |
connection_key=connection_key, |
| 397 |
encryption=encrypted, |
| 398 |
) |
| 399 |
else: |
| 400 |
array_connection = ArrayConnectionPost( |
| 401 |
type=module.params["connection"].lower(), |
| 402 |
management_address=module.params["target_url"].strip("[]"), |
| 403 |
replication_transport=module.params["transport"], |
| 404 |
connection_key=connection_key, |
| 405 |
) |
| 406 |
if not module.check_mode: |
| 407 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 408 |
res = array.post_array_connections( |
| 409 |
array_connection=array_connection, |
| 410 |
context_names=[module.params["context"]], |
| 411 |
) |
| 412 |
else: |
| 413 |
res = array.post_array_connections(array_connection=array_connection) |
| 414 |
if res.status_code != 200: |
| 415 |
module.fail_json( |
| 416 |
msg="Array Connection failed. Error: {0}".format(res.errors[0].message) |
| 417 |
) |
| 418 |
module.exit_json(changed=changed) |
| 419 |
|
| 420 |
|
| 421 |
def main(): |
| 422 |
argument_spec = purefa_argument_spec() |
| 423 |
argument_spec.update( |
| 424 |
dict( |
| 425 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 426 |
connection=dict(type="str", default="async", choices=["async", "sync"]), |
| 427 |
transport=dict(type="str", default="ip", choices=["ip", "fc"]), |
| 428 |
target_url=dict(type="str", required=True), |
| 429 |
target_api=dict(type="str"), |
| 430 |
encrypted=dict(type="bool", default=False), |
| 431 |
renew_key=dict(type="bool", default=False), |
| 432 |
refresh=dict(type="bool", default=False), |
| 433 |
context=dict(type="str", default=""), |
| 434 |
) |
| 435 |
) |
| 436 |
|
| 437 |
required_if = [("state", "present", ["target_api"])] |
| 438 |
|
| 439 |
module = AnsibleModule( |
| 440 |
argument_spec, required_if=required_if, supports_check_mode=True |
| 441 |
) |
| 442 |
|
| 443 |
if not HAS_PYPURECLIENT: |
| 444 |
module.fail_json(msg="pypureclient sdk is required for this module") |
| 445 |
|
| 446 |
state = module.params["state"] |
| 447 |
array = get_array(module) |
| 448 |
target_array = _check_connected(module, array) |
| 449 |
|
| 450 |
module.params["connection"] = module.params["connection"] + "-replication" |
| 451 |
|
| 452 |
if state == "present" and target_array is None: |
| 453 |
create_connection(module, array) |
| 454 |
elif state == "present" and target_array is not None: |
| 455 |
update_connection(module, array, target_array) |
| 456 |
elif state == "absent" and target_array is not None: |
| 457 |
break_connection(module, array, target_array) |
| 458 |
|
| 459 |
module.exit_json(changed=False) |
| 460 |
|
| 461 |
|
| 462 |
if __name__ == "__main__": |
| 463 |
main() |