| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = r''' |
| 11 |
--- |
| 12 |
module: bigip_profile_persistence_universal |
| 13 |
short_description: Manage universal persistence profiles |
| 14 |
description: |
| 15 |
- Manages universal persistence profiles on the BIG-IP system. |
| 16 |
version_added: "1.1.0" |
| 17 |
options: |
| 18 |
name: |
| 19 |
description: |
| 20 |
- Specifies the name of the profile. |
| 21 |
type: str |
| 22 |
required: True |
| 23 |
parent: |
| 24 |
description: |
| 25 |
- Specifies the profile from which this profile inherits settings. |
| 26 |
- When creating a new profile, if this parameter is not specified, the default |
| 27 |
is the system-supplied C(universal) profile. |
| 28 |
type: str |
| 29 |
app_service: |
| 30 |
description: |
| 31 |
- The iApp service to be associated with this profile. When no service is |
| 32 |
specified, the default is None. |
| 33 |
type: str |
| 34 |
match_across_services: |
| 35 |
description: |
| 36 |
- When C(true), specifies all persistent connections from a client IP address that go |
| 37 |
to the same virtual IP address also go to the same node. |
| 38 |
- When creating a new profile, if this parameter is not specified, the |
| 39 |
default is provided by the parent profile. |
| 40 |
type: bool |
| 41 |
match_across_virtuals: |
| 42 |
description: |
| 43 |
- When C(true), specifies all persistent connections from the same client IP address |
| 44 |
go to the same node. |
| 45 |
- When creating a new profile, if this parameter is not specified, the |
| 46 |
default is provided by the parent profile. |
| 47 |
type: bool |
| 48 |
match_across_pools: |
| 49 |
description: |
| 50 |
- When C(true), specifies the system can use any pool that contains this persistence |
| 51 |
record. |
| 52 |
- When creating a new profile, if this parameter is not specified, the |
| 53 |
default is provided by the parent profile. |
| 54 |
type: bool |
| 55 |
mirror: |
| 56 |
description: |
| 57 |
- When C(true), specifies if the active unit goes into the standby mode, the system |
| 58 |
mirrors any persistence records to its peer. |
| 59 |
- When creating a new profile, if this parameter is not specified, the |
| 60 |
default is provided by the parent profile. |
| 61 |
type: bool |
| 62 |
rule: |
| 63 |
description: |
| 64 |
- Specifies the iRule used to select a persistence entry. |
| 65 |
- When creating a new profile, if this parameter is not specified, the |
| 66 |
default is C(None), which disables this setting. |
| 67 |
type: str |
| 68 |
timeout: |
| 69 |
description: |
| 70 |
- Specifies the duration of the persistence entries. |
| 71 |
- When creating a new profile, if this parameter is not specified, the |
| 72 |
default is provided by the parent profile. |
| 73 |
- To specify an indefinite timeout, use the value C(indefinite). |
| 74 |
- If specifying a numeric timeout, the value must be between C(1) and C(4294967295). |
| 75 |
type: str |
| 76 |
override_connection_limit: |
| 77 |
description: |
| 78 |
- When C(true), specifies the system allows you to specify that pool member connection |
| 79 |
limits will be overridden for persisted clients. |
| 80 |
- Per-virtual connection limits remain hard limits and are not overridden. |
| 81 |
type: bool |
| 82 |
partition: |
| 83 |
description: |
| 84 |
- Device partition to manage resources on. |
| 85 |
type: str |
| 86 |
default: Common |
| 87 |
state: |
| 88 |
description: |
| 89 |
- When C(present), ensures the profile exists. |
| 90 |
- When C(absent), ensures the profile is removed. |
| 91 |
type: str |
| 92 |
choices: |
| 93 |
- present |
| 94 |
- absent |
| 95 |
default: present |
| 96 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 97 |
author: |
| 98 |
- Nitin Khanna (@nitinthewiz) |
| 99 |
''' |
| 100 |
|
| 101 |
EXAMPLES = r''' |
| 102 |
- name: Create a profile |
| 103 |
bigip_profile_persistence_universal: |
| 104 |
name: foo |
| 105 |
state: present |
| 106 |
match_across_services: true |
| 107 |
match_across_virtuals: true |
| 108 |
mirror: true |
| 109 |
provider: |
| 110 |
password: secret |
| 111 |
server: lb.mydomain.com |
| 112 |
user: admin |
| 113 |
delegate_to: localhost |
| 114 |
''' |
| 115 |
|
| 116 |
RETURN = r''' |
| 117 |
parent: |
| 118 |
description: The parent profile. |
| 119 |
returned: changed |
| 120 |
type: str |
| 121 |
sample: /Common/cookie |
| 122 |
app_service: |
| 123 |
description: The iApp service associated with this profile |
| 124 |
returned: changed |
| 125 |
type: str |
| 126 |
sample: /Common/good_service.app/good_service |
| 127 |
match_across_pools: |
| 128 |
description: The new Match Across Pools value. |
| 129 |
returned: changed |
| 130 |
type: bool |
| 131 |
sample: true |
| 132 |
match_across_services: |
| 133 |
description: The new Match Across Services value. |
| 134 |
returned: changed |
| 135 |
type: bool |
| 136 |
sample: false |
| 137 |
match_across_virtuals: |
| 138 |
description: The new Match Across Virtuals value. |
| 139 |
returned: changed |
| 140 |
type: bool |
| 141 |
sample: true |
| 142 |
override_connection_limit: |
| 143 |
description: The new Override Connection Limit value. |
| 144 |
returned: changed |
| 145 |
type: bool |
| 146 |
sample: false |
| 147 |
timeout: |
| 148 |
description: The duration of the persistence entries. |
| 149 |
returned: changed |
| 150 |
type: str |
| 151 |
sample: 180 |
| 152 |
mirror: |
| 153 |
description: The new Mirror value. |
| 154 |
returned: changed |
| 155 |
type: bool |
| 156 |
sample: true |
| 157 |
rule: |
| 158 |
description: The iRule used to select persistence entry. |
| 159 |
returned: changed |
| 160 |
type: str |
| 161 |
sample: /Common/_sys_https_redirect |
| 162 |
''' |
| 163 |
from datetime import datetime |
| 164 |
from ansible.module_utils.basic import ( |
| 165 |
AnsibleModule, env_fallback |
| 166 |
) |
| 167 |
|
| 168 |
from ..module_utils.bigip import F5RestClient |
| 169 |
from ..module_utils.common import ( |
| 170 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, flatten_boolean, fq_name |
| 171 |
) |
| 172 |
from ..module_utils.icontrol import tmos_version |
| 173 |
from ..module_utils.teem import send_teem |
| 174 |
|
| 175 |
|
| 176 |
class Parameters(AnsibleF5Parameters): |
| 177 |
api_map = { |
| 178 |
'appService': 'app_service', |
| 179 |
'defaultsFrom': 'parent', |
| 180 |
'matchAcrossPools': 'match_across_pools', |
| 181 |
'matchAcrossServices': 'match_across_services', |
| 182 |
'matchAcrossVirtuals': 'match_across_virtuals', |
| 183 |
'overrideConnectionLimit': 'override_connection_limit', |
| 184 |
} |
| 185 |
|
| 186 |
api_attributes = [ |
| 187 |
'appService', |
| 188 |
'defaultsFrom', |
| 189 |
'matchAcrossPools', |
| 190 |
'matchAcrossServices', |
| 191 |
'matchAcrossVirtuals', |
| 192 |
'overrideConnectionLimit', |
| 193 |
'timeout', |
| 194 |
'mirror', |
| 195 |
'rule', |
| 196 |
] |
| 197 |
|
| 198 |
returnables = [ |
| 199 |
'app_service', |
| 200 |
'parent', |
| 201 |
'match_across_pools', |
| 202 |
'match_across_services', |
| 203 |
'match_across_virtuals', |
| 204 |
'override_connection_limit', |
| 205 |
'timeout', |
| 206 |
'mirror', |
| 207 |
'rule', |
| 208 |
] |
| 209 |
|
| 210 |
updatables = [ |
| 211 |
'app_service', |
| 212 |
'match_across_pools', |
| 213 |
'match_across_services', |
| 214 |
'match_across_virtuals', |
| 215 |
'override_connection_limit', |
| 216 |
'timeout', |
| 217 |
'parent', |
| 218 |
'mirror', |
| 219 |
'rule', |
| 220 |
] |
| 221 |
|
| 222 |
@property |
| 223 |
def timeout(self): |
| 224 |
if self._values['timeout'] in [None, 'indefinite']: |
| 225 |
return self._values['timeout'] |
| 226 |
timeout = int(self._values['timeout']) |
| 227 |
if 1 > timeout > 4294967295: |
| 228 |
raise F5ModuleError( |
| 229 |
"'timeout' value must be between 1 and 4294967295, or the value 'indefinite'." |
| 230 |
) |
| 231 |
return timeout |
| 232 |
|
| 233 |
@property |
| 234 |
def match_across_pools(self): |
| 235 |
return flatten_boolean(self._values['match_across_pools']) |
| 236 |
|
| 237 |
@property |
| 238 |
def match_across_services(self): |
| 239 |
return flatten_boolean(self._values['match_across_services']) |
| 240 |
|
| 241 |
@property |
| 242 |
def match_across_virtuals(self): |
| 243 |
return flatten_boolean(self._values['match_across_virtuals']) |
| 244 |
|
| 245 |
@property |
| 246 |
def override_connection_limit(self): |
| 247 |
return flatten_boolean(self._values['override_connection_limit']) |
| 248 |
|
| 249 |
|
| 250 |
class ApiParameters(Parameters): |
| 251 |
pass |
| 252 |
|
| 253 |
|
| 254 |
class ModuleParameters(Parameters): |
| 255 |
@property |
| 256 |
def parent(self): |
| 257 |
if self._values['parent'] is None: |
| 258 |
return None |
| 259 |
result = fq_name(self.partition, self._values['parent']) |
| 260 |
return result |
| 261 |
|
| 262 |
@property |
| 263 |
def mirror(self): |
| 264 |
result = flatten_boolean(self._values['mirror']) |
| 265 |
if result == 'yes': |
| 266 |
return 'enabled' |
| 267 |
if result == 'no': |
| 268 |
return 'disabled' |
| 269 |
|
| 270 |
|
| 271 |
class Changes(Parameters): |
| 272 |
def to_return(self): |
| 273 |
result = {} |
| 274 |
try: |
| 275 |
for returnable in self.returnables: |
| 276 |
result[returnable] = getattr(self, returnable) |
| 277 |
result = self._filter_params(result) |
| 278 |
except Exception: |
| 279 |
pass |
| 280 |
return result |
| 281 |
|
| 282 |
|
| 283 |
class UsableChanges(Changes): |
| 284 |
@property |
| 285 |
def match_across_pools(self): |
| 286 |
if self._values['match_across_pools'] is None: |
| 287 |
return None |
| 288 |
elif self._values['match_across_pools'] == 'yes': |
| 289 |
return 'enabled' |
| 290 |
return 'disabled' |
| 291 |
|
| 292 |
@property |
| 293 |
def match_across_services(self): |
| 294 |
if self._values['match_across_services'] is None: |
| 295 |
return None |
| 296 |
elif self._values['match_across_services'] == 'yes': |
| 297 |
return 'enabled' |
| 298 |
return 'disabled' |
| 299 |
|
| 300 |
@property |
| 301 |
def match_across_virtuals(self): |
| 302 |
if self._values['match_across_virtuals'] is None: |
| 303 |
return None |
| 304 |
elif self._values['match_across_virtuals'] == 'yes': |
| 305 |
return 'enabled' |
| 306 |
return 'disabled' |
| 307 |
|
| 308 |
@property |
| 309 |
def override_connection_limit(self): |
| 310 |
if self._values['override_connection_limit'] is None: |
| 311 |
return None |
| 312 |
elif self._values['override_connection_limit'] == 'yes': |
| 313 |
return 'enabled' |
| 314 |
return 'disabled' |
| 315 |
|
| 316 |
|
| 317 |
class ReportableChanges(Changes): |
| 318 |
@property |
| 319 |
def match_across_pools(self): |
| 320 |
return flatten_boolean(self._values['match_across_pools']) |
| 321 |
|
| 322 |
@property |
| 323 |
def match_across_services(self): |
| 324 |
return flatten_boolean(self._values['match_across_services']) |
| 325 |
|
| 326 |
@property |
| 327 |
def match_across_virtuals(self): |
| 328 |
return flatten_boolean(self._values['match_across_virtuals']) |
| 329 |
|
| 330 |
@property |
| 331 |
def override_connection_limit(self): |
| 332 |
return flatten_boolean(self._values['override_connection_limit']) |
| 333 |
|
| 334 |
|
| 335 |
class Difference(object): |
| 336 |
def __init__(self, want, have=None): |
| 337 |
self.want = want |
| 338 |
self.have = have |
| 339 |
|
| 340 |
def compare(self, param): |
| 341 |
try: |
| 342 |
result = getattr(self, param) |
| 343 |
return result |
| 344 |
except AttributeError: |
| 345 |
return self.__default(param) |
| 346 |
|
| 347 |
def __default(self, param): |
| 348 |
attr1 = getattr(self.want, param) |
| 349 |
try: |
| 350 |
attr2 = getattr(self.have, param) |
| 351 |
if attr1 != attr2: |
| 352 |
return attr1 |
| 353 |
except AttributeError: |
| 354 |
return attr1 |
| 355 |
|
| 356 |
|
| 357 |
class ModuleManager(object): |
| 358 |
def __init__(self, *args, **kwargs): |
| 359 |
self.module = kwargs.get('module', None) |
| 360 |
self.client = F5RestClient(**self.module.params) |
| 361 |
self.want = ModuleParameters(params=self.module.params) |
| 362 |
self.have = ApiParameters() |
| 363 |
self.changes = UsableChanges() |
| 364 |
|
| 365 |
def _set_changed_options(self): |
| 366 |
changed = {} |
| 367 |
for key in Parameters.returnables: |
| 368 |
if getattr(self.want, key) is not None: |
| 369 |
changed[key] = getattr(self.want, key) |
| 370 |
if changed: |
| 371 |
self.changes = UsableChanges(params=changed) |
| 372 |
|
| 373 |
def _update_changed_options(self): |
| 374 |
diff = Difference(self.want, self.have) |
| 375 |
updatables = Parameters.updatables |
| 376 |
changed = dict() |
| 377 |
for k in updatables: |
| 378 |
change = diff.compare(k) |
| 379 |
if change is None: |
| 380 |
continue |
| 381 |
else: |
| 382 |
if isinstance(change, dict): |
| 383 |
changed.update(change) |
| 384 |
else: |
| 385 |
changed[k] = change |
| 386 |
if changed: |
| 387 |
self.changes = UsableChanges(params=changed) |
| 388 |
return True |
| 389 |
return False |
| 390 |
|
| 391 |
def should_update(self): |
| 392 |
result = self._update_changed_options() |
| 393 |
if result: |
| 394 |
return True |
| 395 |
return False |
| 396 |
|
| 397 |
def exec_module(self): |
| 398 |
start = datetime.now().isoformat() |
| 399 |
version = tmos_version(self.client) |
| 400 |
changed = False |
| 401 |
result = dict() |
| 402 |
state = self.want.state |
| 403 |
|
| 404 |
if state == "present": |
| 405 |
changed = self.present() |
| 406 |
elif state == "absent": |
| 407 |
changed = self.absent() |
| 408 |
|
| 409 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 410 |
changes = reportable.to_return() |
| 411 |
result.update(**changes) |
| 412 |
result.update(dict(changed=changed)) |
| 413 |
self._announce_deprecations(result) |
| 414 |
send_teem(start, self.client, self.module, version) |
| 415 |
return result |
| 416 |
|
| 417 |
def _announce_deprecations(self, result): |
| 418 |
warnings = result.pop('__warnings', []) |
| 419 |
for warning in warnings: |
| 420 |
self.client.module.deprecate( |
| 421 |
msg=warning['msg'], |
| 422 |
version=warning['version'] |
| 423 |
) |
| 424 |
|
| 425 |
def present(self): |
| 426 |
if self.exists(): |
| 427 |
return self.update() |
| 428 |
else: |
| 429 |
return self.create() |
| 430 |
|
| 431 |
def exists(self): |
| 432 |
uri = "https://{0}:{1}/mgmt/tm/ltm/persistence/universal/{2}".format( |
| 433 |
self.client.provider['server'], |
| 434 |
self.client.provider['server_port'], |
| 435 |
transform_name(self.want.partition, self.want.name) |
| 436 |
) |
| 437 |
resp = self.client.api.get(uri) |
| 438 |
try: |
| 439 |
response = resp.json() |
| 440 |
except ValueError as ex: |
| 441 |
raise F5ModuleError(str(ex)) |
| 442 |
|
| 443 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 444 |
return False |
| 445 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 446 |
return True |
| 447 |
|
| 448 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 449 |
|
| 450 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 451 |
if 'message' in response: |
| 452 |
raise F5ModuleError(response['message']) |
| 453 |
else: |
| 454 |
raise F5ModuleError(resp.content) |
| 455 |
|
| 456 |
def update(self): |
| 457 |
self.have = self.read_current_from_device() |
| 458 |
if not self.should_update(): |
| 459 |
return False |
| 460 |
if self.module.check_mode: |
| 461 |
return True |
| 462 |
self.update_on_device() |
| 463 |
return True |
| 464 |
|
| 465 |
def remove(self): |
| 466 |
if self.module.check_mode: |
| 467 |
return True |
| 468 |
self.remove_from_device() |
| 469 |
if self.exists(): |
| 470 |
raise F5ModuleError("Failed to delete the resource.") |
| 471 |
return True |
| 472 |
|
| 473 |
def create(self): |
| 474 |
self._set_changed_options() |
| 475 |
if self.module.check_mode: |
| 476 |
return True |
| 477 |
self.create_on_device() |
| 478 |
return True |
| 479 |
|
| 480 |
def create_on_device(self): |
| 481 |
params = self.changes.api_params() |
| 482 |
params['name'] = self.want.name |
| 483 |
params['partition'] = self.want.partition |
| 484 |
uri = "https://{0}:{1}/mgmt/tm/ltm/persistence/universal/".format( |
| 485 |
self.client.provider['server'], |
| 486 |
self.client.provider['server_port'] |
| 487 |
) |
| 488 |
resp = self.client.api.post(uri, json=params) |
| 489 |
try: |
| 490 |
response = resp.json() |
| 491 |
except ValueError as ex: |
| 492 |
raise F5ModuleError(str(ex)) |
| 493 |
|
| 494 |
if 'code' in response and response['code'] in [400, 403, 404]: |
| 495 |
if 'message' in response: |
| 496 |
raise F5ModuleError(response['message']) |
| 497 |
else: |
| 498 |
raise F5ModuleError(resp.content) |
| 499 |
|
| 500 |
def update_on_device(self): |
| 501 |
params = self.changes.api_params() |
| 502 |
uri = "https://{0}:{1}/mgmt/tm/ltm/persistence/universal/{2}".format( |
| 503 |
self.client.provider['server'], |
| 504 |
self.client.provider['server_port'], |
| 505 |
transform_name(self.want.partition, self.want.name) |
| 506 |
) |
| 507 |
resp = self.client.api.patch(uri, json=params) |
| 508 |
try: |
| 509 |
response = resp.json() |
| 510 |
except ValueError as ex: |
| 511 |
raise F5ModuleError(str(ex)) |
| 512 |
|
| 513 |
if 'code' in response and response['code'] in [400, 404]: |
| 514 |
if 'message' in response: |
| 515 |
raise F5ModuleError(response['message']) |
| 516 |
else: |
| 517 |
raise F5ModuleError(resp.content) |
| 518 |
|
| 519 |
def absent(self): |
| 520 |
if self.exists(): |
| 521 |
return self.remove() |
| 522 |
return False |
| 523 |
|
| 524 |
def remove_from_device(self): |
| 525 |
uri = "https://{0}:{1}/mgmt/tm/ltm/persistence/universal/{2}".format( |
| 526 |
self.client.provider['server'], |
| 527 |
self.client.provider['server_port'], |
| 528 |
transform_name(self.want.partition, self.want.name) |
| 529 |
) |
| 530 |
resp = self.client.api.delete(uri) |
| 531 |
if resp.status == 200: |
| 532 |
return True |
| 533 |
raise F5ModuleError(resp.content) |
| 534 |
|
| 535 |
def read_current_from_device(self): |
| 536 |
uri = "https://{0}:{1}/mgmt/tm/ltm/persistence/universal/{2}".format( |
| 537 |
self.client.provider['server'], |
| 538 |
self.client.provider['server_port'], |
| 539 |
transform_name(self.want.partition, self.want.name) |
| 540 |
) |
| 541 |
resp = self.client.api.get(uri) |
| 542 |
try: |
| 543 |
response = resp.json() |
| 544 |
except ValueError as ex: |
| 545 |
raise F5ModuleError(str(ex)) |
| 546 |
|
| 547 |
if 'code' in response and response['code'] == 400: |
| 548 |
if 'message' in response: |
| 549 |
raise F5ModuleError(response['message']) |
| 550 |
else: |
| 551 |
raise F5ModuleError(resp.content) |
| 552 |
return ApiParameters(params=response) |
| 553 |
|
| 554 |
|
| 555 |
class ArgumentSpec(object): |
| 556 |
def __init__(self): |
| 557 |
self.supports_check_mode = True |
| 558 |
argument_spec = dict( |
| 559 |
name=dict(required=True), |
| 560 |
app_service=dict(), |
| 561 |
parent=dict(), |
| 562 |
match_across_services=dict(type='bool'), |
| 563 |
match_across_virtuals=dict(type='bool'), |
| 564 |
match_across_pools=dict(type='bool'), |
| 565 |
mirror=dict(type='bool'), |
| 566 |
rule=dict(), |
| 567 |
timeout=dict(), |
| 568 |
override_connection_limit=dict(type='bool'), |
| 569 |
state=dict( |
| 570 |
default='present', |
| 571 |
choices=['present', 'absent'] |
| 572 |
), |
| 573 |
partition=dict( |
| 574 |
default='Common', |
| 575 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 576 |
) |
| 577 |
) |
| 578 |
self.argument_spec = {} |
| 579 |
self.argument_spec.update(f5_argument_spec) |
| 580 |
self.argument_spec.update(argument_spec) |
| 581 |
|
| 582 |
|
| 583 |
def main(): |
| 584 |
spec = ArgumentSpec() |
| 585 |
|
| 586 |
module = AnsibleModule( |
| 587 |
argument_spec=spec.argument_spec, |
| 588 |
supports_check_mode=spec.supports_check_mode, |
| 589 |
) |
| 590 |
|
| 591 |
try: |
| 592 |
mm = ModuleManager(module=module) |
| 593 |
results = mm.exec_module() |
| 594 |
module.exit_json(**results) |
| 595 |
except F5ModuleError as ex: |
| 596 |
module.fail_json(msg=str(ex)) |
| 597 |
|
| 598 |
|
| 599 |
if __name__ == '__main__': |
| 600 |
main() |