| 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_device_license |
| 13 |
short_description: Manage license installation and activation on BIG-IP devices |
| 14 |
description: |
| 15 |
- Manage license installation and activation on a BIG-IP. |
| 16 |
version_added: "1.0.0" |
| 17 |
options: |
| 18 |
license_key: |
| 19 |
description: |
| 20 |
- The registration key to use to license the BIG-IP. |
| 21 |
- This parameter is required if the C(state) is equal to C(present) or C(latest). |
| 22 |
- This parameter is not required when C(state) is C(absent) and will be |
| 23 |
ignored if it is provided. |
| 24 |
type: str |
| 25 |
addon_keys: |
| 26 |
description: |
| 27 |
- The list of addon keys to use to in conjunction with the base license. |
| 28 |
- This parameter will be ignored if no C(license_key) is provided. |
| 29 |
- This parameter is not required when C(state) is C(absent) and will be |
| 30 |
ignored if it is provided. |
| 31 |
type: list |
| 32 |
elements: str |
| 33 |
version_added: "1.2.0" |
| 34 |
license_server: |
| 35 |
description: |
| 36 |
- The F5 license server to use when getting a license and validating a dossier. |
| 37 |
- This parameter is required if the C(state) is equal to C(present) or C(latest). |
| 38 |
- This parameter is not required when C(state) is C(absent) and will be |
| 39 |
ignored if it is provided. |
| 40 |
type: str |
| 41 |
default: activate.f5.com |
| 42 |
state: |
| 43 |
description: |
| 44 |
- The state of the license on the system. |
| 45 |
- When C(present), only guarantees that a license exists. |
| 46 |
- When C(absent), removes the license on the system. |
| 47 |
- When C(latest), ensures that the license is always valid. This is not idempotent state since re-run can modify result. |
| 48 |
- When C(revoked), removes the license on the system and revokes its future usage |
| 49 |
on the F5 license servers. |
| 50 |
type: str |
| 51 |
choices: |
| 52 |
- absent |
| 53 |
- latest |
| 54 |
- present |
| 55 |
- revoked |
| 56 |
default: present |
| 57 |
accept_eula: |
| 58 |
description: |
| 59 |
- Declares whether you accept the BIG-IP EULA or not. By default, this |
| 60 |
value is C(false). You must specifically declare you have viewed and |
| 61 |
accepted the license. This module does not present you with the EULA, |
| 62 |
so it is incumbent on you to read it. |
| 63 |
- The EULA can be found here; https://support.f5.com/csp/article/K12902. |
| 64 |
- This parameter is not required when C(state) is C(absent) and will be |
| 65 |
ignored if it is provided. |
| 66 |
type: bool |
| 67 |
default: false |
| 68 |
force: |
| 69 |
description: |
| 70 |
- Declares whether to force license renewal. By default, this |
| 71 |
value is C(false). |
| 72 |
- This parameter is not required and will be ignored if it is provided. |
| 73 |
type: bool |
| 74 |
default: false |
| 75 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 76 |
notes: |
| 77 |
- This module can be used to license BIG-IPs that do not have access to internet. |
| 78 |
- Only the Ansible Controller needs internet access as the license activation is done on the Ansible Controller from |
| 79 |
which the module is running. |
| 80 |
author: |
| 81 |
- Tim Rupp (@caphrim007) |
| 82 |
- Wojciech Wypior (@wojtek0806) |
| 83 |
- Andrey Kashcheev (@andreykashcheev) |
| 84 |
''' |
| 85 |
|
| 86 |
EXAMPLES = ''' |
| 87 |
- name: License BIG-IP using a key |
| 88 |
bigip_device_license: |
| 89 |
license_key: "XXXXX-XXXXX-XXXXX-XXXXX-XXXXXXX" |
| 90 |
provider: |
| 91 |
server: "lb.mydomain.com" |
| 92 |
user: "admin" |
| 93 |
password: "secret" |
| 94 |
delegate_to: localhost |
| 95 |
|
| 96 |
- name: License BIG-IP using a key |
| 97 |
bigip_device_license: |
| 98 |
license_key: "XXXXX-XXXXX-XXXXX-XXXXX-XXXXXXX" |
| 99 |
provider: |
| 100 |
server: "lb.mydomain.com" |
| 101 |
user: "admin" |
| 102 |
password: "secret" |
| 103 |
delegate_to: localhost |
| 104 |
|
| 105 |
- name: Remove the license from the system |
| 106 |
bigip_device_license: |
| 107 |
state: "absent" |
| 108 |
provider: |
| 109 |
server: "lb.mydomain.com" |
| 110 |
user: "admin" |
| 111 |
password: "secret" |
| 112 |
delegate_to: localhost |
| 113 |
''' |
| 114 |
|
| 115 |
RETURN = r''' |
| 116 |
# only common fields returned |
| 117 |
''' |
| 118 |
import re |
| 119 |
import time |
| 120 |
import xml.etree.ElementTree |
| 121 |
from datetime import datetime |
| 122 |
|
| 123 |
from ansible.module_utils.basic import AnsibleModule |
| 124 |
from ansible.module_utils.six import iteritems |
| 125 |
|
| 126 |
from ..module_utils.bigip import F5RestClient |
| 127 |
from ..module_utils.common import ( |
| 128 |
F5ModuleError, AnsibleF5Parameters, is_empty_list, f5_argument_spec |
| 129 |
) |
| 130 |
from ..module_utils.icontrol import iControlRestSession |
| 131 |
|
| 132 |
from ..module_utils.teem import send_teem |
| 133 |
|
| 134 |
|
| 135 |
class LicenseXmlParser(object): |
| 136 |
def __init__(self, content=None): |
| 137 |
self.raw_content = content |
| 138 |
try: |
| 139 |
self.content = xml.etree.ElementTree.fromstring(content) |
| 140 |
except xml.etree.ElementTree.ParseError as ex: |
| 141 |
raise F5ModuleError("Provided XML payload is invalid. Received '{0}'.".format(str(ex))) |
| 142 |
|
| 143 |
@property |
| 144 |
def namespaces(self): |
| 145 |
result = { |
| 146 |
'xsi': 'http://www.w3.org/2001/XMLSchema-instance' |
| 147 |
} |
| 148 |
return result |
| 149 |
|
| 150 |
@property |
| 151 |
def eula(self): |
| 152 |
try: |
| 153 |
root = self.content.findall('.//eula', self.namespaces) |
| 154 |
return root[0].text |
| 155 |
except Exception: |
| 156 |
return None |
| 157 |
|
| 158 |
@property |
| 159 |
def license(self): |
| 160 |
try: |
| 161 |
root = self.content.findall('.//license', self.namespaces) |
| 162 |
return root[0].text |
| 163 |
except Exception: |
| 164 |
return None |
| 165 |
|
| 166 |
def find_element(self, value): |
| 167 |
root = self.content.findall('.//multiRef', self.namespaces) |
| 168 |
if len(root) == 0: |
| 169 |
return None |
| 170 |
for elem in root: |
| 171 |
for k, v in iteritems(elem.attrib): |
| 172 |
if value in v: |
| 173 |
return elem |
| 174 |
|
| 175 |
@property |
| 176 |
def state(self): |
| 177 |
elem = self.find_element('TransactionState') |
| 178 |
if elem is not None: |
| 179 |
return elem.text |
| 180 |
|
| 181 |
@property |
| 182 |
def fault_number(self): |
| 183 |
fault = self.get_fault() |
| 184 |
return fault.get('faultNumber', None) |
| 185 |
|
| 186 |
@property |
| 187 |
def fault_text(self): |
| 188 |
fault = self.get_fault() |
| 189 |
return fault.get('faultText', None) |
| 190 |
|
| 191 |
def get_fault(self): |
| 192 |
result = dict() |
| 193 |
|
| 194 |
self.set_result_for_license_fault(result) |
| 195 |
self.set_result_for_general_fault(result) |
| 196 |
|
| 197 |
if 'faultNumber' not in result: |
| 198 |
result['faultNumber'] = None |
| 199 |
return result |
| 200 |
|
| 201 |
def set_result_for_license_fault(self, result): |
| 202 |
root = self.find_element('LicensingFault') |
| 203 |
if root is None: |
| 204 |
return result |
| 205 |
for elem in root: |
| 206 |
if elem.tag == 'faultNumber': |
| 207 |
result['faultNumber'] = elem.text |
| 208 |
elif elem.tag == 'faultText': |
| 209 |
tmp = elem.attrib.get('{http://www.w3.org/2001/XMLSchema-instance}nil', None) |
| 210 |
if tmp == 'true': |
| 211 |
result['faultText'] = None |
| 212 |
else: |
| 213 |
result['faultText'] = elem.text |
| 214 |
|
| 215 |
def set_result_for_general_fault(self, result): |
| 216 |
namespaces = { |
| 217 |
'ns2': 'http://schemas.xmlsoap.org/soap/envelope/' |
| 218 |
} |
| 219 |
root = self.content.findall('.//ns2:Fault', namespaces) |
| 220 |
if len(root) == 0: |
| 221 |
return None |
| 222 |
for elem in root[0]: |
| 223 |
if elem.tag == 'faultstring': |
| 224 |
result['faultText'] = elem.text |
| 225 |
|
| 226 |
def json(self): |
| 227 |
result = dict( |
| 228 |
eula=self.eula or None, |
| 229 |
license=self.license or None, |
| 230 |
state=self.state or None, |
| 231 |
fault_number=self.fault_number, |
| 232 |
fault_text=self.fault_text or None |
| 233 |
) |
| 234 |
return result |
| 235 |
|
| 236 |
|
| 237 |
class Parameters(AnsibleF5Parameters): |
| 238 |
api_map = { |
| 239 |
'licenseEndDateTime': 'license_end_date_time', |
| 240 |
} |
| 241 |
|
| 242 |
api_attributes = [ |
| 243 |
|
| 244 |
] |
| 245 |
|
| 246 |
returnables = [ |
| 247 |
|
| 248 |
] |
| 249 |
|
| 250 |
updatables = [ |
| 251 |
|
| 252 |
] |
| 253 |
|
| 254 |
|
| 255 |
class ApiParameters(Parameters): |
| 256 |
pass |
| 257 |
|
| 258 |
|
| 259 |
class ModuleParameters(Parameters): |
| 260 |
@property |
| 261 |
def license_options(self): |
| 262 |
result = dict( |
| 263 |
eula=self.eula or '', |
| 264 |
email=self.email or '', |
| 265 |
first_name=self.first_name or '', |
| 266 |
last_name=self.last_name or '', |
| 267 |
company=self.company or '', |
| 268 |
phone=self.phone or '', |
| 269 |
job_title=self.job_title or '', |
| 270 |
address=self.address or '', |
| 271 |
city=self.city or '', |
| 272 |
state=self.state or '', |
| 273 |
postal_code=self.postal_code or '', |
| 274 |
country=self.country or '' |
| 275 |
) |
| 276 |
return result |
| 277 |
|
| 278 |
@property |
| 279 |
def license_url(self): |
| 280 |
result = 'https://{0}/license/services/urn:com.f5.license.v5b.ActivationService'.format(self.license_server) |
| 281 |
return result |
| 282 |
|
| 283 |
@property |
| 284 |
def license_envelope(self): |
| 285 |
result = """<?xml version="1.0" encoding="UTF-8"?> |
| 286 |
<SOAP-ENV:Envelope xmlns:ns3="http://www.w3.org/2001/XMLSchema" |
| 287 |
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" |
| 288 |
xmlns:ns0="http://schemas.xmlsoap.org/soap/encoding/" |
| 289 |
xmlns:ns1="https://{0}/license/services/urn:com.f5.license.v5b.ActivationService" |
| 290 |
xmlns:ns2="http://schemas.xmlsoap.org/soap/envelope/" |
| 291 |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 292 |
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" |
| 293 |
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> |
| 294 |
<SOAP-ENV:Header/> |
| 295 |
<ns2:Body> |
| 296 |
<ns1:getLicense> |
| 297 |
<dossier xsi:type="ns3:string">{1}</dossier> |
| 298 |
<eula xsi:type="ns3:string">{eula}</eula> |
| 299 |
<email xsi:type="ns3:string">{email}</email> |
| 300 |
<firstName xsi:type="ns3:string">{first_name}</firstName> |
| 301 |
<lastName xsi:type="ns3:string">{last_name}</lastName> |
| 302 |
<companyName xsi:type="ns3:string">{company}</companyName> |
| 303 |
<phone xsi:type="ns3:string">{phone}</phone> |
| 304 |
<jobTitle xsi:type="ns3:string">{job_title}</jobTitle> |
| 305 |
<address xsi:type="ns3:string">{address}</address> |
| 306 |
<city xsi:type="ns3:string">{city}</city> |
| 307 |
<stateProvince xsi:type="ns3:string">{state}</stateProvince> |
| 308 |
<postalCode xsi:type="ns3:string">{postal_code}</postalCode> |
| 309 |
<country xsi:type="ns3:string">{country}</country> |
| 310 |
</ns1:getLicense> |
| 311 |
</ns2:Body> |
| 312 |
</SOAP-ENV:Envelope>""" |
| 313 |
result = result.format(self.license_server, self.dossier, **self.license_options) |
| 314 |
return result |
| 315 |
|
| 316 |
@property |
| 317 |
def addon_keys(self): |
| 318 |
if self._values['license_key'] is None: |
| 319 |
return None |
| 320 |
if self._values['addon_keys'] is None or is_empty_list(self._values['addon_keys']): |
| 321 |
return None |
| 322 |
result = ','.join(self._values['addon_keys']) |
| 323 |
return result |
| 324 |
|
| 325 |
|
| 326 |
class Changes(Parameters): |
| 327 |
def to_return(self): |
| 328 |
result = {} |
| 329 |
try: |
| 330 |
for returnable in self.returnables: |
| 331 |
result[returnable] = getattr(self, returnable) |
| 332 |
result = self._filter_params(result) |
| 333 |
except Exception: |
| 334 |
raise |
| 335 |
return result |
| 336 |
|
| 337 |
|
| 338 |
class UsableChanges(Changes): |
| 339 |
pass |
| 340 |
|
| 341 |
|
| 342 |
class ReportableChanges(Changes): |
| 343 |
pass |
| 344 |
|
| 345 |
|
| 346 |
class Difference(object): |
| 347 |
def __init__(self, want, have=None): |
| 348 |
self.want = want |
| 349 |
self.have = have |
| 350 |
|
| 351 |
def compare(self, param): |
| 352 |
try: |
| 353 |
result = getattr(self, param) |
| 354 |
return result |
| 355 |
except AttributeError: |
| 356 |
return self.__default(param) |
| 357 |
|
| 358 |
def __default(self, param): |
| 359 |
attr1 = getattr(self.want, param) |
| 360 |
try: |
| 361 |
attr2 = getattr(self.have, param) |
| 362 |
if attr1 != attr2: |
| 363 |
return attr1 |
| 364 |
except AttributeError: |
| 365 |
return attr1 |
| 366 |
|
| 367 |
|
| 368 |
class ModuleManager(object): |
| 369 |
def __init__(self, *args, **kwargs): |
| 370 |
self.module = kwargs.get('module', None) |
| 371 |
self.client = F5RestClient(**self.module.params) |
| 372 |
self.want = ModuleParameters(params=self.module.params, client=self.client) |
| 373 |
self.have = ApiParameters(client=self.client) |
| 374 |
self.changes = UsableChanges() |
| 375 |
self.escape_patterns = r'([$"' + "'])" |
| 376 |
|
| 377 |
def _set_changed_options(self): |
| 378 |
changed = {} |
| 379 |
for key in Parameters.returnables: |
| 380 |
if getattr(self.want, key) is not None: |
| 381 |
changed[key] = getattr(self.want, key) |
| 382 |
if changed: |
| 383 |
self.changes = UsableChanges(params=changed) |
| 384 |
|
| 385 |
def _update_changed_options(self): |
| 386 |
diff = Difference(self.want, self.have) |
| 387 |
updatables = Parameters.updatables |
| 388 |
changed = dict() |
| 389 |
for k in updatables: |
| 390 |
change = diff.compare(k) |
| 391 |
if change is None: |
| 392 |
continue |
| 393 |
else: |
| 394 |
if isinstance(change, dict): |
| 395 |
changed.update(change) |
| 396 |
else: |
| 397 |
changed[k] = change |
| 398 |
if changed: |
| 399 |
self.changes = UsableChanges(params=changed) |
| 400 |
return True |
| 401 |
return False |
| 402 |
|
| 403 |
def should_update(self): |
| 404 |
result = self._update_changed_options() |
| 405 |
if result: |
| 406 |
return True |
| 407 |
return False |
| 408 |
|
| 409 |
def exec_module(self): |
| 410 |
start = datetime.now().isoformat() |
| 411 |
changed = False |
| 412 |
result = dict() |
| 413 |
state = self.want.state |
| 414 |
if state == "present": |
| 415 |
changed = self.present() |
| 416 |
elif state == "latest": |
| 417 |
changed = self.valid() |
| 418 |
elif state == "absent": |
| 419 |
changed = self.absent() |
| 420 |
elif state == "revoked": |
| 421 |
changed = self.revoke() |
| 422 |
|
| 423 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 424 |
changes = reportable.to_return() |
| 425 |
result.update(**changes) |
| 426 |
result.update(dict(changed=changed)) |
| 427 |
self._announce_deprecations(result) |
| 428 |
send_teem(start, self.client, self.module, None) |
| 429 |
return result |
| 430 |
|
| 431 |
def _announce_deprecations(self, result): |
| 432 |
warnings = result.pop('__warnings', []) |
| 433 |
for warning in warnings: |
| 434 |
self.client.module.deprecate( |
| 435 |
msg=warning['msg'], |
| 436 |
version=warning['version'] |
| 437 |
) |
| 438 |
|
| 439 |
def present(self): |
| 440 |
if self.exists() and not self.is_revoked(): |
| 441 |
return False |
| 442 |
else: |
| 443 |
return self.create() |
| 444 |
|
| 445 |
def valid(self): |
| 446 |
if self.exists() and not self.is_revoked(): |
| 447 |
if self.want.force or not self.license_valid(): |
| 448 |
return self.create() |
| 449 |
else: |
| 450 |
return False |
| 451 |
else: |
| 452 |
return self.create() |
| 453 |
|
| 454 |
def remove(self): |
| 455 |
if self.module.check_mode: |
| 456 |
return True |
| 457 |
self.remove_from_device() |
| 458 |
if self.exists(): |
| 459 |
raise F5ModuleError("Failed to delete the resource.") |
| 460 |
return True |
| 461 |
|
| 462 |
def revoke(self): |
| 463 |
if self.is_revoked(): |
| 464 |
return False |
| 465 |
else: |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
self.want.update({'accept_eula': True}) |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
return self.create() |
| 476 |
|
| 477 |
def revoke_from_device(self): |
| 478 |
if self.module.check_mode: |
| 479 |
return True |
| 480 |
|
| 481 |
dossier = self.read_dossier_from_device() |
| 482 |
if dossier: |
| 483 |
self.want.update({'dossier': dossier}) |
| 484 |
else: |
| 485 |
raise F5ModuleError("Dossier not generated.") |
| 486 |
|
| 487 |
if self.is_revoked(): |
| 488 |
return False |
| 489 |
|
| 490 |
def is_revoked(self): |
| 491 |
command = '-c "egrep Revoked /config/bigip.license"' |
| 492 |
params = dict( |
| 493 |
command='run', |
| 494 |
utilCmdArgs=command |
| 495 |
) |
| 496 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 497 |
self.client.provider['server'], |
| 498 |
self.client.provider['server_port'] |
| 499 |
) |
| 500 |
resp = self.client.api.post(uri, json=params) |
| 501 |
try: |
| 502 |
response = resp.json() |
| 503 |
except ValueError as ex: |
| 504 |
raise F5ModuleError(str(ex)) |
| 505 |
|
| 506 |
if resp.status not in [200, 201] or 'code' in response and response['code'] not in [200, 201]: |
| 507 |
raise F5ModuleError(resp.content) |
| 508 |
if 'commandResult' in response and 'Revoked' in response['commandResult']: |
| 509 |
return True |
| 510 |
return False |
| 511 |
|
| 512 |
def read_dossier_from_device(self): |
| 513 |
params = dict( |
| 514 |
command='run', |
| 515 |
utilCmdArgs='-b "{0}"'.format(self.want.license_key) |
| 516 |
) |
| 517 |
if self.want.addon_keys: |
| 518 |
addons = self.want.addon_keys |
| 519 |
params['utilCmdArgs'] = '-a {0} '.format(addons) + params['utilCmdArgs'] |
| 520 |
|
| 521 |
if self.want.state == 'revoked': |
| 522 |
params['utilCmdArgs'] = '-r ' + params['utilCmdArgs'] |
| 523 |
|
| 524 |
uri = "https://{0}:{1}/mgmt/tm/util/get-dossier".format( |
| 525 |
self.client.provider['server'], |
| 526 |
self.client.provider['server_port'] |
| 527 |
) |
| 528 |
resp = self.client.api.post(uri, json=params) |
| 529 |
try: |
| 530 |
response = resp.json() |
| 531 |
except ValueError as ex: |
| 532 |
raise F5ModuleError(str(ex)) |
| 533 |
|
| 534 |
if resp.status not in [200, 201] or 'code' in response and response['code'] not in [200, 201]: |
| 535 |
raise F5ModuleError(resp.content) |
| 536 |
try: |
| 537 |
if self.want.state == 'revoked': |
| 538 |
return response['commandResult'][8:] |
| 539 |
else: |
| 540 |
return response['commandResult'] |
| 541 |
except Exception: |
| 542 |
return None |
| 543 |
|
| 544 |
def generate_license_from_remote(self): |
| 545 |
mgmt = iControlRestSession( |
| 546 |
validate_certs=False, |
| 547 |
headers={ |
| 548 |
'SOAPAction': '""', |
| 549 |
'Content-Type': 'text/xml; charset=utf-8', |
| 550 |
} |
| 551 |
) |
| 552 |
|
| 553 |
for x in range(0, 10): |
| 554 |
try: |
| 555 |
resp = mgmt.post( |
| 556 |
self.want.license_url, |
| 557 |
data=self.want.license_envelope, |
| 558 |
) |
| 559 |
except Exception: |
| 560 |
|
| 561 |
raise |
| 562 |
|
| 563 |
try: |
| 564 |
resp = LicenseXmlParser(content=resp.content) |
| 565 |
result = resp.json() |
| 566 |
except F5ModuleError: |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
raise |
| 574 |
except Exception: |
| 575 |
|
| 576 |
raise |
| 577 |
|
| 578 |
if result['state'] == 'EULA_REQUIRED': |
| 579 |
self.want.update({'eula': result['eula']}) |
| 580 |
continue |
| 581 |
if result['state'] == 'LICENSE_RETURNED': |
| 582 |
return result |
| 583 |
elif result['state'] == 'EMAIL_REQUIRED': |
| 584 |
raise F5ModuleError("Email must be provided") |
| 585 |
elif result['state'] == 'CONTACT_INFO_REQUIRED': |
| 586 |
raise F5ModuleError("Contact info must be provided") |
| 587 |
else: |
| 588 |
raise F5ModuleError(result['fault_text']) |
| 589 |
|
| 590 |
def create(self): |
| 591 |
self._set_changed_options() |
| 592 |
if not self.want.accept_eula: |
| 593 |
raise F5ModuleError( |
| 594 |
"You must read and accept the product EULA to license the box." |
| 595 |
) |
| 596 |
if self.module.check_mode: |
| 597 |
return True |
| 598 |
|
| 599 |
dossier = self.read_dossier_from_device() |
| 600 |
if dossier: |
| 601 |
self.want.update({'dossier': dossier}) |
| 602 |
else: |
| 603 |
raise F5ModuleError("Dossier not generated.") |
| 604 |
|
| 605 |
self.create_on_device() |
| 606 |
self.wait_for_mcpd() |
| 607 |
if not self.exists(): |
| 608 |
raise F5ModuleError( |
| 609 |
"Failed to license the device." |
| 610 |
) |
| 611 |
return True |
| 612 |
|
| 613 |
def absent(self): |
| 614 |
if self.any_license_exists(): |
| 615 |
self.remove() |
| 616 |
self.wait_for_mcpd() |
| 617 |
if self.exists(): |
| 618 |
raise F5ModuleError( |
| 619 |
"Failed to remove the license from the device." |
| 620 |
) |
| 621 |
return True |
| 622 |
return False |
| 623 |
|
| 624 |
def license_valid(self): |
| 625 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 626 |
uri = "https://{0}:{1}/mgmt/tm/shared/licensing/registration".format( |
| 627 |
self.client.provider['server'], |
| 628 |
self.client.provider['server_port'], |
| 629 |
) |
| 630 |
resp = self.client.api.get(uri) |
| 631 |
try: |
| 632 |
response = resp.json() |
| 633 |
except ValueError as ex: |
| 634 |
raise F5ModuleError(str(ex)) |
| 635 |
|
| 636 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 637 |
if 'message' in response: |
| 638 |
raise F5ModuleError(response['message']) |
| 639 |
else: |
| 640 |
raise F5ModuleError(resp.content) |
| 641 |
try: |
| 642 |
return int(float(response['expiresInDays'])) > 0 |
| 643 |
except Exception: |
| 644 |
pass |
| 645 |
return False |
| 646 |
|
| 647 |
def exists(self): |
| 648 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 649 |
uri = "https://{0}:{1}/mgmt/tm/shared/licensing/registration".format( |
| 650 |
self.client.provider['server'], |
| 651 |
self.client.provider['server_port'], |
| 652 |
) |
| 653 |
resp = self.client.api.get(uri) |
| 654 |
try: |
| 655 |
response = resp.json() |
| 656 |
except ValueError as ex: |
| 657 |
raise F5ModuleError(str(ex)) |
| 658 |
|
| 659 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 660 |
if 'message' in response: |
| 661 |
raise F5ModuleError(response['message']) |
| 662 |
else: |
| 663 |
raise F5ModuleError(resp.content) |
| 664 |
|
| 665 |
try: |
| 666 |
if response['registrationKey'] == self.want.license_key: |
| 667 |
return True |
| 668 |
except Exception: |
| 669 |
pass |
| 670 |
return False |
| 671 |
|
| 672 |
def wait_for_mcpd(self): |
| 673 |
nops = 0 |
| 674 |
|
| 675 |
|
| 676 |
time.sleep(5) |
| 677 |
|
| 678 |
while nops < 4: |
| 679 |
try: |
| 680 |
if self._is_mcpd_ready_on_device(): |
| 681 |
nops += 1 |
| 682 |
else: |
| 683 |
nops = 0 |
| 684 |
except Exception as ex: |
| 685 |
if '"message":"X-F5-Auth-Token has expired."' in str(ex): |
| 686 |
raise F5ModuleError("X-F5-Auth-Token has expired.") |
| 687 |
pass |
| 688 |
time.sleep(5) |
| 689 |
|
| 690 |
def _is_mcpd_ready_on_device(self): |
| 691 |
try: |
| 692 |
command = "tmsh show sys mcp-state | grep running" |
| 693 |
params = dict( |
| 694 |
command='run', |
| 695 |
utilCmdArgs='-c "{0}"'.format(command) |
| 696 |
) |
| 697 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 698 |
self.client.provider['server'], |
| 699 |
self.client.provider['server_port'] |
| 700 |
) |
| 701 |
resp = self.client.api.post(uri, json=params) |
| 702 |
try: |
| 703 |
response = resp.json() |
| 704 |
except ValueError as ex: |
| 705 |
raise F5ModuleError(str(ex)) |
| 706 |
|
| 707 |
if resp.status not in [200, 201] or 'code' in response and response['code'] not in [200, 201]: |
| 708 |
raise F5ModuleError(resp.content) |
| 709 |
|
| 710 |
if 'commandResult' in response: |
| 711 |
return True |
| 712 |
except Exception as ex: |
| 713 |
if '"message":"X-F5-Auth-Token has expired."' in str(ex): |
| 714 |
raise |
| 715 |
pass |
| 716 |
return False |
| 717 |
|
| 718 |
def any_license_exists(self): |
| 719 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 720 |
uri = "https://{0}:{1}/mgmt/tm/shared/licensing/registration".format( |
| 721 |
self.client.provider['server'], |
| 722 |
self.client.provider['server_port'], |
| 723 |
) |
| 724 |
resp = self.client.api.get(uri) |
| 725 |
try: |
| 726 |
response = resp.json() |
| 727 |
except ValueError as ex: |
| 728 |
raise F5ModuleError(str(ex)) |
| 729 |
|
| 730 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 731 |
if 'message' in response: |
| 732 |
raise F5ModuleError(response['message']) |
| 733 |
else: |
| 734 |
raise F5ModuleError(resp.content) |
| 735 |
try: |
| 736 |
if response['registrationKey'] is not None: |
| 737 |
return True |
| 738 |
except Exception: |
| 739 |
pass |
| 740 |
return False |
| 741 |
|
| 742 |
def create_on_device(self): |
| 743 |
license = self.generate_license_from_remote() |
| 744 |
if license is None: |
| 745 |
raise F5ModuleError( |
| 746 |
"Failed to generate license from F5 activation servers." |
| 747 |
) |
| 748 |
result = self.upload_license_to_device(license) |
| 749 |
if not result: |
| 750 |
raise F5ModuleError( |
| 751 |
"Failed to install license on device." |
| 752 |
) |
| 753 |
result = self.upload_eula_to_device(license) |
| 754 |
if not result: |
| 755 |
raise F5ModuleError( |
| 756 |
"Failed to upload EULA file to device." |
| 757 |
) |
| 758 |
result = self.reload_license() |
| 759 |
if not result: |
| 760 |
raise F5ModuleError( |
| 761 |
"Failed to reload license configuration." |
| 762 |
) |
| 763 |
|
| 764 |
def upload_license_to_device(self, license): |
| 765 |
license_payload = re.sub(self.escape_patterns, r'\\\1', license['license']) |
| 766 |
command_arg = """cat > /config/bigip.license <<EOF\n{0}\nEOF""".format(license_payload) |
| 767 |
command = '-c "{0}"'.format(command_arg) |
| 768 |
params = dict( |
| 769 |
command='run', |
| 770 |
utilCmdArgs=command |
| 771 |
) |
| 772 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 773 |
self.client.provider['server'], |
| 774 |
self.client.provider['server_port'] |
| 775 |
) |
| 776 |
resp = self.client.api.post(uri, json=params) |
| 777 |
try: |
| 778 |
response = resp.json() |
| 779 |
except ValueError as ex: |
| 780 |
raise F5ModuleError(str(ex)) |
| 781 |
|
| 782 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 783 |
return True |
| 784 |
raise F5ModuleError(resp.content) |
| 785 |
|
| 786 |
def upload_eula_to_device(self, license): |
| 787 |
eula_payload = re.sub(self.escape_patterns, r'\\\1', license['eula']) |
| 788 |
command_arg = """cat > /LICENSE.F5 <<EOF\n{0}\nEOF""".format(eula_payload) |
| 789 |
command = '-c "{0}"'.format(command_arg) |
| 790 |
params = dict( |
| 791 |
command='run', |
| 792 |
utilCmdArgs=command |
| 793 |
) |
| 794 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 795 |
self.client.provider['server'], |
| 796 |
self.client.provider['server_port'] |
| 797 |
) |
| 798 |
resp = self.client.api.post(uri, json=params) |
| 799 |
try: |
| 800 |
response = resp.json() |
| 801 |
except ValueError as ex: |
| 802 |
raise F5ModuleError(str(ex)) |
| 803 |
|
| 804 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 805 |
return True |
| 806 |
raise F5ModuleError(resp.content) |
| 807 |
|
| 808 |
def reload_license(self): |
| 809 |
command = '-c "{0}"'.format("/usr/bin/reloadlic") |
| 810 |
params = dict( |
| 811 |
command='run', |
| 812 |
utilCmdArgs=command |
| 813 |
) |
| 814 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 815 |
self.client.provider['server'], |
| 816 |
self.client.provider['server_port'] |
| 817 |
) |
| 818 |
resp = self.client.api.post(uri, json=params) |
| 819 |
try: |
| 820 |
response = resp.json() |
| 821 |
except ValueError as ex: |
| 822 |
raise F5ModuleError(str(ex)) |
| 823 |
|
| 824 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 825 |
return True |
| 826 |
raise F5ModuleError(resp.content) |
| 827 |
|
| 828 |
def remove_from_device(self): |
| 829 |
result = self.remove_license_from_device() |
| 830 |
if not result: |
| 831 |
raise F5ModuleError( |
| 832 |
"Failed to remove license from device." |
| 833 |
) |
| 834 |
result = self.remove_eula_from_device() |
| 835 |
if not result: |
| 836 |
raise F5ModuleError( |
| 837 |
"Failed to remove EULA file from device." |
| 838 |
) |
| 839 |
result = self.reload_license() |
| 840 |
if not result: |
| 841 |
raise F5ModuleError( |
| 842 |
"Failed to reload the empty license configuration." |
| 843 |
) |
| 844 |
|
| 845 |
def remove_license_from_device(self): |
| 846 |
params = dict( |
| 847 |
command='run', |
| 848 |
utilCmdArgs='/config/bigip.license' |
| 849 |
) |
| 850 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 851 |
self.client.provider['server'], |
| 852 |
self.client.provider['server_port'] |
| 853 |
) |
| 854 |
resp = self.client.api.post(uri, json=params) |
| 855 |
|
| 856 |
try: |
| 857 |
response = resp.json() |
| 858 |
except ValueError as ex: |
| 859 |
raise F5ModuleError(str(ex)) |
| 860 |
|
| 861 |
if resp.status not in [200, 201] or 'code' in response and response['code'] not in [200, 201]: |
| 862 |
raise F5ModuleError(resp.content) |
| 863 |
|
| 864 |
if 'commandResult' in response: |
| 865 |
if 'No such file or directory' in response['commandResult']: |
| 866 |
return True |
| 867 |
else: |
| 868 |
raise F5ModuleError(response['commandResult']) |
| 869 |
return True |
| 870 |
|
| 871 |
def remove_eula_from_device(self): |
| 872 |
params = dict( |
| 873 |
command='run', |
| 874 |
utilCmdArgs='/LICENSE.F5' |
| 875 |
) |
| 876 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 877 |
self.client.provider['server'], |
| 878 |
self.client.provider['server_port'] |
| 879 |
) |
| 880 |
resp = self.client.api.post(uri, json=params) |
| 881 |
|
| 882 |
try: |
| 883 |
response = resp.json() |
| 884 |
except ValueError as ex: |
| 885 |
raise F5ModuleError(str(ex)) |
| 886 |
|
| 887 |
if resp.status not in [200, 201] or 'code' in response and response['code'] not in [200, 201]: |
| 888 |
raise F5ModuleError(resp.content) |
| 889 |
|
| 890 |
if 'commandResult' in response: |
| 891 |
if 'No such file or directory' in response['commandResult']: |
| 892 |
return True |
| 893 |
else: |
| 894 |
raise F5ModuleError(response['commandResult']) |
| 895 |
return True |
| 896 |
|
| 897 |
|
| 898 |
class ArgumentSpec(object): |
| 899 |
def __init__(self): |
| 900 |
self.supports_check_mode = True |
| 901 |
argument_spec = dict( |
| 902 |
license_key=dict(no_log=True), |
| 903 |
addon_keys=dict( |
| 904 |
type='list', |
| 905 |
elements='str', |
| 906 |
no_log=True |
| 907 |
), |
| 908 |
license_server=dict( |
| 909 |
default='activate.f5.com' |
| 910 |
), |
| 911 |
state=dict( |
| 912 |
choices=['absent', 'present', 'revoked', 'latest'], |
| 913 |
default='present' |
| 914 |
), |
| 915 |
accept_eula=dict( |
| 916 |
type='bool', |
| 917 |
default='no' |
| 918 |
), |
| 919 |
force=dict( |
| 920 |
type='bool', |
| 921 |
default='no' |
| 922 |
) |
| 923 |
) |
| 924 |
self.argument_spec = {} |
| 925 |
self.argument_spec.update(f5_argument_spec) |
| 926 |
self.argument_spec.update(argument_spec) |
| 927 |
self.required_if = [ |
| 928 |
['state', 'present', ['accept_eula', 'license_key']], |
| 929 |
['state', 'latest', ['accept_eula', 'license_key']] |
| 930 |
] |
| 931 |
|
| 932 |
|
| 933 |
def main(): |
| 934 |
spec = ArgumentSpec() |
| 935 |
|
| 936 |
module = AnsibleModule( |
| 937 |
argument_spec=spec.argument_spec, |
| 938 |
supports_check_mode=spec.supports_check_mode, |
| 939 |
required_if=spec.required_if |
| 940 |
) |
| 941 |
|
| 942 |
try: |
| 943 |
mm = ModuleManager(module=module) |
| 944 |
results = mm.exec_module() |
| 945 |
module.exit_json(**results) |
| 946 |
except F5ModuleError as ex: |
| 947 |
module.fail_json(msg=str(ex)) |
| 948 |
|
| 949 |
|
| 950 |
if __name__ == '__main__': |
| 951 |
main() |