| 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_provision_async |
| 13 |
short_description: Manage BIG-IP module provisioning |
| 14 |
description: |
| 15 |
- Manages BIG-IP module provisioning. This module will perform provisioning operations in an asynchronous way. See the Notes |
| 16 |
section for more information. |
| 17 |
version_added: "1.25.0" |
| 18 |
options: |
| 19 |
module: |
| 20 |
description: |
| 21 |
- The module to provision in BIG-IP. |
| 22 |
type: str |
| 23 |
required: True |
| 24 |
choices: |
| 25 |
- am |
| 26 |
- afm |
| 27 |
- apm |
| 28 |
- asm |
| 29 |
- avr |
| 30 |
- cgnat |
| 31 |
- fps |
| 32 |
- gtm |
| 33 |
- ilx |
| 34 |
- lc |
| 35 |
- ltm |
| 36 |
- mgmt |
| 37 |
- pem |
| 38 |
- sam |
| 39 |
- sslo |
| 40 |
- swg |
| 41 |
- urldb |
| 42 |
- vcmp |
| 43 |
aliases: |
| 44 |
- name |
| 45 |
level: |
| 46 |
description: |
| 47 |
- Sets the provisioning level for the requested modules. Changing the |
| 48 |
level for one module may require modifying the level of another module. |
| 49 |
For example, changing one module to C(dedicated) requires setting all |
| 50 |
others to C(none). Setting the level of a module to C(none) means |
| 51 |
the module is not activated. |
| 52 |
- Use a C(state) if B(absent) to set c(level) to none and de-provision the module. |
| 53 |
- This parameter is not relevant to C(cgnat - pre tmos 15.0) or C(mgmt) and will not be |
| 54 |
applied to the C(cgnat - pre tmos 15.0) or the C(mgmt) module. |
| 55 |
type: str |
| 56 |
choices: |
| 57 |
- dedicated |
| 58 |
- nominal |
| 59 |
- minimum |
| 60 |
default: nominal |
| 61 |
memory: |
| 62 |
description: |
| 63 |
- Sets additional memory for the management module. This is in addition to |
| 64 |
the minimum allocated RAM of 1264MB. |
| 65 |
- The accepted value range is C(0 - 8192). Maximum value is restricted by |
| 66 |
the available RAM in the system. |
| 67 |
- Specifying C(large) reserves an additional 500MB for the mgmt module. |
| 68 |
- Specifying C(medium) reserves an additional 200MB for the mgmt module. |
| 69 |
- Specifying C(small) reserves no additional RAM for the mgmt module. |
| 70 |
- Use C(large) for configurations containing more than 2000 objects, or |
| 71 |
more specifically, for any configuration that exceeds 1000 objects |
| 72 |
per 2 GB of installed memory. Changing the Management C(mgmt) size |
| 73 |
after initial provisioning causes a re-provision operation. |
| 74 |
type: str |
| 75 |
check_status: |
| 76 |
description: |
| 77 |
- If C(true), then the module will run to check provisioning progress. |
| 78 |
- Required parameter, if using the C(module) parameter. |
| 79 |
type: bool |
| 80 |
default: false |
| 81 |
status_timeout: |
| 82 |
description: |
| 83 |
- The amount of time in seconds to wait for provisioning process to finish. |
| 84 |
- The accepted value range is between C(150) and C(3600) seconds. |
| 85 |
- If the device needs to restart the module, then it will return with no change and an appropriate message. In such cases, |
| 86 |
you must pause the playbook execution, until the device is ready (see the C(EXAMPLES) section). |
| 87 |
type: int |
| 88 |
default: 300 |
| 89 |
state: |
| 90 |
description: |
| 91 |
- The state of the provisioned module on the system. If C(present), then that |
| 92 |
guarantees the specified module is provisioned at the requested |
| 93 |
level, provided there are sufficient resources on the device (such |
| 94 |
as physical RAM) to support the module. |
| 95 |
- If C(absent), then that de-provisions the module. |
| 96 |
- C(absent), is not an option for the C(mgmt) module, as it cannot be de-provisioned. |
| 97 |
type: str |
| 98 |
choices: |
| 99 |
- present |
| 100 |
- absent |
| 101 |
default: present |
| 102 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 103 |
notes: |
| 104 |
- Checking for provisioning status with the C(check_status) parameter is not idempotent (see the C(EXAMPLES) section). |
| 105 |
- The module allows the same provisioning operations as bigip_provision with the difference being that it will |
| 106 |
not wait through the service restarts or device reboots. This is to fix the edge cases, when using this module in |
| 107 |
certain environments that can cause a timeout or be stuck in infinite loops, despite the provisioning operation being |
| 108 |
successful. |
| 109 |
- Use the module in conjunction with the C(bigip_wait) module, for best results. |
| 110 |
- This module requires TMOS version of 15.x and above. |
| 111 |
author: |
| 112 |
- Wojciech Wypior (@wojtek0806) |
| 113 |
''' |
| 114 |
|
| 115 |
EXAMPLES = r''' |
| 116 |
- name: Provision GTM on the device |
| 117 |
bigip_provision_async: |
| 118 |
module: "gtm" |
| 119 |
provider: |
| 120 |
server: lb.mydomain.com |
| 121 |
password: secret |
| 122 |
user: admin |
| 123 |
|
| 124 |
- name: Check for provision progress |
| 125 |
bigip_provision_async: |
| 126 |
module: "gtm" |
| 127 |
check_status: true |
| 128 |
status_timeout: 900 |
| 129 |
provider: |
| 130 |
server: lb.mydomain.com |
| 131 |
password: secret |
| 132 |
user: admin |
| 133 |
delegate_to: localhost |
| 134 |
register: status |
| 135 |
|
| 136 |
- name: Wait for 3 minutes if device is restarting services |
| 137 |
bigip_wait: |
| 138 |
timeout: 180 |
| 139 |
provider: |
| 140 |
server: lb.mydomain.com |
| 141 |
password: secret |
| 142 |
user: admin |
| 143 |
delegate_to: localhost |
| 144 |
when: |
| 145 |
- result.message == "Device is restarting services, unable to check provisioning status." |
| 146 |
|
| 147 |
- name: Re-check for provision progress |
| 148 |
bigip_provision_async: |
| 149 |
module: "gtm" |
| 150 |
check_status: true |
| 151 |
status_timeout: 900 |
| 152 |
provider: |
| 153 |
server: lb.mydomain.com |
| 154 |
password: secret |
| 155 |
user: admin |
| 156 |
delegate_to: localhost |
| 157 |
register: status |
| 158 |
when: |
| 159 |
- status.message == "Device is restarting services, unable to check provisioning status." |
| 160 |
|
| 161 |
- name: Provision GTM on the device - Idempotent Check |
| 162 |
bigip_provision_async: |
| 163 |
module: "gtm" |
| 164 |
provider: |
| 165 |
server: lb.mydomain.com |
| 166 |
password: secret |
| 167 |
user: admin |
| 168 |
delegate_to: localhost |
| 169 |
register: result |
| 170 |
when: |
| 171 |
- status.message == "Device has finished provisioning the requested module." |
| 172 |
|
| 173 |
- name: Assert Provision GTM on the device - Idempotent Check |
| 174 |
assert: |
| 175 |
that: |
| 176 |
- result is not changed |
| 177 |
when: |
| 178 |
- status.message == "Device has finished provisioning the requested module." |
| 179 |
|
| 180 |
- name: Provision VCMP on the device |
| 181 |
bigip_provision_async: |
| 182 |
module: "vcmp" |
| 183 |
level: "dedicated" |
| 184 |
provider: |
| 185 |
server: lb.mydomain.com |
| 186 |
password: secret |
| 187 |
user: admin |
| 188 |
delegate_to: localhost |
| 189 |
|
| 190 |
- name: Check for provision progress |
| 191 |
bigip_provision_async: |
| 192 |
module: "vcmp" |
| 193 |
level: "dedicated" |
| 194 |
check_status: true |
| 195 |
status_timeout: 900 |
| 196 |
provider: |
| 197 |
server: lb.mydomain.com |
| 198 |
password: secret |
| 199 |
user: admin |
| 200 |
delegate_to: localhost |
| 201 |
register: status |
| 202 |
|
| 203 |
- name: Wait for 10 minutes if device is restarting services |
| 204 |
bigip_wait: |
| 205 |
timeout: 600 |
| 206 |
type: vcmp |
| 207 |
provider: |
| 208 |
server: lb.mydomain.com |
| 209 |
password: secret |
| 210 |
user: admin |
| 211 |
delegate_to: localhost |
| 212 |
when: |
| 213 |
- result.message == "Device is restarting services, unable to check provisioning status." |
| 214 |
|
| 215 |
- name: Re-check for provision progress |
| 216 |
bigip_provision_async: |
| 217 |
module: "vcmp" |
| 218 |
level: "dedicated" |
| 219 |
check_status: true |
| 220 |
status_timeout: 900 |
| 221 |
provider: |
| 222 |
server: lb.mydomain.com |
| 223 |
password: secret |
| 224 |
user: admin |
| 225 |
delegate_to: localhost |
| 226 |
register: status |
| 227 |
when: |
| 228 |
- status.message == "Device is restarting services, unable to check provisioning status." |
| 229 |
|
| 230 |
- name: Provision VCMP on the device - Idempotent Check |
| 231 |
bigip_provision_async: |
| 232 |
module: "vcmp" |
| 233 |
level: "dedicated" |
| 234 |
provider: |
| 235 |
server: lb.mydomain.com |
| 236 |
password: secret |
| 237 |
user: admin |
| 238 |
register: result |
| 239 |
delegate_to: localhost |
| 240 |
when: |
| 241 |
- status.message == "Device has finished provisioning the requested module." |
| 242 |
|
| 243 |
- name: Assert Provision VCMP on the device - Idempotent Check |
| 244 |
assert: |
| 245 |
that: |
| 246 |
- result is not changed |
| 247 |
when: |
| 248 |
- status.message == "Device has finished provisioning the requested module." |
| 249 |
|
| 250 |
- name: De-provision VCMP on the device |
| 251 |
bigip_provision_async: |
| 252 |
module: "vcmp" |
| 253 |
state: "absent" |
| 254 |
provider: |
| 255 |
server: lb.mydomain.com |
| 256 |
password: secret |
| 257 |
user: admin |
| 258 |
|
| 259 |
- name: Check for de-provision progress |
| 260 |
bigip_provision_async: |
| 261 |
module: "vcmp" |
| 262 |
state: "absent" |
| 263 |
check_status: true |
| 264 |
status_timeout: 900 |
| 265 |
provider: |
| 266 |
server: lb.mydomain.com |
| 267 |
password: secret |
| 268 |
user: admin |
| 269 |
register: status |
| 270 |
|
| 271 |
- name: Wait for 10 minutes if device is restarting services |
| 272 |
bigip_wait: |
| 273 |
timeout: 600 |
| 274 |
provider: |
| 275 |
server: lb.mydomain.com |
| 276 |
password: secret |
| 277 |
user: admin |
| 278 |
when: |
| 279 |
- result.message == "Device is restarting services, unable to check provisioning status." |
| 280 |
|
| 281 |
- name: Re-check for de-provision progress |
| 282 |
bigip_provision_async: |
| 283 |
module: "vcmp" |
| 284 |
state: "absent" |
| 285 |
check_status: true |
| 286 |
status_timeout: 900 |
| 287 |
provider: |
| 288 |
server: lb.mydomain.com |
| 289 |
password: secret |
| 290 |
user: admin |
| 291 |
register: status |
| 292 |
when: |
| 293 |
- status.message == "Device is restarting services, unable to check provisioning status." |
| 294 |
|
| 295 |
- name: De-provision VCMP on the device - Idempotent Check |
| 296 |
bigip_provision_async: |
| 297 |
module: "vcmp" |
| 298 |
state: "absent" |
| 299 |
provider: |
| 300 |
server: lb.mydomain.com |
| 301 |
password: secret |
| 302 |
user: admin |
| 303 |
register: result |
| 304 |
when: |
| 305 |
- status.message == "Device has finished de-provisioning the requested module." |
| 306 |
|
| 307 |
- name: Assert Provision VCMP on the device - Idempotent Check |
| 308 |
assert: |
| 309 |
that: |
| 310 |
- result is not changed |
| 311 |
when: |
| 312 |
- status.message == "Device has finished de-provisioning the requested module." |
| 313 |
''' |
| 314 |
|
| 315 |
RETURN = r''' |
| 316 |
level: |
| 317 |
description: The new provisioning level of the module. |
| 318 |
returned: changed |
| 319 |
type: str |
| 320 |
sample: minimum |
| 321 |
memory: |
| 322 |
description: The new provisioned amount of memory for the mgmt module. |
| 323 |
returned: changed |
| 324 |
type: str |
| 325 |
sample: large |
| 326 |
message: |
| 327 |
description: Informative message of the ansible task status. |
| 328 |
returned: changed |
| 329 |
type: dict |
| 330 |
sample: hash/dictionary of values |
| 331 |
''' |
| 332 |
import time |
| 333 |
import traceback |
| 334 |
from datetime import datetime |
| 335 |
|
| 336 |
try: |
| 337 |
from packaging.version import Version |
| 338 |
except ImportError: |
| 339 |
HAS_PACKAGING = False |
| 340 |
Version = None |
| 341 |
PACKAGING_IMPORT_ERROR = traceback.format_exc() |
| 342 |
else: |
| 343 |
HAS_PACKAGING = True |
| 344 |
PACKAGING_IMPORT_ERROR = None |
| 345 |
|
| 346 |
from ansible.module_utils.basic import ( |
| 347 |
AnsibleModule, missing_required_lib |
| 348 |
) |
| 349 |
|
| 350 |
from ..module_utils.bigip import F5RestClient |
| 351 |
from ..module_utils.common import ( |
| 352 |
F5ModuleError, AnsibleF5Parameters, f5_argument_spec, |
| 353 |
) |
| 354 |
from ..module_utils.icontrol import ( |
| 355 |
TransactionContextManager, tmos_version |
| 356 |
) |
| 357 |
from ..module_utils.teem import send_teem |
| 358 |
|
| 359 |
|
| 360 |
class Parameters(AnsibleF5Parameters): |
| 361 |
api_map = { |
| 362 |
'value': 'memory', |
| 363 |
} |
| 364 |
|
| 365 |
api_attributes = [ |
| 366 |
'level', |
| 367 |
'value', |
| 368 |
] |
| 369 |
|
| 370 |
returnables = [ |
| 371 |
'level', |
| 372 |
'memory', |
| 373 |
'message', |
| 374 |
] |
| 375 |
|
| 376 |
updatables = [ |
| 377 |
'level', |
| 378 |
'cgnat', |
| 379 |
'memory', |
| 380 |
] |
| 381 |
|
| 382 |
|
| 383 |
class ApiParameters(Parameters): |
| 384 |
pass |
| 385 |
|
| 386 |
|
| 387 |
class ModuleParameters(Parameters): |
| 388 |
|
| 389 |
def _validate_memory_limit(self, limit): |
| 390 |
if self._values['memory'] == 'small': |
| 391 |
return '0' |
| 392 |
if self._values['memory'] == 'medium': |
| 393 |
return '200' |
| 394 |
if self._values['memory'] == 'large': |
| 395 |
return '500' |
| 396 |
if 0 <= int(limit) <= 8192: |
| 397 |
return str(limit) |
| 398 |
raise F5ModuleError( |
| 399 |
"Valid 'memory' must be in range 0 - 8192, 'small', 'medium', or 'large'." |
| 400 |
) |
| 401 |
|
| 402 |
@property |
| 403 |
def level(self): |
| 404 |
if self._values['level'] is None: |
| 405 |
return None |
| 406 |
if self._values['module'] == 'mgmt': |
| 407 |
return None |
| 408 |
if self.state == 'absent': |
| 409 |
return 'none' |
| 410 |
return str(self._values['level']) |
| 411 |
|
| 412 |
@property |
| 413 |
def memory(self): |
| 414 |
if self._values['memory'] is None: |
| 415 |
return None |
| 416 |
if self._values['module'] != 'mgmt': |
| 417 |
return None |
| 418 |
return int(self._validate_memory_limit(self._values['memory'])) |
| 419 |
|
| 420 |
@property |
| 421 |
def status_timeout(self): |
| 422 |
divisor = 100 |
| 423 |
timeout = self._values['status_timeout'] |
| 424 |
if timeout < 150 or timeout > 3600: |
| 425 |
raise F5ModuleError( |
| 426 |
"Timeout value must be between 150 and 3600 seconds." |
| 427 |
) |
| 428 |
|
| 429 |
delay = timeout / divisor |
| 430 |
|
| 431 |
return delay, divisor |
| 432 |
|
| 433 |
|
| 434 |
class Changes(Parameters): |
| 435 |
def to_return(self): |
| 436 |
result = {} |
| 437 |
try: |
| 438 |
for returnable in self.returnables: |
| 439 |
result[returnable] = getattr(self, returnable) |
| 440 |
result = self._filter_params(result) |
| 441 |
return result |
| 442 |
except Exception: |
| 443 |
return result |
| 444 |
|
| 445 |
|
| 446 |
class UsableChanges(Changes): |
| 447 |
pass |
| 448 |
|
| 449 |
|
| 450 |
class ReportableChanges(Changes): |
| 451 |
@property |
| 452 |
def memory(self): |
| 453 |
if self._values['memory'] is None: |
| 454 |
return None |
| 455 |
if self._values['memory'] == '0': |
| 456 |
return 'small' |
| 457 |
if self._values['memory'] == '200': |
| 458 |
return 'medium' |
| 459 |
if self._values['memory'] == '500': |
| 460 |
return 'large' |
| 461 |
return str(self._values['memory']) |
| 462 |
|
| 463 |
|
| 464 |
class Difference(object): |
| 465 |
def __init__(self, want, have=None): |
| 466 |
self.want = want |
| 467 |
self.have = have |
| 468 |
|
| 469 |
def compare(self, param): |
| 470 |
try: |
| 471 |
result = getattr(self, param) |
| 472 |
return result |
| 473 |
except AttributeError: |
| 474 |
result = self.__default(param) |
| 475 |
return result |
| 476 |
|
| 477 |
def __default(self, param): |
| 478 |
attr1 = getattr(self.want, param) |
| 479 |
try: |
| 480 |
attr2 = getattr(self.have, param) |
| 481 |
if attr1 != attr2: |
| 482 |
return attr1 |
| 483 |
except AttributeError: |
| 484 |
return attr1 |
| 485 |
|
| 486 |
@property |
| 487 |
def cgnat(self): |
| 488 |
if self.want.module == 'cgnat': |
| 489 |
if self.want.state == 'absent' and self.have.enabled is True: |
| 490 |
return True |
| 491 |
if self.want.state == 'present' and self.have.disabled is True: |
| 492 |
return True |
| 493 |
|
| 494 |
|
| 495 |
class ModuleManager(object): |
| 496 |
def __init__(self, *args, **kwargs): |
| 497 |
self.module = kwargs.get('module', None) |
| 498 |
self.client = F5RestClient(**self.module.params) |
| 499 |
self.have = None |
| 500 |
self.want = ModuleParameters(params=self.module.params) |
| 501 |
self.changes = UsableChanges() |
| 502 |
|
| 503 |
def _update_changed_options(self): |
| 504 |
diff = Difference(self.want, self.have) |
| 505 |
updatables = Parameters.updatables |
| 506 |
changed = dict() |
| 507 |
for k in updatables: |
| 508 |
change = diff.compare(k) |
| 509 |
if change is None: |
| 510 |
continue |
| 511 |
else: |
| 512 |
if isinstance(change, dict): |
| 513 |
changed.update(change) |
| 514 |
else: |
| 515 |
changed[k] = change |
| 516 |
if changed: |
| 517 |
self.changes = UsableChanges(params=changed) |
| 518 |
return True |
| 519 |
return False |
| 520 |
|
| 521 |
def exec_module(self): |
| 522 |
start = datetime.now().isoformat() |
| 523 |
version = tmos_version(self.client) |
| 524 |
|
| 525 |
if Version(version) < Version('15.0.0'): |
| 526 |
raise F5ModuleError("This module can only be used on BIG-IP with TMOS version 15.x and above.") |
| 527 |
|
| 528 |
changed = False |
| 529 |
result = dict() |
| 530 |
state = self.want.state |
| 531 |
|
| 532 |
if state == "present": |
| 533 |
changed = self.present() |
| 534 |
elif state == "absent": |
| 535 |
changed = self.absent() |
| 536 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 537 |
changes = reportable.to_return() |
| 538 |
result.update(**changes) |
| 539 |
result.update(dict(changed=changed)) |
| 540 |
send_teem(start, self.client, self.module, version) |
| 541 |
return result |
| 542 |
|
| 543 |
def present(self): |
| 544 |
if self.want.check_status is True: |
| 545 |
return self.check_progress() |
| 546 |
if self.exists(): |
| 547 |
return False |
| 548 |
return self.update() |
| 549 |
|
| 550 |
def mgmt_exists(self): |
| 551 |
uri = "https://{0}:{1}/mgmt/tm/sys/db/provision.extramb/".format( |
| 552 |
self.client.provider['server'], |
| 553 |
self.client.provider['server_port'], |
| 554 |
) |
| 555 |
resp = self.client.api.get(uri) |
| 556 |
try: |
| 557 |
response = resp.json() |
| 558 |
except ValueError as ex: |
| 559 |
raise F5ModuleError(str(ex)) |
| 560 |
|
| 561 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 562 |
return False |
| 563 |
if int(response['value']) != 0 and self.want.memory == 0: |
| 564 |
return False |
| 565 |
if int(response['value']) == 0 and self.want.memory == 0: |
| 566 |
return True |
| 567 |
if int(response['value']) == self.want.memory: |
| 568 |
return True |
| 569 |
return False |
| 570 |
|
| 571 |
def exists(self): |
| 572 |
if self.want.module == 'mgmt': |
| 573 |
return self.mgmt_exists() |
| 574 |
|
| 575 |
uri = "https://{0}:{1}/mgmt/tm/sys/provision/".format( |
| 576 |
self.client.provider['server'], |
| 577 |
self.client.provider['server_port'] |
| 578 |
) |
| 579 |
resp = self.client.api.get(uri) |
| 580 |
try: |
| 581 |
response = resp.json() |
| 582 |
except ValueError as ex: |
| 583 |
raise F5ModuleError(str(ex)) |
| 584 |
|
| 585 |
if 'code' in response and response['code'] in [400, 404]: |
| 586 |
if 'message' in response: |
| 587 |
raise F5ModuleError(response['message']) |
| 588 |
else: |
| 589 |
raise F5ModuleError(resp.content) |
| 590 |
|
| 591 |
for module in response['items']: |
| 592 |
if module['name'] == self.want.module: |
| 593 |
if module['level'] != 'none' and self.want.level == 'none': |
| 594 |
return True |
| 595 |
if module['level'] == 'none' and self.want.level == 'none': |
| 596 |
return False |
| 597 |
if module['level'] == self.want.level: |
| 598 |
return True |
| 599 |
return False |
| 600 |
|
| 601 |
def update(self): |
| 602 |
self.have = self.read_current_from_device() |
| 603 |
if not self.should_update(): |
| 604 |
return False |
| 605 |
if self.module.check_mode: |
| 606 |
return True |
| 607 |
self.update_on_device() |
| 608 |
if self._device_is_rebooting(): |
| 609 |
self.changes.update( |
| 610 |
{'message': "Device goes down for a reboot and will start provisioning requested module: {0}, " |
| 611 |
"recheck the status by re-running the module with 'check_status' parameter.".format(self.want.module)} |
| 612 |
) |
| 613 |
else: |
| 614 |
self.changes.update( |
| 615 |
{'message': "Device will start provisioning requested module: {0}, " |
| 616 |
"recheck the status by re-running the module with 'check_status' parameter.".format(self.want.module)} |
| 617 |
) |
| 618 |
return True |
| 619 |
|
| 620 |
def check_progress(self): |
| 621 |
if self._device_is_rebooting(): |
| 622 |
self.changes.update( |
| 623 |
{'message': 'Device is rebooting, unable to check provisioning status.'} |
| 624 |
) |
| 625 |
if not self.device_is_ready(): |
| 626 |
self.changes.update( |
| 627 |
{'message': 'Device is restarting services, unable to check provisioning status.'} |
| 628 |
) |
| 629 |
return False |
| 630 |
return self.wait_for_provisioning() |
| 631 |
|
| 632 |
def device_is_ready(self): |
| 633 |
try: |
| 634 |
if self._is_rest_available(): |
| 635 |
return True |
| 636 |
except Exception as ex: |
| 637 |
|
| 638 |
|
| 639 |
if 'Failed to validate the SSL' in str(ex): |
| 640 |
raise F5ModuleError(str(ex)) |
| 641 |
pass |
| 642 |
return False |
| 643 |
|
| 644 |
def wait_for_provisioning(self): |
| 645 |
delay, period = self.want.status_timeout |
| 646 |
checks = 0 |
| 647 |
for x in range(0, period): |
| 648 |
|
| 649 |
if not self.device_is_ready(): |
| 650 |
self.changes.update( |
| 651 |
{'message': 'Device is restarting services, unable to check provisioning status.'} |
| 652 |
) |
| 653 |
return False |
| 654 |
if not self._is_mprov_running_on_device(): |
| 655 |
checks += 1 |
| 656 |
if checks > 2: |
| 657 |
if self.want.state == 'absent': |
| 658 |
self.changes.update( |
| 659 |
{'message': 'Device has finished de-provisioning the requested module.'} |
| 660 |
) |
| 661 |
|
| 662 |
else: |
| 663 |
self.changes.update( |
| 664 |
{'message': 'Device has finished provisioning the requested module.'} |
| 665 |
) |
| 666 |
return True |
| 667 |
time.sleep(delay) |
| 668 |
raise F5ModuleError( |
| 669 |
"Module timeout reached, state change is unknown, " |
| 670 |
"please increase the status_timeout parameter for long lived actions." |
| 671 |
) |
| 672 |
|
| 673 |
def _is_rest_available(self): |
| 674 |
uri = "https://{0}:{1}/mgmt/tm/sys/available".format( |
| 675 |
self.client.provider['server'], |
| 676 |
self.client.provider['server_port'] |
| 677 |
) |
| 678 |
resp = self.client.api.get(uri) |
| 679 |
try: |
| 680 |
response = resp.json() |
| 681 |
except ValueError: |
| 682 |
return False |
| 683 |
|
| 684 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 685 |
return True |
| 686 |
return False |
| 687 |
|
| 688 |
def _is_mprov_running_on_device(self): |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
command = "ps aux | grep \'[m]prov\' | grep -v /usr/libexec/qemu-kvm" |
| 700 |
params = dict( |
| 701 |
command="run", |
| 702 |
utilCmdArgs='-c "{0}"'.format(command) |
| 703 |
) |
| 704 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 705 |
self.client.provider['server'], |
| 706 |
self.client.provider['server_port'] |
| 707 |
) |
| 708 |
resp = self.client.api.post(uri, json=params) |
| 709 |
|
| 710 |
try: |
| 711 |
response = resp.json() |
| 712 |
except ValueError: |
| 713 |
return False |
| 714 |
|
| 715 |
if 'code' in response and response['code'] in [400, 403]: |
| 716 |
return False |
| 717 |
if 'commandResult' in response: |
| 718 |
return True |
| 719 |
return False |
| 720 |
|
| 721 |
def _device_is_rebooting(self): |
| 722 |
params = { |
| 723 |
"command": "run", |
| 724 |
"utilCmdArgs": '-c "runlevel"' |
| 725 |
} |
| 726 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 727 |
self.client.provider['server'], |
| 728 |
self.client.provider['server_port'] |
| 729 |
) |
| 730 |
resp = self.client.api.post(uri, json=params) |
| 731 |
try: |
| 732 |
response = resp.json() |
| 733 |
except ValueError: |
| 734 |
|
| 735 |
|
| 736 |
return False |
| 737 |
|
| 738 |
if resp.status not in [200, 201] or 'code' in response and response['code'] not in [200, 201]: |
| 739 |
raise F5ModuleError(resp.content) |
| 740 |
|
| 741 |
if 'commandResult' in response and '6' in response['commandResult']: |
| 742 |
return True |
| 743 |
return False |
| 744 |
|
| 745 |
def should_update(self): |
| 746 |
result = self._update_changed_options() |
| 747 |
if result: |
| 748 |
return True |
| 749 |
return False |
| 750 |
|
| 751 |
def update_on_device(self): |
| 752 |
if self.want.level == 'dedicated' and self.want.module != 'mgmt': |
| 753 |
self.provision_dedicated_on_device() |
| 754 |
else: |
| 755 |
self.provision_non_dedicated_on_device() |
| 756 |
|
| 757 |
def _read_other_modules_from_device(self, uri): |
| 758 |
resp = self.client.api.get(uri) |
| 759 |
try: |
| 760 |
response = resp.json() |
| 761 |
except ValueError as ex: |
| 762 |
raise F5ModuleError(str(ex)) |
| 763 |
|
| 764 |
if 'code' in response and response['code'] in [400, 404]: |
| 765 |
if 'message' in response: |
| 766 |
raise F5ModuleError(response['message']) |
| 767 |
else: |
| 768 |
raise F5ModuleError(resp.content) |
| 769 |
return [x['name'] for x in response['items'] if x['name'] != self.want.module] |
| 770 |
|
| 771 |
def provision_dedicated_on_device(self): |
| 772 |
params = self.want.api_params() |
| 773 |
uri = "https://{0}:{1}/mgmt/tm/sys/provision/".format( |
| 774 |
self.client.provider['server'], |
| 775 |
self.client.provider['server_port'], |
| 776 |
) |
| 777 |
|
| 778 |
resources = self._read_other_modules_from_device(uri) |
| 779 |
|
| 780 |
with TransactionContextManager(self.client) as transact: |
| 781 |
for resource in resources: |
| 782 |
target = uri + resource |
| 783 |
resp = transact.api.patch(target, json=dict(level='none')) |
| 784 |
try: |
| 785 |
response = resp.json() |
| 786 |
except ValueError as ex: |
| 787 |
raise F5ModuleError(str(ex)) |
| 788 |
|
| 789 |
if 'code' in response and response['code'] in [400, 404]: |
| 790 |
if 'message' in response: |
| 791 |
raise F5ModuleError(response['message']) |
| 792 |
else: |
| 793 |
raise F5ModuleError(resp.content) |
| 794 |
|
| 795 |
target = uri + self.want.module |
| 796 |
resp = transact.api.patch(target, json=params) |
| 797 |
try: |
| 798 |
response = resp.json() |
| 799 |
except ValueError as ex: |
| 800 |
raise F5ModuleError(str(ex)) |
| 801 |
|
| 802 |
if 'code' in response and response['code'] in [400, 404]: |
| 803 |
if 'message' in response: |
| 804 |
raise F5ModuleError(response['message']) |
| 805 |
else: |
| 806 |
raise F5ModuleError(resp.content) |
| 807 |
|
| 808 |
def provision_mgmt_on_device(self, params): |
| 809 |
uri = "https://{0}:{1}/mgmt/tm/sys/db/provision.extramb/".format( |
| 810 |
self.client.provider['server'], |
| 811 |
self.client.provider['server_port'], |
| 812 |
) |
| 813 |
resp = self.client.api.patch(uri, json=params) |
| 814 |
try: |
| 815 |
response = resp.json() |
| 816 |
except ValueError as ex: |
| 817 |
raise F5ModuleError(str(ex)) |
| 818 |
|
| 819 |
if 'code' in response and response['code'] in [400, 404]: |
| 820 |
if 'message' in response: |
| 821 |
raise F5ModuleError(response['message']) |
| 822 |
else: |
| 823 |
raise F5ModuleError(resp.content) |
| 824 |
|
| 825 |
def provision_non_dedicated_on_device(self): |
| 826 |
params = self.want.api_params() |
| 827 |
|
| 828 |
if self.want.module == 'mgmt': |
| 829 |
return self.provision_mgmt_on_device(params) |
| 830 |
|
| 831 |
uri = "https://{0}:{1}/mgmt/tm/sys/provision/{2}".format( |
| 832 |
self.client.provider['server'], |
| 833 |
self.client.provider['server_port'], |
| 834 |
self.want.module |
| 835 |
) |
| 836 |
resp = self.client.api.patch(uri, json=params) |
| 837 |
try: |
| 838 |
response = resp.json() |
| 839 |
except ValueError as ex: |
| 840 |
raise F5ModuleError(str(ex)) |
| 841 |
|
| 842 |
if 'code' in response and response['code'] in [400, 404]: |
| 843 |
if 'message' in response: |
| 844 |
raise F5ModuleError(response['message']) |
| 845 |
else: |
| 846 |
raise F5ModuleError(resp.content) |
| 847 |
|
| 848 |
def read_mgmt_from_device(self): |
| 849 |
uri = "https://{0}:{1}/mgmt/tm/sys/db/provision.extramb/".format( |
| 850 |
self.client.provider['server'], |
| 851 |
self.client.provider['server_port'], |
| 852 |
) |
| 853 |
resp = self.client.api.get(uri) |
| 854 |
try: |
| 855 |
response = resp.json() |
| 856 |
except ValueError as ex: |
| 857 |
raise F5ModuleError(str(ex)) |
| 858 |
|
| 859 |
if 'code' in response and response['code'] in [400, 404]: |
| 860 |
if 'message' in response: |
| 861 |
raise F5ModuleError(response['message']) |
| 862 |
else: |
| 863 |
raise F5ModuleError(resp.content) |
| 864 |
return ApiParameters(params=response) |
| 865 |
|
| 866 |
def read_current_from_device(self): |
| 867 |
if self.want.module == 'mgmt': |
| 868 |
return self.read_mgmt_from_device() |
| 869 |
|
| 870 |
uri = "https://{0}:{1}/mgmt/tm/sys/provision/{2}".format( |
| 871 |
self.client.provider['server'], |
| 872 |
self.client.provider['server_port'], |
| 873 |
self.want.module |
| 874 |
) |
| 875 |
resp = self.client.api.get(uri) |
| 876 |
try: |
| 877 |
response = resp.json() |
| 878 |
except ValueError as ex: |
| 879 |
raise F5ModuleError(str(ex)) |
| 880 |
|
| 881 |
if 'code' in response and response['code'] in [400, 404]: |
| 882 |
if 'message' in response: |
| 883 |
raise F5ModuleError(response['message']) |
| 884 |
else: |
| 885 |
raise F5ModuleError(resp.content) |
| 886 |
return ApiParameters(params=response) |
| 887 |
|
| 888 |
def absent(self): |
| 889 |
if self.want.check_status is True: |
| 890 |
return self.check_removal_progress() |
| 891 |
if self.exists(): |
| 892 |
return self.remove() |
| 893 |
return False |
| 894 |
|
| 895 |
def remove(self): |
| 896 |
if self.module.check_mode: |
| 897 |
return True |
| 898 |
self.remove_from_device() |
| 899 |
if self._device_is_rebooting(): |
| 900 |
self.changes.update( |
| 901 |
{'message': "Device goes down for a reboot and will start de-provisioning requested module: {0}, " |
| 902 |
"recheck the status by re-running the module with 'check_status' parameter.".format(self.want.module)} |
| 903 |
) |
| 904 |
else: |
| 905 |
self.changes.update( |
| 906 |
{'message': "Device will start de-provisioning requested module: {0}, " |
| 907 |
"recheck the status by re-running the module with 'check_status' parameter.".format(self.want.module)} |
| 908 |
) |
| 909 |
return True |
| 910 |
|
| 911 |
def check_removal_progress(self): |
| 912 |
done = self.check_progress() |
| 913 |
if done: |
| 914 |
if self.should_reboot(): |
| 915 |
self.save_on_device() |
| 916 |
self.changes.update( |
| 917 |
{'message': "Device finished de-provisioning requested module: {0} " |
| 918 |
"and configuration has been saved, a reboot is required.".format(self.want.module)} |
| 919 |
) |
| 920 |
return True |
| 921 |
return False |
| 922 |
|
| 923 |
def should_reboot(self): |
| 924 |
|
| 925 |
for x in range(0, 3): |
| 926 |
uri = "https://{0}:{1}/mgmt/tm/sys/db/{2}".format( |
| 927 |
self.client.provider['server'], |
| 928 |
self.client.provider['server_port'], |
| 929 |
'provision.action' |
| 930 |
) |
| 931 |
resp = self.client.api.get(uri) |
| 932 |
try: |
| 933 |
response = resp.json() |
| 934 |
except ValueError as ex: |
| 935 |
raise F5ModuleError(str(ex)) |
| 936 |
|
| 937 |
if 'code' in response and response['code'] in [400, 404]: |
| 938 |
if 'message' in response: |
| 939 |
raise F5ModuleError(response['message']) |
| 940 |
else: |
| 941 |
raise F5ModuleError(resp.content) |
| 942 |
|
| 943 |
if response['value'] == 'reboot': |
| 944 |
return True |
| 945 |
elif response['value'] == 'none': |
| 946 |
time.sleep(5) |
| 947 |
return False |
| 948 |
|
| 949 |
def save_on_device(self): |
| 950 |
command = 'tmsh save sys config' |
| 951 |
params = dict( |
| 952 |
command="run", |
| 953 |
utilCmdArgs='-c "{0}"'.format(command) |
| 954 |
) |
| 955 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 956 |
self.client.provider['server'], |
| 957 |
self.client.provider['server_port'] |
| 958 |
) |
| 959 |
resp = self.client.api.post(uri, json=params) |
| 960 |
|
| 961 |
try: |
| 962 |
response = resp.json() |
| 963 |
except ValueError as ex: |
| 964 |
raise F5ModuleError(str(ex)) |
| 965 |
|
| 966 |
if 'code' in response and response['code'] in [400, 403]: |
| 967 |
if 'message' in response: |
| 968 |
raise F5ModuleError(response['message']) |
| 969 |
else: |
| 970 |
raise F5ModuleError(resp.content) |
| 971 |
|
| 972 |
def remove_from_device(self): |
| 973 |
uri = "https://{0}:{1}/mgmt/tm/sys/provision/{2}".format( |
| 974 |
self.client.provider['server'], |
| 975 |
self.client.provider['server_port'], |
| 976 |
self.want.module |
| 977 |
) |
| 978 |
resp = self.client.api.patch(uri, json=dict(level='none')) |
| 979 |
|
| 980 |
try: |
| 981 |
response = resp.json() |
| 982 |
except ValueError as ex: |
| 983 |
raise F5ModuleError(str(ex)) |
| 984 |
|
| 985 |
if 'code' in response and response['code'] == 400: |
| 986 |
if 'message' in response: |
| 987 |
raise F5ModuleError(response['message']) |
| 988 |
else: |
| 989 |
raise F5ModuleError(resp.content) |
| 990 |
|
| 991 |
|
| 992 |
class ArgumentSpec(object): |
| 993 |
def __init__(self): |
| 994 |
self.supports_check_mode = True |
| 995 |
argument_spec = dict( |
| 996 |
module=dict( |
| 997 |
required=True, |
| 998 |
choices=[ |
| 999 |
'afm', 'am', 'apm', 'asm', 'avr', 'cgnat', |
| 1000 |
'fps', 'gtm', 'ilx', 'lc', 'ltm', 'mgmt', |
| 1001 |
'pem', 'sam', 'sslo', 'swg', 'urldb', 'vcmp' |
| 1002 |
], |
| 1003 |
aliases=['name'] |
| 1004 |
), |
| 1005 |
check_status=dict( |
| 1006 |
type='bool', |
| 1007 |
default='no' |
| 1008 |
), |
| 1009 |
status_timeout=dict( |
| 1010 |
type='int', |
| 1011 |
default=300 |
| 1012 |
), |
| 1013 |
level=dict( |
| 1014 |
default='nominal', |
| 1015 |
choices=['nominal', 'dedicated', 'minimum'] |
| 1016 |
), |
| 1017 |
memory=dict(), |
| 1018 |
state=dict( |
| 1019 |
default='present', |
| 1020 |
choices=['present', 'absent'] |
| 1021 |
) |
| 1022 |
) |
| 1023 |
self.argument_spec = {} |
| 1024 |
self.argument_spec.update(f5_argument_spec) |
| 1025 |
self.argument_spec.update(argument_spec) |
| 1026 |
self.required_if = [ |
| 1027 |
['check_status', 'yes', ['module']] |
| 1028 |
] |
| 1029 |
|
| 1030 |
|
| 1031 |
def main(): |
| 1032 |
spec = ArgumentSpec() |
| 1033 |
|
| 1034 |
module = AnsibleModule( |
| 1035 |
argument_spec=spec.argument_spec, |
| 1036 |
supports_check_mode=spec.supports_check_mode, |
| 1037 |
required_if=spec.required_if, |
| 1038 |
|
| 1039 |
) |
| 1040 |
|
| 1041 |
if not HAS_PACKAGING: |
| 1042 |
module.fail_json( |
| 1043 |
msg=missing_required_lib('packaging'), |
| 1044 |
exception=PACKAGING_IMPORT_ERROR |
| 1045 |
) |
| 1046 |
|
| 1047 |
try: |
| 1048 |
mm = ModuleManager(module=module) |
| 1049 |
results = mm.exec_module() |
| 1050 |
module.exit_json(**results) |
| 1051 |
except F5ModuleError as ex: |
| 1052 |
module.fail_json(msg=str(ex)) |
| 1053 |
|
| 1054 |
|
| 1055 |
if __name__ == '__main__': |
| 1056 |
main() |