| 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_gtm_wide_ip |
| 13 |
short_description: Manages F5 BIG-IP GTM Wide IP |
| 14 |
description: |
| 15 |
- Manages the F5 BIG-IP GTM (now BIG-IP DNS) Wide IP. |
| 16 |
version_added: "1.0.0" |
| 17 |
options: |
| 18 |
pool_lb_method: |
| 19 |
description: |
| 20 |
- Specifies the load balancing method used to select a pool in this wide |
| 21 |
IP. This setting is relevant only when multiple pools are configured |
| 22 |
for a Wide IP. |
| 23 |
type: str |
| 24 |
aliases: ['lb_method'] |
| 25 |
choices: |
| 26 |
- round-robin |
| 27 |
- ratio |
| 28 |
- topology |
| 29 |
- global-availability |
| 30 |
name: |
| 31 |
description: |
| 32 |
- Wide IP name. This name must be formatted as a fully qualified |
| 33 |
domain name (FQDN). You can also use the alias C(wide_ip) but this |
| 34 |
is deprecated and will be removed in a future Ansible version. |
| 35 |
type: str |
| 36 |
required: True |
| 37 |
aliases: |
| 38 |
- wide_ip |
| 39 |
type: |
| 40 |
description: |
| 41 |
- Specifies the type of Wide IP. GTM Wide IPs need to be keyed by query |
| 42 |
type in addition to name, because pool members need different attributes |
| 43 |
depending on the response RDATA they are meant to supply. |
| 44 |
type: str |
| 45 |
required: True |
| 46 |
choices: |
| 47 |
- a |
| 48 |
- aaaa |
| 49 |
- cname |
| 50 |
- mx |
| 51 |
- naptr |
| 52 |
- srv |
| 53 |
state: |
| 54 |
description: |
| 55 |
- When C(present) or C(enabled), ensures the Wide IP exists and |
| 56 |
is enabled. |
| 57 |
- When C(absent), ensures the Wide IP has been removed. |
| 58 |
- When C(disabled), ensures the Wide IP exists and is disabled. |
| 59 |
type: str |
| 60 |
choices: |
| 61 |
- present |
| 62 |
- absent |
| 63 |
- disabled |
| 64 |
- enabled |
| 65 |
default: present |
| 66 |
partition: |
| 67 |
description: |
| 68 |
- Device partition to manage resources on. |
| 69 |
type: str |
| 70 |
default: Common |
| 71 |
pools: |
| 72 |
description: |
| 73 |
- The pools you want associated with the Wide IP. |
| 74 |
- If C(ratio) is not provided when creating a new Wide IP, it will default |
| 75 |
to 1. |
| 76 |
type: list |
| 77 |
elements: dict |
| 78 |
suboptions: |
| 79 |
name: |
| 80 |
description: |
| 81 |
- The name of the pool to include. |
| 82 |
type: str |
| 83 |
required: True |
| 84 |
ratio: |
| 85 |
description: |
| 86 |
- Ratio for the pool. |
| 87 |
- The system uses this number with the Ratio load balancing method. |
| 88 |
- When C(ratio) is not provided, the module assigns it value of C(0). |
| 89 |
type: int |
| 90 |
order: |
| 91 |
description: |
| 92 |
- Order of the pool in relation to other pools attached to this Wide IP. |
| 93 |
- Pool order is significant when the Global Availability load balancing method is used. |
| 94 |
- When C(order) is not provided, the module assigns it value of C(0). |
| 95 |
type: int |
| 96 |
irules: |
| 97 |
description: |
| 98 |
- List of rules to be applied. |
| 99 |
- If you want to remove all existing iRules, specify a single empty value; C(""). |
| 100 |
See the documentation for an example. |
| 101 |
type: list |
| 102 |
elements: str |
| 103 |
aliases: |
| 104 |
description: |
| 105 |
- Specifies alternate domain names for the web site content you are load |
| 106 |
balancing. |
| 107 |
- You can use the same wildcard characters for aliases as you can for actual |
| 108 |
Wide IP names. |
| 109 |
type: list |
| 110 |
elements: str |
| 111 |
last_resort_pool: |
| 112 |
description: |
| 113 |
- Specifies which GTM pool for the system to use as the last resort pool for |
| 114 |
the Wide IP. |
| 115 |
- The valid pools for this parameter are those with the C(type) specified in this |
| 116 |
module. |
| 117 |
type: str |
| 118 |
persistence: |
| 119 |
description: |
| 120 |
- When C(true), ensures when a local DNS makes repetitive requests on |
| 121 |
behalf of a client, the system reconnects the client to the same resource |
| 122 |
as previous requests. |
| 123 |
- When C(false), ensures repetitive requests do not reconnect the client |
| 124 |
to the same resource. |
| 125 |
type: bool |
| 126 |
persistence_ttl: |
| 127 |
description: |
| 128 |
- Specifies the time to maintain a connection between an local DNS and |
| 129 |
a particular virtual server. |
| 130 |
type: int |
| 131 |
persist_cidr_ipv4: |
| 132 |
description: |
| 133 |
- Specifies a mask used to group IPv4 LDNS addresses. This feature |
| 134 |
allows one persistence record to be shared by LDNS addresses |
| 135 |
that match within this mask. |
| 136 |
type: int |
| 137 |
persist_cidr_ipv6: |
| 138 |
description: |
| 139 |
- Specifies a mask used to group IPv6 LDNS addresses. This feature |
| 140 |
allows one persistence record to be shared by LDNS addresses |
| 141 |
that match within this mask. |
| 142 |
type: int |
| 143 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 144 |
author: |
| 145 |
- Tim Rupp (@caphrim007) |
| 146 |
- Wojciech Wypior (@wojtek0806) |
| 147 |
''' |
| 148 |
|
| 149 |
EXAMPLES = r''' |
| 150 |
- name: Set lb method |
| 151 |
bigip_gtm_wide_ip: |
| 152 |
pool_lb_method: round-robin |
| 153 |
name: my-wide-ip.example.com |
| 154 |
type: a |
| 155 |
provider: |
| 156 |
user: admin |
| 157 |
password: secret |
| 158 |
server: lb.mydomain.com |
| 159 |
delegate_to: localhost |
| 160 |
|
| 161 |
- name: Add iRules to the Wide IP |
| 162 |
bigip_gtm_wide_ip: |
| 163 |
pool_lb_method: round-robin |
| 164 |
name: my-wide-ip.example.com |
| 165 |
type: a |
| 166 |
irules: |
| 167 |
- irule1 |
| 168 |
- irule2 |
| 169 |
provider: |
| 170 |
user: admin |
| 171 |
password: secret |
| 172 |
server: lb.mydomain.com |
| 173 |
delegate_to: localhost |
| 174 |
|
| 175 |
- name: Remove one iRule from the Virtual Server |
| 176 |
bigip_gtm_wide_ip: |
| 177 |
pool_lb_method: round-robin |
| 178 |
name: my-wide-ip.example.com |
| 179 |
type: a |
| 180 |
irules: |
| 181 |
- irule1 |
| 182 |
provider: |
| 183 |
user: admin |
| 184 |
password: secret |
| 185 |
server: lb.mydomain.com |
| 186 |
delegate_to: localhost |
| 187 |
|
| 188 |
- name: Remove all iRules from the Virtual Server |
| 189 |
bigip_gtm_wide_ip: |
| 190 |
pool_lb_method: round-robin |
| 191 |
name: my-wide-ip.example.com |
| 192 |
type: a |
| 193 |
irules: "" |
| 194 |
provider: |
| 195 |
user: admin |
| 196 |
password: secret |
| 197 |
server: lb.mydomain.com |
| 198 |
delegate_to: localhost |
| 199 |
|
| 200 |
- name: Assign a pool with ratio to the Wide IP |
| 201 |
bigip_gtm_wide_ip: |
| 202 |
pool_lb_method: round-robin |
| 203 |
name: my-wide-ip.example.com |
| 204 |
type: a |
| 205 |
pools: |
| 206 |
- name: pool1 |
| 207 |
ratio: 100 |
| 208 |
order: 2 |
| 209 |
- name: pool1 |
| 210 |
ratio: 100 |
| 211 |
order: 1 |
| 212 |
provider: |
| 213 |
user: admin |
| 214 |
password: secret |
| 215 |
server: lb.mydomain.com |
| 216 |
delegate_to: localhost |
| 217 |
|
| 218 |
- name: Assign a pool with persistence to the Wide IP |
| 219 |
bigip_gtm_wide_ip: |
| 220 |
pool_lb_method: round-robin |
| 221 |
name: my-wide-ip.example.com |
| 222 |
type: a |
| 223 |
pools: |
| 224 |
- name: pool1 |
| 225 |
persistence: true |
| 226 |
persist_cidr_ipv4: 24 |
| 227 |
persist_cidr_ipv6: 120 |
| 228 |
persistence_ttl: 3500 |
| 229 |
provider: |
| 230 |
user: admin |
| 231 |
password: secret |
| 232 |
server: lb.mydomain.com |
| 233 |
delegate_to: localhost |
| 234 |
''' |
| 235 |
|
| 236 |
RETURN = r''' |
| 237 |
lb_method: |
| 238 |
description: The new load balancing method used by the Wide IP. |
| 239 |
returned: changed |
| 240 |
type: str |
| 241 |
sample: topology |
| 242 |
state: |
| 243 |
description: The new state of the Wide IP. |
| 244 |
returned: changed |
| 245 |
type: str |
| 246 |
sample: disabled |
| 247 |
irules: |
| 248 |
description: iRules set on the Wide IP. |
| 249 |
returned: changed |
| 250 |
type: list |
| 251 |
sample: ['/Common/irule1', '/Common/irule2'] |
| 252 |
aliases: |
| 253 |
description: Aliases set on the Wide IP. |
| 254 |
returned: changed |
| 255 |
type: list |
| 256 |
sample: ['alias1.foo.com', '*.wildcard.domain'] |
| 257 |
persistence: |
| 258 |
description: Whether pool connections will be persisted. |
| 259 |
returned: changed |
| 260 |
type: bool |
| 261 |
sample: false |
| 262 |
persist_cidr_ipv4: |
| 263 |
description: Specifies a mask used to group IPv4 LDNS addresses. |
| 264 |
returned: changed |
| 265 |
type: int |
| 266 |
sample: 32 |
| 267 |
persist_cidr_ipv6: |
| 268 |
description: Specifies a mask used to group IPv6 LDNS addresses. |
| 269 |
returned: changed |
| 270 |
type: int |
| 271 |
sample: 128 |
| 272 |
persistence_ttl: |
| 273 |
description: Specifies the persistence TTL between an local DNS and a particular virtual server. |
| 274 |
returned: changed |
| 275 |
type: int |
| 276 |
sample: 3600 |
| 277 |
''' |
| 278 |
from datetime import datetime |
| 279 |
|
| 280 |
from ansible.module_utils.basic import ( |
| 281 |
AnsibleModule, env_fallback |
| 282 |
) |
| 283 |
from ansible.module_utils.six import iteritems |
| 284 |
|
| 285 |
from ..module_utils.bigip import F5RestClient |
| 286 |
from ..module_utils.common import ( |
| 287 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, flatten_boolean, fq_name, is_valid_fqdn |
| 288 |
) |
| 289 |
from ..module_utils.icontrol import ( |
| 290 |
module_provisioned, tmos_version |
| 291 |
) |
| 292 |
from ..module_utils.teem import send_teem |
| 293 |
|
| 294 |
|
| 295 |
class Parameters(AnsibleF5Parameters): |
| 296 |
api_map = { |
| 297 |
'poolLbMode': 'pool_lb_method', |
| 298 |
'rules': 'irules', |
| 299 |
'lastResortPool': 'last_resort_pool', |
| 300 |
'persistCidrIpv4': 'persist_cidr_ipv4', |
| 301 |
'persistCidrIpv6': 'persist_cidr_ipv6', |
| 302 |
'ttlPersistence': 'persistence_ttl', |
| 303 |
} |
| 304 |
|
| 305 |
updatables = [ |
| 306 |
'pool_lb_method', |
| 307 |
'state', |
| 308 |
'pools', |
| 309 |
'irules', |
| 310 |
'enabled', |
| 311 |
'disabled', |
| 312 |
'aliases', |
| 313 |
'last_resort_pool', |
| 314 |
'persist_cidr_ipv4', |
| 315 |
'persist_cidr_ipv6', |
| 316 |
'persistence', |
| 317 |
'persistence_ttl', |
| 318 |
] |
| 319 |
|
| 320 |
returnables = [ |
| 321 |
'name', |
| 322 |
'pool_lb_method', |
| 323 |
'state', |
| 324 |
'pools', |
| 325 |
'irules', |
| 326 |
'aliases', |
| 327 |
'last_resort_pool', |
| 328 |
'persistence', |
| 329 |
'persist_cidr_ipv4', |
| 330 |
'persist_cidr_ipv6', |
| 331 |
'persistence_ttl', |
| 332 |
] |
| 333 |
|
| 334 |
api_attributes = [ |
| 335 |
'poolLbMode', |
| 336 |
'enabled', |
| 337 |
'disabled', |
| 338 |
'pools', |
| 339 |
'rules', |
| 340 |
'aliases', |
| 341 |
'lastResortPool', |
| 342 |
'persistence', |
| 343 |
'ttlPersistence', |
| 344 |
'persistCidrIpv4', |
| 345 |
'persistCidrIpv6', |
| 346 |
] |
| 347 |
|
| 348 |
|
| 349 |
class ApiParameters(Parameters): |
| 350 |
@property |
| 351 |
def disabled(self): |
| 352 |
if self._values['disabled'] is True: |
| 353 |
return True |
| 354 |
return False |
| 355 |
|
| 356 |
@property |
| 357 |
def enabled(self): |
| 358 |
if self._values['enabled'] is True: |
| 359 |
return True |
| 360 |
return False |
| 361 |
|
| 362 |
@property |
| 363 |
def pools(self): |
| 364 |
result = [] |
| 365 |
if self._values['pools'] is None: |
| 366 |
return None |
| 367 |
pools = sorted(self._values['pools'], key=lambda x: x['order']) |
| 368 |
for item in pools: |
| 369 |
pool = dict() |
| 370 |
pool.update(item) |
| 371 |
name = '/{0}/{1}'.format(item['partition'], item['name']) |
| 372 |
del pool['nameReference'] |
| 373 |
del pool['name'] |
| 374 |
del pool['partition'] |
| 375 |
pool['name'] = name |
| 376 |
result.append(pool) |
| 377 |
return result |
| 378 |
|
| 379 |
@property |
| 380 |
def last_resort_pool(self): |
| 381 |
if self._values['last_resort_pool'] in [None, '', 'none']: |
| 382 |
return '' |
| 383 |
return self._values['last_resort_pool'] |
| 384 |
|
| 385 |
|
| 386 |
class ModuleParameters(Parameters): |
| 387 |
@property |
| 388 |
def last_resort_pool(self): |
| 389 |
if self._values['last_resort_pool'] is None: |
| 390 |
return None |
| 391 |
if self._values['last_resort_pool'] in ['', 'none']: |
| 392 |
return 'none' |
| 393 |
return '{0} {1}'.format( |
| 394 |
self.type, fq_name(self.partition, self._values['last_resort_pool']) |
| 395 |
) |
| 396 |
|
| 397 |
@property |
| 398 |
def pool_lb_method(self): |
| 399 |
if self._values['pool_lb_method'] is None: |
| 400 |
return None |
| 401 |
lb_method = str(self._values['pool_lb_method']) |
| 402 |
return lb_method |
| 403 |
|
| 404 |
@property |
| 405 |
def type(self): |
| 406 |
if self._values['type'] is None: |
| 407 |
return None |
| 408 |
return str(self._values['type']) |
| 409 |
|
| 410 |
@property |
| 411 |
def name(self): |
| 412 |
if self._values['name'] is None: |
| 413 |
return None |
| 414 |
if not is_valid_fqdn(self._values['name']): |
| 415 |
raise F5ModuleError( |
| 416 |
"The provided name must be a valid FQDN" |
| 417 |
) |
| 418 |
return self._values['name'] |
| 419 |
|
| 420 |
@property |
| 421 |
def state(self): |
| 422 |
if self._values['state'] == 'enabled': |
| 423 |
return 'present' |
| 424 |
return self._values['state'] |
| 425 |
|
| 426 |
@property |
| 427 |
def enabled(self): |
| 428 |
if self._values['state'] == 'disabled': |
| 429 |
return False |
| 430 |
elif self._values['state'] in ['present', 'enabled']: |
| 431 |
return True |
| 432 |
else: |
| 433 |
return None |
| 434 |
|
| 435 |
@property |
| 436 |
def disabled(self): |
| 437 |
if self._values['state'] == 'disabled': |
| 438 |
return True |
| 439 |
elif self._values['state'] in ['present', 'enabled']: |
| 440 |
return False |
| 441 |
else: |
| 442 |
return None |
| 443 |
|
| 444 |
@property |
| 445 |
def pools(self): |
| 446 |
result = [] |
| 447 |
if self._values['pools'] is None: |
| 448 |
return None |
| 449 |
for item in self._values['pools']: |
| 450 |
pool = dict() |
| 451 |
if 'name' not in item: |
| 452 |
raise F5ModuleError( |
| 453 |
"'name' is a required key for items in the list of pools." |
| 454 |
) |
| 455 |
if 'ratio' not in item or item['ratio'] is None: |
| 456 |
pool['ratio'] = 0 |
| 457 |
else: |
| 458 |
pool['ratio'] = item['ratio'] |
| 459 |
if 'order' not in item or item['order'] is None: |
| 460 |
pool['order'] = 0 |
| 461 |
else: |
| 462 |
pool['order'] = item['order'] |
| 463 |
pool['name'] = fq_name(self.partition, item['name']) |
| 464 |
result.append(pool) |
| 465 |
if result: |
| 466 |
pools = sorted(result, key=lambda x: x['order']) |
| 467 |
return pools |
| 468 |
|
| 469 |
@property |
| 470 |
def irules(self): |
| 471 |
results = [] |
| 472 |
if self._values['irules'] is None: |
| 473 |
return None |
| 474 |
if len(self._values['irules']) == 1 and self._values['irules'][0] == '': |
| 475 |
return '' |
| 476 |
for irule in self._values['irules']: |
| 477 |
result = fq_name(self.partition, irule) |
| 478 |
results.append(result) |
| 479 |
return results |
| 480 |
|
| 481 |
@property |
| 482 |
def aliases(self): |
| 483 |
if self._values['aliases'] is None: |
| 484 |
return None |
| 485 |
if len(self._values['aliases']) == 1 and self._values['aliases'][0] == '': |
| 486 |
return '' |
| 487 |
self._values['aliases'].sort() |
| 488 |
return self._values['aliases'] |
| 489 |
|
| 490 |
@property |
| 491 |
def persistence(self): |
| 492 |
if self._values['persistence'] is None: |
| 493 |
return None |
| 494 |
result = flatten_boolean(self._values['persistence']) |
| 495 |
if result is None: |
| 496 |
return None |
| 497 |
if result == 'yes': |
| 498 |
return 'enabled' |
| 499 |
return 'disabled' |
| 500 |
|
| 501 |
@property |
| 502 |
def persistence_ttl(self): |
| 503 |
if self._values['persistence_ttl'] is None: |
| 504 |
return None |
| 505 |
if 0 <= self._values['persistence_ttl'] <= 4294967295: |
| 506 |
return self._values['persistence_ttl'] |
| 507 |
raise F5ModuleError( |
| 508 |
"Valid 'persistence_ttl' must be in range 0 - 4294967295." |
| 509 |
) |
| 510 |
|
| 511 |
@property |
| 512 |
def persist_cidr_ipv4(self): |
| 513 |
if self._values['persist_cidr_ipv4'] is None: |
| 514 |
return None |
| 515 |
if 0 <= self._values['persist_cidr_ipv4'] <= 4294967295: |
| 516 |
return self._values['persist_cidr_ipv4'] |
| 517 |
raise F5ModuleError( |
| 518 |
"Valid 'persist_cidr_ipv4' must be in range 0 - 4294967295." |
| 519 |
) |
| 520 |
|
| 521 |
@property |
| 522 |
def persist_cidr_ipv6(self): |
| 523 |
if self._values['persist_cidr_ipv6'] is None: |
| 524 |
return None |
| 525 |
if 0 <= self._values['persist_cidr_ipv6'] <= 4294967295: |
| 526 |
return self._values['persist_cidr_ipv6'] |
| 527 |
raise F5ModuleError( |
| 528 |
"Valid 'persist_cidr_ipv6' must be in range 0 - 4294967295." |
| 529 |
) |
| 530 |
|
| 531 |
|
| 532 |
class Changes(Parameters): |
| 533 |
def to_return(self): |
| 534 |
result = {} |
| 535 |
try: |
| 536 |
for returnable in self.returnables: |
| 537 |
change = getattr(self, returnable) |
| 538 |
if isinstance(change, dict): |
| 539 |
result.update(change) |
| 540 |
else: |
| 541 |
result[returnable] = change |
| 542 |
result = self._filter_params(result) |
| 543 |
except Exception: |
| 544 |
raise |
| 545 |
return result |
| 546 |
|
| 547 |
|
| 548 |
class UsableChanges(Changes): |
| 549 |
@property |
| 550 |
def irules(self): |
| 551 |
if self._values['irules'] is None: |
| 552 |
return None |
| 553 |
if self._values['irules'] == '': |
| 554 |
return [] |
| 555 |
return self._values['irules'] |
| 556 |
|
| 557 |
|
| 558 |
class ReportableChanges(Changes): |
| 559 |
@property |
| 560 |
def pool_lb_method(self): |
| 561 |
result = dict( |
| 562 |
lb_method=self._values['pool_lb_method'], |
| 563 |
pool_lb_method=self._values['pool_lb_method'], |
| 564 |
) |
| 565 |
return result |
| 566 |
|
| 567 |
@property |
| 568 |
def last_resort_pool(self): |
| 569 |
if self._values['last_resort_pool'] is None: |
| 570 |
return None |
| 571 |
if self._values['last_resort_pool'] in ['', 'none']: |
| 572 |
return 'none' |
| 573 |
return self._values['last_resort_pool'].split(' ')[1] |
| 574 |
|
| 575 |
|
| 576 |
class Difference(object): |
| 577 |
def __init__(self, want, have=None): |
| 578 |
self.want = want |
| 579 |
self.have = have |
| 580 |
|
| 581 |
def compare(self, param): |
| 582 |
try: |
| 583 |
result = getattr(self, param) |
| 584 |
return result |
| 585 |
except AttributeError: |
| 586 |
return self.__default(param) |
| 587 |
|
| 588 |
def __default(self, param): |
| 589 |
attr1 = getattr(self.want, param) |
| 590 |
try: |
| 591 |
attr2 = getattr(self.have, param) |
| 592 |
if attr1 != attr2: |
| 593 |
return attr1 |
| 594 |
except AttributeError: |
| 595 |
return attr1 |
| 596 |
|
| 597 |
def to_tuple(self, items): |
| 598 |
result = [] |
| 599 |
for x in items: |
| 600 |
tmp = [(str(k), str(v)) for k, v in iteritems(x)] |
| 601 |
result += tmp |
| 602 |
return result |
| 603 |
|
| 604 |
def _diff_complex_items(self, want, have): |
| 605 |
if want == [] and have is None: |
| 606 |
return None |
| 607 |
if want is None: |
| 608 |
return None |
| 609 |
w = self.to_tuple(want) if isinstance(want, list) else list() |
| 610 |
h = self.to_tuple(have) if isinstance(have, list) else list() |
| 611 |
if set(w).issubset(set(h)): |
| 612 |
return None |
| 613 |
else: |
| 614 |
return want |
| 615 |
|
| 616 |
@property |
| 617 |
def last_resort_pool(self): |
| 618 |
if self.want.last_resort_pool is None: |
| 619 |
return None |
| 620 |
if self.want.last_resort_pool == 'none' and self.have.last_resort_pool == '': |
| 621 |
return None |
| 622 |
if self.want.last_resort_pool != self.have.last_resort_pool: |
| 623 |
return self.want.last_resort_pool |
| 624 |
|
| 625 |
@property |
| 626 |
def state(self): |
| 627 |
if self.want.state == 'disabled' and self.have.enabled: |
| 628 |
return self.want.state |
| 629 |
elif self.want.state in ['present', 'enabled'] and self.have.disabled: |
| 630 |
return self.want.state |
| 631 |
|
| 632 |
@property |
| 633 |
def pools(self): |
| 634 |
result = self._diff_complex_items(self.want.pools, self.have.pools) |
| 635 |
return result |
| 636 |
|
| 637 |
@property |
| 638 |
def irules(self): |
| 639 |
if self.want.irules is None: |
| 640 |
return None |
| 641 |
if self.want.irules == '' and self.have.irules is None: |
| 642 |
return None |
| 643 |
if self.want.irules == '' and len(self.have.irules) > 0: |
| 644 |
return [] |
| 645 |
if self.have.irules is None: |
| 646 |
return self.want.irules |
| 647 |
if sorted(set(self.want.irules)) != sorted(set(self.have.irules)): |
| 648 |
return self.want.irules |
| 649 |
|
| 650 |
@property |
| 651 |
def aliases(self): |
| 652 |
if self.want.aliases is None: |
| 653 |
return None |
| 654 |
if self.want.aliases == '' and self.have.aliases is None: |
| 655 |
return None |
| 656 |
if self.want.aliases == '' and len(self.have.aliases) > 0: |
| 657 |
return [] |
| 658 |
if self.have.aliases is None: |
| 659 |
return self.want.aliases |
| 660 |
if set(self.want.aliases) != set(self.have.aliases): |
| 661 |
return self.want.aliases |
| 662 |
|
| 663 |
|
| 664 |
class ModuleManager(object): |
| 665 |
def __init__(self, *args, **kwargs): |
| 666 |
self.module = kwargs.get('module', None) |
| 667 |
self.client = F5RestClient(**self.module.params) |
| 668 |
self.want = ModuleParameters(params=self.module.params) |
| 669 |
self.have = ApiParameters() |
| 670 |
self.changes = UsableChanges() |
| 671 |
|
| 672 |
def _set_changed_options(self): |
| 673 |
changed = {} |
| 674 |
for key in Parameters.returnables: |
| 675 |
if getattr(self.want, key) is not None: |
| 676 |
changed[key] = getattr(self.want, key) |
| 677 |
if changed: |
| 678 |
self.changes = UsableChanges(params=changed) |
| 679 |
|
| 680 |
def _update_changed_options(self): |
| 681 |
diff = Difference(self.want, self.have) |
| 682 |
updatables = Parameters.updatables |
| 683 |
changed = dict() |
| 684 |
for k in updatables: |
| 685 |
change = diff.compare(k) |
| 686 |
if change is None: |
| 687 |
continue |
| 688 |
else: |
| 689 |
if isinstance(change, dict): |
| 690 |
changed.update(change) |
| 691 |
else: |
| 692 |
changed[k] = change |
| 693 |
if changed: |
| 694 |
self.changes = UsableChanges(params=changed) |
| 695 |
return True |
| 696 |
return False |
| 697 |
|
| 698 |
def exec_module(self): |
| 699 |
if not module_provisioned(self.client, 'gtm'): |
| 700 |
raise F5ModuleError( |
| 701 |
"GTM must be provisioned to use this module." |
| 702 |
) |
| 703 |
start = datetime.now().isoformat() |
| 704 |
version = tmos_version(self.client) |
| 705 |
changed = False |
| 706 |
result = dict() |
| 707 |
state = self.want.state |
| 708 |
|
| 709 |
if state in ["present", "disabled"]: |
| 710 |
changed = self.present() |
| 711 |
elif state == "absent": |
| 712 |
changed = self.absent() |
| 713 |
|
| 714 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 715 |
changes = reportable.to_return() |
| 716 |
result.update(**changes) |
| 717 |
result.update(dict(changed=changed)) |
| 718 |
self._announce_deprecations(result) |
| 719 |
send_teem(start, self.client, self.module, version) |
| 720 |
return result |
| 721 |
|
| 722 |
def _announce_deprecations(self, result): |
| 723 |
warnings = result.pop('__warnings', []) |
| 724 |
for warning in warnings: |
| 725 |
self.module.deprecate( |
| 726 |
msg=warning['msg'], |
| 727 |
version=warning['version'] |
| 728 |
) |
| 729 |
|
| 730 |
def present(self): |
| 731 |
if self.exists(): |
| 732 |
return self.update() |
| 733 |
else: |
| 734 |
return self.create() |
| 735 |
|
| 736 |
def create(self): |
| 737 |
if self.want.pool_lb_method is None: |
| 738 |
raise F5ModuleError( |
| 739 |
"The 'pool_lb_method' option is required when state is 'present'" |
| 740 |
) |
| 741 |
self._set_changed_options() |
| 742 |
if self.module.check_mode: |
| 743 |
return True |
| 744 |
self.create_on_device() |
| 745 |
return True |
| 746 |
|
| 747 |
def should_update(self): |
| 748 |
result = self._update_changed_options() |
| 749 |
if result: |
| 750 |
return True |
| 751 |
return False |
| 752 |
|
| 753 |
def update(self): |
| 754 |
self.have = self.read_current_from_device() |
| 755 |
if not self.should_update(): |
| 756 |
return False |
| 757 |
if self.module.check_mode: |
| 758 |
return True |
| 759 |
self.update_on_device() |
| 760 |
return True |
| 761 |
|
| 762 |
def absent(self): |
| 763 |
if self.exists(): |
| 764 |
return self.remove() |
| 765 |
return False |
| 766 |
|
| 767 |
def remove(self): |
| 768 |
if self.module.check_mode: |
| 769 |
return True |
| 770 |
self.remove_from_device() |
| 771 |
if self.exists(): |
| 772 |
raise F5ModuleError("Failed to delete the Wide IP") |
| 773 |
return True |
| 774 |
|
| 775 |
def exists(self): |
| 776 |
uri = "https://{0}:{1}/mgmt/tm/gtm/wideip/{2}/{3}".format( |
| 777 |
self.client.provider['server'], |
| 778 |
self.client.provider['server_port'], |
| 779 |
self.want.type, |
| 780 |
transform_name(self.want.partition, self.want.name) |
| 781 |
) |
| 782 |
resp = self.client.api.get(uri) |
| 783 |
try: |
| 784 |
response = resp.json() |
| 785 |
except ValueError as ex: |
| 786 |
raise F5ModuleError(str(ex)) |
| 787 |
|
| 788 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 789 |
return False |
| 790 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 791 |
return True |
| 792 |
|
| 793 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 794 |
|
| 795 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 796 |
if 'message' in response: |
| 797 |
raise F5ModuleError(response['message']) |
| 798 |
else: |
| 799 |
raise F5ModuleError(resp.content) |
| 800 |
|
| 801 |
def update_on_device(self): |
| 802 |
params = self.changes.api_params() |
| 803 |
uri = "https://{0}:{1}/mgmt/tm/gtm/wideip/{2}/{3}".format( |
| 804 |
self.client.provider['server'], |
| 805 |
self.client.provider['server_port'], |
| 806 |
self.want.type, |
| 807 |
transform_name(self.want.partition, self.want.name) |
| 808 |
) |
| 809 |
resp = self.client.api.patch(uri, json=params) |
| 810 |
try: |
| 811 |
response = resp.json() |
| 812 |
except ValueError as ex: |
| 813 |
raise F5ModuleError(str(ex)) |
| 814 |
|
| 815 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 816 |
return True |
| 817 |
raise F5ModuleError(resp.content) |
| 818 |
|
| 819 |
def read_current_from_device(self): |
| 820 |
uri = "https://{0}:{1}/mgmt/tm/gtm/wideip/{2}/{3}".format( |
| 821 |
self.client.provider['server'], |
| 822 |
self.client.provider['server_port'], |
| 823 |
self.want.type, |
| 824 |
transform_name(self.want.partition, self.want.name) |
| 825 |
) |
| 826 |
resp = self.client.api.get(uri) |
| 827 |
try: |
| 828 |
response = resp.json() |
| 829 |
except ValueError as ex: |
| 830 |
raise F5ModuleError(str(ex)) |
| 831 |
|
| 832 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 833 |
return ApiParameters(params=response) |
| 834 |
raise F5ModuleError(resp.content) |
| 835 |
|
| 836 |
def create_on_device(self): |
| 837 |
params = self.changes.api_params() |
| 838 |
params['name'] = self.want.name |
| 839 |
params['partition'] = self.want.partition |
| 840 |
params['disabled'] = self.want.disabled |
| 841 |
params['enabled'] = self.want.enabled |
| 842 |
|
| 843 |
uri = "https://{0}:{1}/mgmt/tm/gtm/wideip/{2}/".format( |
| 844 |
self.client.provider['server'], |
| 845 |
self.client.provider['server_port'], |
| 846 |
self.want.type |
| 847 |
) |
| 848 |
resp = self.client.api.post(uri, json=params) |
| 849 |
try: |
| 850 |
response = resp.json() |
| 851 |
except ValueError as ex: |
| 852 |
raise F5ModuleError(str(ex)) |
| 853 |
|
| 854 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 855 |
return True |
| 856 |
raise F5ModuleError(resp.content) |
| 857 |
|
| 858 |
def remove_from_device(self): |
| 859 |
uri = "https://{0}:{1}/mgmt/tm/gtm/wideip/{2}/{3}".format( |
| 860 |
self.client.provider['server'], |
| 861 |
self.client.provider['server_port'], |
| 862 |
self.want.type, |
| 863 |
transform_name(self.want.partition, self.want.name) |
| 864 |
) |
| 865 |
response = self.client.api.delete(uri) |
| 866 |
if response.status == 200: |
| 867 |
return True |
| 868 |
raise F5ModuleError(response.content) |
| 869 |
|
| 870 |
|
| 871 |
class ArgumentSpec(object): |
| 872 |
def __init__(self): |
| 873 |
lb_method_choices = [ |
| 874 |
'round-robin', 'topology', 'ratio', 'global-availability', |
| 875 |
] |
| 876 |
self.supports_check_mode = True |
| 877 |
argument_spec = dict( |
| 878 |
pool_lb_method=dict( |
| 879 |
choices=lb_method_choices, |
| 880 |
aliases=['lb_method'] |
| 881 |
), |
| 882 |
name=dict( |
| 883 |
required=True, |
| 884 |
aliases=['wide_ip'] |
| 885 |
), |
| 886 |
type=dict( |
| 887 |
choices=[ |
| 888 |
'a', 'aaaa', 'cname', 'mx', 'naptr', 'srv' |
| 889 |
], |
| 890 |
required=True |
| 891 |
), |
| 892 |
state=dict( |
| 893 |
default='present', |
| 894 |
choices=['absent', 'present', 'enabled', 'disabled'] |
| 895 |
), |
| 896 |
pools=dict( |
| 897 |
type='list', |
| 898 |
elements='dict', |
| 899 |
options=dict( |
| 900 |
name=dict(required=True), |
| 901 |
ratio=dict(type='int'), |
| 902 |
order=dict(type='int') |
| 903 |
) |
| 904 |
), |
| 905 |
partition=dict( |
| 906 |
default='Common', |
| 907 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 908 |
), |
| 909 |
irules=dict( |
| 910 |
type='list', |
| 911 |
elements='str', |
| 912 |
), |
| 913 |
aliases=dict( |
| 914 |
type='list', |
| 915 |
elements='str', |
| 916 |
), |
| 917 |
last_resort_pool=dict(), |
| 918 |
persistence=dict(type='bool'), |
| 919 |
persistence_ttl=dict(type='int'), |
| 920 |
persist_cidr_ipv4=dict(type='int'), |
| 921 |
persist_cidr_ipv6=dict(type='int'), |
| 922 |
) |
| 923 |
self.argument_spec = {} |
| 924 |
self.argument_spec.update(f5_argument_spec) |
| 925 |
self.argument_spec.update(argument_spec) |
| 926 |
|
| 927 |
|
| 928 |
def main(): |
| 929 |
spec = ArgumentSpec() |
| 930 |
|
| 931 |
module = AnsibleModule( |
| 932 |
argument_spec=spec.argument_spec, |
| 933 |
supports_check_mode=spec.supports_check_mode, |
| 934 |
) |
| 935 |
|
| 936 |
try: |
| 937 |
mm = ModuleManager(module=module) |
| 938 |
results = mm.exec_module() |
| 939 |
module.exit_json(**results) |
| 940 |
except F5ModuleError as ex: |
| 941 |
module.fail_json(msg=str(ex)) |
| 942 |
|
| 943 |
|
| 944 |
if __name__ == '__main__': |
| 945 |
main() |