| 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_virtual_address |
| 13 |
short_description: Manage LTM virtual addresses on a BIG-IP |
| 14 |
description: |
| 15 |
- Manage LTM virtual addresses on a BIG-IP system. |
| 16 |
version_added: "1.0.0" |
| 17 |
options: |
| 18 |
name: |
| 19 |
description: |
| 20 |
- Name of the virtual address. |
| 21 |
- If this parameter is not provided, the system uses the value of C(address). |
| 22 |
type: str |
| 23 |
address: |
| 24 |
description: |
| 25 |
- Specifies the virtual address. This value cannot be modified after it is set. |
| 26 |
- If you never created a virtual address, but did create virtual servers, |
| 27 |
a virtual address for each virtual server was created automatically. The name |
| 28 |
of this virtual address is its IP address value. |
| 29 |
type: str |
| 30 |
netmask: |
| 31 |
description: |
| 32 |
- Specifies the netmask of the provided virtual address. This value cannot be |
| 33 |
modified after it is set. |
| 34 |
- When creating a new virtual address, if this parameter is not specified, the |
| 35 |
default value is C(255.255.255.255) for IPv4 addresses and |
| 36 |
C(ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff) for IPv6 addresses. |
| 37 |
type: str |
| 38 |
connection_limit: |
| 39 |
description: |
| 40 |
- Specifies the number of concurrent connections the system |
| 41 |
allows on this virtual address. |
| 42 |
type: int |
| 43 |
arp: |
| 44 |
description: |
| 45 |
- Specifies whether the system accepts ARP requests. |
| 46 |
- When C(false), specifies the system does not accept ARP requests. |
| 47 |
- When C(true), the packets are dropped. |
| 48 |
- Both ARP and ICMP Echo must be disabled in order for forwarding |
| 49 |
virtual servers using that virtual address to forward ICMP packets. |
| 50 |
- When creating a new virtual address, if this parameter is not specified, |
| 51 |
the default value is C(true). |
| 52 |
type: bool |
| 53 |
auto_delete: |
| 54 |
description: |
| 55 |
- Specifies whether the system automatically deletes the virtual |
| 56 |
address with the deletion of the last associated virtual server. |
| 57 |
When C(false), specifies the system leaves the virtual |
| 58 |
address, even when all associated virtual servers have been deleted. |
| 59 |
When creating the virtual address, the default value is C(true). |
| 60 |
type: bool |
| 61 |
icmp_echo: |
| 62 |
description: |
| 63 |
- Specifies how the system sends responses to (ICMP) echo requests |
| 64 |
on a per-virtual address basis for enabling route advertisement. |
| 65 |
When C(enabled), the BIG-IP system intercepts ICMP echo request |
| 66 |
packets and responds to them directly. When C(disabled), the BIG-IP |
| 67 |
system passes ICMP echo requests through to the backend servers. |
| 68 |
When (selective), causes the BIG-IP system to internally enable or |
| 69 |
disable responses based on virtual server state; C(when_any_available), |
| 70 |
C(when_all_available, or C(always), regardless of the state of any |
| 71 |
virtual servers. |
| 72 |
type: str |
| 73 |
choices: |
| 74 |
- enabled |
| 75 |
- disabled |
| 76 |
- selective |
| 77 |
state: |
| 78 |
description: |
| 79 |
- The virtual address state. If C(absent), the system makes an attempt |
| 80 |
to delete the virtual address. This will only succeed if this |
| 81 |
virtual address is not in use by a virtual server. C(present) creates |
| 82 |
the virtual address and enables it. If C(enabled), enables the virtual |
| 83 |
address if it exists. If C(disabled), creates the virtual address if |
| 84 |
needed, and sets the state to C(disabled). |
| 85 |
type: str |
| 86 |
choices: |
| 87 |
- present |
| 88 |
- absent |
| 89 |
- enabled |
| 90 |
- disabled |
| 91 |
default: present |
| 92 |
availability_calculation: |
| 93 |
description: |
| 94 |
- Specifies which routes of the virtual address the system advertises. |
| 95 |
When C(when_any_available), advertises the route when any virtual |
| 96 |
server is available. When C(when_all_available), advertises the |
| 97 |
route when all virtual servers are available. When (always), always |
| 98 |
advertises the route regardless of the virtual servers available. |
| 99 |
type: str |
| 100 |
choices: |
| 101 |
- always |
| 102 |
- when_all_available |
| 103 |
- when_any_available |
| 104 |
aliases: ['advertise_route'] |
| 105 |
route_advertisement: |
| 106 |
description: |
| 107 |
- Specifies whether the system uses route advertisement for this |
| 108 |
virtual address. |
| 109 |
- When disabled, the system does not advertise routes for this virtual address. |
| 110 |
- The majority of these options are only supported on versions 13.0.0-HF1 or |
| 111 |
later. On versions prior than this, all choices expect C(disabled) |
| 112 |
translate to C(enabled). |
| 113 |
- When C(always), the BIG-IP system always advertises the route for the |
| 114 |
virtual address, regardless of availability status. This requires an C(enabled) |
| 115 |
virtual address. |
| 116 |
- When C(enabled), the BIG-IP system advertises the route for the available |
| 117 |
virtual address, based on the calculation method in the availability calculation. |
| 118 |
- When C(disabled), the BIG-IP system does not advertise the route for the virtual |
| 119 |
address, regardless of the availability status. |
| 120 |
- When C(selective), you can also selectively enable ICMP echo responses, which |
| 121 |
causes the BIG-IP system to internally enable or disable responses based on |
| 122 |
virtual server state. |
| 123 |
- When C(any), the BIG-IP system advertises the route for the virtual address |
| 124 |
when any virtual server is available. |
| 125 |
- When C(all), the BIG-IP system advertises the route for the virtual address |
| 126 |
when all virtual servers are available. |
| 127 |
type: str |
| 128 |
choices: |
| 129 |
- disabled |
| 130 |
- enabled |
| 131 |
- always |
| 132 |
- selective |
| 133 |
- any |
| 134 |
- all |
| 135 |
partition: |
| 136 |
description: |
| 137 |
- Device partition to manage resources on. |
| 138 |
type: str |
| 139 |
default: Common |
| 140 |
traffic_group: |
| 141 |
description: |
| 142 |
- The traffic group for the virtual address. When creating a new address, |
| 143 |
if this value is not specified, the default is C(/Common/traffic-group-1). |
| 144 |
type: str |
| 145 |
route_domain: |
| 146 |
description: |
| 147 |
- The route domain of the C(address) you want to use. |
| 148 |
- This value cannot be modified after it is set. |
| 149 |
type: str |
| 150 |
spanning: |
| 151 |
description: |
| 152 |
- Enables all BIG-IP systems in a device group to listen for and process traffic |
| 153 |
on the same virtual address. |
| 154 |
- Spanning for a virtual address occurs when you enable the C(spanning) option on a |
| 155 |
device, and then sync the virtual address to the other members of the device group. |
| 156 |
- Spanning also relies on the upstream router to distribute application flows to the |
| 157 |
BIG-IP systems using ECMP routes. ECMP defines a route to the virtual address using |
| 158 |
distinct Floating self-IP addresses configured on each BIG-IP system. |
| 159 |
- You must also configure MAC masquerade addresses and disable C(arp) on the virtual |
| 160 |
address when Spanning is enabled. |
| 161 |
- When creating a new virtual address, if this parameter is not specified, the default |
| 162 |
valus is C(false). |
| 163 |
type: bool |
| 164 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 165 |
author: |
| 166 |
- Tim Rupp (@caphrim007) |
| 167 |
- Wojciech Wypior (@wojtek0806) |
| 168 |
''' |
| 169 |
|
| 170 |
EXAMPLES = r''' |
| 171 |
- name: Add virtual address |
| 172 |
bigip_virtual_address: |
| 173 |
state: present |
| 174 |
partition: Common |
| 175 |
address: 10.10.10.10 |
| 176 |
provider: |
| 177 |
server: lb.mydomain.net |
| 178 |
user: admin |
| 179 |
password: secret |
| 180 |
delegate_to: localhost |
| 181 |
|
| 182 |
- name: Enable route advertisement on the virtual address |
| 183 |
bigip_virtual_address: |
| 184 |
state: present |
| 185 |
address: 10.10.10.10 |
| 186 |
route_advertisement: any |
| 187 |
provider: |
| 188 |
server: lb.mydomain.net |
| 189 |
user: admin |
| 190 |
password: secret |
| 191 |
delegate_to: localhost |
| 192 |
''' |
| 193 |
|
| 194 |
RETURN = r''' |
| 195 |
availability_calculation: |
| 196 |
description: Specifies which routes of the virtual address the system advertises. |
| 197 |
returned: changed |
| 198 |
type: str |
| 199 |
sample: always |
| 200 |
auto_delete: |
| 201 |
description: New setting for auto deleting virtual address. |
| 202 |
returned: changed |
| 203 |
type: bool |
| 204 |
sample: true |
| 205 |
icmp_echo: |
| 206 |
description: New ICMP echo setting applied to virtual address. |
| 207 |
returned: changed |
| 208 |
type: str |
| 209 |
sample: disabled |
| 210 |
connection_limit: |
| 211 |
description: The new connection limit of the virtual address. |
| 212 |
returned: changed |
| 213 |
type: int |
| 214 |
sample: 1000 |
| 215 |
netmask: |
| 216 |
description: The netmask of the virtual address. |
| 217 |
returned: created |
| 218 |
type: int |
| 219 |
sample: 2345 |
| 220 |
arp: |
| 221 |
description: The new way the virtual address handles ARP requests. |
| 222 |
returned: changed |
| 223 |
type: bool |
| 224 |
sample: true |
| 225 |
address: |
| 226 |
description: The address of the virtual address. |
| 227 |
returned: created |
| 228 |
type: int |
| 229 |
sample: 2345 |
| 230 |
state: |
| 231 |
description: The new state of the virtual address. |
| 232 |
returned: changed |
| 233 |
type: str |
| 234 |
sample: disabled |
| 235 |
spanning: |
| 236 |
description: Whether spanning is enabled or not. |
| 237 |
returned: changed |
| 238 |
type: str |
| 239 |
sample: disabled |
| 240 |
''' |
| 241 |
import traceback |
| 242 |
from datetime import datetime |
| 243 |
|
| 244 |
try: |
| 245 |
from packaging.version import Version |
| 246 |
except ImportError: |
| 247 |
HAS_PACKAGING = False |
| 248 |
Version = None |
| 249 |
PACKAGING_IMPORT_ERROR = traceback.format_exc() |
| 250 |
else: |
| 251 |
HAS_PACKAGING = True |
| 252 |
PACKAGING_IMPORT_ERROR = None |
| 253 |
|
| 254 |
from ansible.module_utils.basic import ( |
| 255 |
AnsibleModule, missing_required_lib, env_fallback |
| 256 |
) |
| 257 |
from ansible.module_utils.parsing.convert_bool import ( |
| 258 |
BOOLEANS_TRUE, BOOLEANS_FALSE |
| 259 |
) |
| 260 |
|
| 261 |
from ..module_utils.bigip import F5RestClient |
| 262 |
from ..module_utils.common import ( |
| 263 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, fq_name, flatten_boolean |
| 264 |
) |
| 265 |
from ..module_utils.icontrol import tmos_version |
| 266 |
from ..module_utils.ipaddress import ( |
| 267 |
is_valid_ip, compress_address |
| 268 |
) |
| 269 |
from ..module_utils.teem import send_teem |
| 270 |
|
| 271 |
|
| 272 |
class Parameters(AnsibleF5Parameters): |
| 273 |
api_map = { |
| 274 |
'routeAdvertisement': 'route_advertisement_type', |
| 275 |
'autoDelete': 'auto_delete', |
| 276 |
'icmpEcho': 'icmp_echo', |
| 277 |
'connectionLimit': 'connection_limit', |
| 278 |
'serverScope': 'availability_calculation', |
| 279 |
'mask': 'netmask', |
| 280 |
'trafficGroup': 'traffic_group', |
| 281 |
} |
| 282 |
|
| 283 |
updatables = [ |
| 284 |
'route_advertisement_type', |
| 285 |
'auto_delete', |
| 286 |
'icmp_echo', |
| 287 |
'connection_limit', |
| 288 |
'arp', |
| 289 |
'enabled', |
| 290 |
'availability_calculation', |
| 291 |
'traffic_group', |
| 292 |
'spanning', |
| 293 |
] |
| 294 |
|
| 295 |
returnables = [ |
| 296 |
'route_advertisement_type', |
| 297 |
'auto_delete', |
| 298 |
'icmp_echo', |
| 299 |
'connection_limit', |
| 300 |
'netmask', |
| 301 |
'arp', |
| 302 |
'address', |
| 303 |
'state', |
| 304 |
'traffic_group', |
| 305 |
'route_domain', |
| 306 |
'spanning', |
| 307 |
'availability_calculation', |
| 308 |
] |
| 309 |
|
| 310 |
api_attributes = [ |
| 311 |
'routeAdvertisement', |
| 312 |
'autoDelete', |
| 313 |
'icmpEcho', |
| 314 |
'connectionLimit', |
| 315 |
'advertiseRoute', |
| 316 |
'arp', |
| 317 |
'mask', |
| 318 |
'enabled', |
| 319 |
'serverScope', |
| 320 |
'trafficGroup', |
| 321 |
'spanning', |
| 322 |
'serverScope', |
| 323 |
] |
| 324 |
|
| 325 |
@property |
| 326 |
def availability_calculation(self): |
| 327 |
if self._values['availability_calculation'] is None: |
| 328 |
return None |
| 329 |
elif self._values['availability_calculation'] in ['any', 'when_any_available']: |
| 330 |
return 'any' |
| 331 |
elif self._values['availability_calculation'] in ['all', 'when_all_available']: |
| 332 |
return 'all' |
| 333 |
elif self._values['availability_calculation'] in ['none', 'always']: |
| 334 |
return 'none' |
| 335 |
|
| 336 |
@property |
| 337 |
def connection_limit(self): |
| 338 |
if self._values['connection_limit'] is None: |
| 339 |
return None |
| 340 |
return int(self._values['connection_limit']) |
| 341 |
|
| 342 |
@property |
| 343 |
def enabled(self): |
| 344 |
if self._values['state'] in ['enabled', 'present']: |
| 345 |
return 'yes' |
| 346 |
elif self._values['enabled'] in BOOLEANS_TRUE: |
| 347 |
return 'yes' |
| 348 |
elif self._values['state'] == 'disabled': |
| 349 |
return 'no' |
| 350 |
elif self._values['enabled'] in BOOLEANS_FALSE: |
| 351 |
return 'no' |
| 352 |
else: |
| 353 |
return None |
| 354 |
|
| 355 |
@property |
| 356 |
def netmask(self): |
| 357 |
if self._values['netmask'] is None: |
| 358 |
return None |
| 359 |
if is_valid_ip(self._values['netmask']): |
| 360 |
return self._values['netmask'] |
| 361 |
else: |
| 362 |
raise F5ModuleError( |
| 363 |
"The provided 'netmask' is not a valid IP address" |
| 364 |
) |
| 365 |
|
| 366 |
@property |
| 367 |
def auto_delete(self): |
| 368 |
result = flatten_boolean(self._values['auto_delete']) |
| 369 |
if result == 'yes': |
| 370 |
return 'true' |
| 371 |
if result == 'no': |
| 372 |
return 'false' |
| 373 |
|
| 374 |
@property |
| 375 |
def state(self): |
| 376 |
if self.enabled == 'yes' and self._values['state'] != 'present': |
| 377 |
return 'enabled' |
| 378 |
elif self.enabled == 'no': |
| 379 |
return 'disabled' |
| 380 |
else: |
| 381 |
return self._values['state'] |
| 382 |
|
| 383 |
@property |
| 384 |
def traffic_group(self): |
| 385 |
if self._values['traffic_group'] is None: |
| 386 |
return None |
| 387 |
else: |
| 388 |
result = fq_name(self.partition, self._values['traffic_group']) |
| 389 |
if result.startswith('/Common/'): |
| 390 |
return result |
| 391 |
else: |
| 392 |
raise F5ModuleError( |
| 393 |
"Traffic groups can only exist in /Common" |
| 394 |
) |
| 395 |
|
| 396 |
@property |
| 397 |
def route_advertisement_type(self): |
| 398 |
if self.route_advertisement: |
| 399 |
return self.route_advertisement |
| 400 |
else: |
| 401 |
return self._values['route_advertisement_type'] |
| 402 |
|
| 403 |
@property |
| 404 |
def route_advertisement(self): |
| 405 |
if self._values['route_advertisement'] is None: |
| 406 |
return None |
| 407 |
version = tmos_version(self.client) |
| 408 |
if Version(version) <= Version('13.0.0'): |
| 409 |
if self._values['route_advertisement'] == 'disabled': |
| 410 |
return 'disabled' |
| 411 |
else: |
| 412 |
return 'enabled' |
| 413 |
else: |
| 414 |
return self._values['route_advertisement'] |
| 415 |
|
| 416 |
|
| 417 |
class ApiParameters(Parameters): |
| 418 |
pass |
| 419 |
|
| 420 |
|
| 421 |
class ModuleParameters(Parameters): |
| 422 |
@property |
| 423 |
def address(self): |
| 424 |
if self._values['address'] is None: |
| 425 |
return None |
| 426 |
if is_valid_ip(self._values['address']): |
| 427 |
return compress_address(self._values['address']) |
| 428 |
else: |
| 429 |
raise F5ModuleError( |
| 430 |
"The provided 'address' is not a valid IP address" |
| 431 |
) |
| 432 |
|
| 433 |
@property |
| 434 |
def full_address(self): |
| 435 |
if self.route_domain is not None: |
| 436 |
return '{0}%{1}'.format(self.address, self.route_domain) |
| 437 |
return self.address |
| 438 |
|
| 439 |
@property |
| 440 |
def name(self): |
| 441 |
if self._values['name'] is None: |
| 442 |
result = str(self.address) |
| 443 |
if self.route_domain: |
| 444 |
result = "{0}%{1}".format(result, self.route_domain) |
| 445 |
else: |
| 446 |
result = self._values['name'] |
| 447 |
return result |
| 448 |
|
| 449 |
@property |
| 450 |
def route_domain(self): |
| 451 |
if self._values['route_domain'] is None: |
| 452 |
return None |
| 453 |
try: |
| 454 |
return int(self._values['route_domain']) |
| 455 |
except ValueError: |
| 456 |
uri = "https://{0}:{1}/mgmt/tm/net/route-domain/{2}".format( |
| 457 |
self.client.provider['server'], |
| 458 |
self.client.provider['server_port'], |
| 459 |
transform_name(self._values['partition'], self._values['route_domain']) |
| 460 |
) |
| 461 |
resp = self.client.api.get(uri) |
| 462 |
try: |
| 463 |
response = resp.json() |
| 464 |
except ValueError: |
| 465 |
raise F5ModuleError( |
| 466 |
"The specified 'route_domain' was not found." |
| 467 |
) |
| 468 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 469 |
raise F5ModuleError( |
| 470 |
"The specified 'route_domain' was not found." |
| 471 |
) |
| 472 |
|
| 473 |
return int(response['id']) |
| 474 |
|
| 475 |
@property |
| 476 |
def arp(self): |
| 477 |
result = flatten_boolean(self._values['arp']) |
| 478 |
if result == 'yes': |
| 479 |
return 'enabled' |
| 480 |
if result == 'no': |
| 481 |
return 'disabled' |
| 482 |
|
| 483 |
@property |
| 484 |
def spanning(self): |
| 485 |
result = flatten_boolean(self._values['spanning']) |
| 486 |
if result == 'yes': |
| 487 |
return 'enabled' |
| 488 |
if result == 'no': |
| 489 |
return 'disabled' |
| 490 |
|
| 491 |
|
| 492 |
class Changes(Parameters): |
| 493 |
def to_return(self): |
| 494 |
result = {} |
| 495 |
try: |
| 496 |
for returnable in self.returnables: |
| 497 |
result[returnable] = getattr(self, returnable) |
| 498 |
result = self._filter_params(result) |
| 499 |
except Exception: |
| 500 |
raise |
| 501 |
return result |
| 502 |
|
| 503 |
|
| 504 |
class UsableChanges(Changes): |
| 505 |
@property |
| 506 |
def address(self): |
| 507 |
if self._values['address'] is None: |
| 508 |
return None |
| 509 |
if self._values['route_domain'] is None: |
| 510 |
return self._values['address'] |
| 511 |
result = "{0}%{1}".format(self._values['address'], self.route_domain) |
| 512 |
return result |
| 513 |
|
| 514 |
|
| 515 |
class ReportableChanges(Changes): |
| 516 |
@property |
| 517 |
def arp(self): |
| 518 |
if self._values['arp'] == 'disabled': |
| 519 |
return 'no' |
| 520 |
elif self._values['arp'] == 'enabled': |
| 521 |
return 'yes' |
| 522 |
|
| 523 |
@property |
| 524 |
def spanning(self): |
| 525 |
if self._values['spanning'] == 'disabled': |
| 526 |
return 'no' |
| 527 |
elif self._values['spanning'] == 'enabled': |
| 528 |
return 'yes' |
| 529 |
|
| 530 |
|
| 531 |
class Difference(object): |
| 532 |
def __init__(self, want, have=None): |
| 533 |
self.want = want |
| 534 |
self.have = have |
| 535 |
|
| 536 |
def compare(self, param): |
| 537 |
try: |
| 538 |
result = getattr(self, param) |
| 539 |
return result |
| 540 |
except AttributeError: |
| 541 |
return self.__default(param) |
| 542 |
|
| 543 |
def __default(self, param): |
| 544 |
attr1 = getattr(self.want, param) |
| 545 |
try: |
| 546 |
attr2 = getattr(self.have, param) |
| 547 |
if attr1 != attr2: |
| 548 |
return attr1 |
| 549 |
except AttributeError: |
| 550 |
return attr1 |
| 551 |
|
| 552 |
@property |
| 553 |
def traffic_group(self): |
| 554 |
if self.want.traffic_group != self.have.traffic_group: |
| 555 |
return self.want.traffic_group |
| 556 |
|
| 557 |
@property |
| 558 |
def spanning(self): |
| 559 |
if self.want.spanning is None: |
| 560 |
return None |
| 561 |
if self.want.spanning != self.have.spanning: |
| 562 |
return self.want.spanning |
| 563 |
|
| 564 |
@property |
| 565 |
def arp(self): |
| 566 |
if self.want.arp is None: |
| 567 |
return None |
| 568 |
if self.want.arp != self.have.arp: |
| 569 |
return self.want.arp |
| 570 |
|
| 571 |
|
| 572 |
class ModuleManager(object): |
| 573 |
def __init__(self, *args, **kwargs): |
| 574 |
self.module = kwargs.get('module', None) |
| 575 |
self.client = F5RestClient(**self.module.params) |
| 576 |
self.have = ApiParameters() |
| 577 |
self.want = ModuleParameters(client=self.client, params=self.module.params) |
| 578 |
self.changes = UsableChanges() |
| 579 |
|
| 580 |
def _set_changed_options(self): |
| 581 |
changed = {} |
| 582 |
for key in Parameters.returnables: |
| 583 |
if getattr(self.want, key) is not None: |
| 584 |
changed[key] = getattr(self.want, key) |
| 585 |
if changed: |
| 586 |
self.changes = UsableChanges(params=changed) |
| 587 |
|
| 588 |
def _update_changed_options(self): |
| 589 |
diff = Difference(self.want, self.have) |
| 590 |
updatables = Parameters.updatables |
| 591 |
changed = dict() |
| 592 |
for k in updatables: |
| 593 |
change = diff.compare(k) |
| 594 |
if change is None: |
| 595 |
continue |
| 596 |
else: |
| 597 |
if isinstance(change, dict): |
| 598 |
changed.update(change) |
| 599 |
else: |
| 600 |
changed[k] = change |
| 601 |
if changed: |
| 602 |
self.changes = UsableChanges(params=changed) |
| 603 |
return True |
| 604 |
return False |
| 605 |
|
| 606 |
def _announce_deprecations(self, result): |
| 607 |
warnings = result.pop('__warnings', []) |
| 608 |
for warning in warnings: |
| 609 |
self.client.module.deprecate( |
| 610 |
msg=warning['msg'], |
| 611 |
version=warning['version'] |
| 612 |
) |
| 613 |
|
| 614 |
def exec_module(self): |
| 615 |
start = datetime.now().isoformat() |
| 616 |
version = tmos_version(self.client) |
| 617 |
changed = False |
| 618 |
result = dict() |
| 619 |
state = self.want.state |
| 620 |
|
| 621 |
if state in ['present', 'enabled', 'disabled']: |
| 622 |
changed = self.present() |
| 623 |
elif state == "absent": |
| 624 |
changed = self.absent() |
| 625 |
|
| 626 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 627 |
changes = reportable.to_return() |
| 628 |
result.update(**changes) |
| 629 |
|
| 630 |
if self.module._diff and self.have: |
| 631 |
result['diff'] = self.make_diff() |
| 632 |
|
| 633 |
result.update(dict(changed=changed)) |
| 634 |
self._announce_deprecations(result) |
| 635 |
send_teem(start, self.client, self.module, version) |
| 636 |
return result |
| 637 |
|
| 638 |
def _grab_attr(self, item): |
| 639 |
result = dict() |
| 640 |
updatables = Parameters.updatables |
| 641 |
for k in updatables: |
| 642 |
if getattr(item, k) is not None: |
| 643 |
result[k] = getattr(item, k) |
| 644 |
return result |
| 645 |
|
| 646 |
def make_diff(self): |
| 647 |
result = dict(before=self._grab_attr(self.have), after=self._grab_attr(self.want)) |
| 648 |
return result |
| 649 |
|
| 650 |
def should_update(self): |
| 651 |
result = self._update_changed_options() |
| 652 |
if result: |
| 653 |
return True |
| 654 |
return False |
| 655 |
|
| 656 |
def present(self): |
| 657 |
if self.exists(): |
| 658 |
return self.update() |
| 659 |
else: |
| 660 |
return self.create() |
| 661 |
|
| 662 |
def absent(self): |
| 663 |
changed = False |
| 664 |
if self.exists(): |
| 665 |
changed = self.remove() |
| 666 |
return changed |
| 667 |
|
| 668 |
def remove(self): |
| 669 |
if self.module.check_mode: |
| 670 |
return True |
| 671 |
self.remove_from_device() |
| 672 |
if self.exists(): |
| 673 |
raise F5ModuleError("Failed to delete the virtual address") |
| 674 |
return True |
| 675 |
|
| 676 |
def create(self): |
| 677 |
self._set_changed_options() |
| 678 |
|
| 679 |
if self.want.traffic_group is None: |
| 680 |
self.want.update({'traffic_group': '/Common/traffic-group-1'}) |
| 681 |
if self.want.arp is None: |
| 682 |
self.want.update({'arp': True}) |
| 683 |
if self.want.spanning is None: |
| 684 |
self.want.update({'spanning': False}) |
| 685 |
|
| 686 |
if self.want.netmask is None: |
| 687 |
if is_valid_ip(self.want.address, type='ipv4'): |
| 688 |
self.want.update({'netmask': '255.255.255.255'}) |
| 689 |
else: |
| 690 |
self.want.update({'netmask': 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'}) |
| 691 |
|
| 692 |
if self.want.arp == 'enabled' and self.want.spanning == 'enabled': |
| 693 |
raise F5ModuleError( |
| 694 |
"'arp' and 'spanning' cannot both be enabled on virtual address." |
| 695 |
) |
| 696 |
if self.module.check_mode: |
| 697 |
return True |
| 698 |
self.create_on_device() |
| 699 |
if self.exists(): |
| 700 |
return True |
| 701 |
else: |
| 702 |
raise F5ModuleError("Failed to create the virtual address") |
| 703 |
|
| 704 |
def update(self): |
| 705 |
self.have = self.read_current_from_device() |
| 706 |
if self.want.netmask is not None: |
| 707 |
if self.have.netmask != self.want.netmask: |
| 708 |
raise F5ModuleError( |
| 709 |
"The netmask cannot be changed. Delete and recreate " |
| 710 |
"the virtual address if you need to do this." |
| 711 |
) |
| 712 |
if self.want.address is not None: |
| 713 |
if self.have.address != self.want.full_address: |
| 714 |
raise F5ModuleError( |
| 715 |
"The address cannot be changed. Delete and recreate " |
| 716 |
"the virtual address if you need to do this." |
| 717 |
) |
| 718 |
if self.changes.arp == 'enabled' and self.changes.spanning == 'enabled': |
| 719 |
raise F5ModuleError( |
| 720 |
"'arp' and 'spanning' cannot both be enabled on virtual address." |
| 721 |
) |
| 722 |
|
| 723 |
if not self.should_update(): |
| 724 |
return False |
| 725 |
if self.module.check_mode: |
| 726 |
return True |
| 727 |
self.update_on_device() |
| 728 |
return True |
| 729 |
|
| 730 |
def exists(self): |
| 731 |
uri = "https://{0}:{1}/mgmt/tm/ltm/virtual-address/{2}".format( |
| 732 |
self.client.provider['server'], |
| 733 |
self.client.provider['server_port'], |
| 734 |
transform_name(self.want.partition, self.want.name) |
| 735 |
) |
| 736 |
resp = self.client.api.get(uri) |
| 737 |
try: |
| 738 |
response = resp.json() |
| 739 |
except ValueError as ex: |
| 740 |
raise F5ModuleError(str(ex)) |
| 741 |
|
| 742 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 743 |
return False |
| 744 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 745 |
return True |
| 746 |
|
| 747 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 748 |
|
| 749 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 750 |
if 'message' in response: |
| 751 |
raise F5ModuleError(response['message']) |
| 752 |
else: |
| 753 |
raise F5ModuleError(resp.content) |
| 754 |
|
| 755 |
def read_current_from_device(self): |
| 756 |
uri = "https://{0}:{1}/mgmt/tm/ltm/virtual-address/{2}".format( |
| 757 |
self.client.provider['server'], |
| 758 |
self.client.provider['server_port'], |
| 759 |
transform_name(self.want.partition, self.want.name) |
| 760 |
) |
| 761 |
resp = self.client.api.get(uri) |
| 762 |
try: |
| 763 |
response = resp.json() |
| 764 |
except ValueError as ex: |
| 765 |
raise F5ModuleError(str(ex)) |
| 766 |
|
| 767 |
if 'code' in response and response['code'] == 400: |
| 768 |
if 'message' in response: |
| 769 |
raise F5ModuleError(response['message']) |
| 770 |
else: |
| 771 |
raise F5ModuleError(resp.content) |
| 772 |
return ApiParameters(params=response) |
| 773 |
|
| 774 |
def update_on_device(self): |
| 775 |
params = self.changes.api_params() |
| 776 |
uri = "https://{0}:{1}/mgmt/tm/ltm/virtual-address/{2}".format( |
| 777 |
self.client.provider['server'], |
| 778 |
self.client.provider['server_port'], |
| 779 |
transform_name(self.want.partition, self.want.name) |
| 780 |
) |
| 781 |
resp = self.client.api.patch(uri, json=params) |
| 782 |
try: |
| 783 |
response = resp.json() |
| 784 |
except ValueError as ex: |
| 785 |
raise F5ModuleError(str(ex)) |
| 786 |
|
| 787 |
if 'code' in response and response['code'] == 400: |
| 788 |
if 'message' in response: |
| 789 |
raise F5ModuleError(response['message']) |
| 790 |
else: |
| 791 |
raise F5ModuleError(resp.content) |
| 792 |
|
| 793 |
def create_on_device(self): |
| 794 |
params = self.changes.api_params() |
| 795 |
params['name'] = self.want.name |
| 796 |
params['partition'] = self.want.partition |
| 797 |
params['address'] = self.changes.address |
| 798 |
uri = "https://{0}:{1}/mgmt/tm/ltm/virtual-address/".format( |
| 799 |
self.client.provider['server'], |
| 800 |
self.client.provider['server_port'], |
| 801 |
) |
| 802 |
resp = self.client.api.post(uri, json=params) |
| 803 |
try: |
| 804 |
response = resp.json() |
| 805 |
except ValueError as ex: |
| 806 |
raise F5ModuleError(str(ex)) |
| 807 |
|
| 808 |
if 'code' in response and response['code'] in [400, 403, 409]: |
| 809 |
if 'message' in response: |
| 810 |
raise F5ModuleError(response['message']) |
| 811 |
else: |
| 812 |
raise F5ModuleError(resp.content) |
| 813 |
|
| 814 |
return response['selfLink'] |
| 815 |
|
| 816 |
def remove_from_device(self): |
| 817 |
uri = "https://{0}:{1}/mgmt/tm/ltm/virtual-address/{2}".format( |
| 818 |
self.client.provider['server'], |
| 819 |
self.client.provider['server_port'], |
| 820 |
transform_name(self.want.partition, self.want.name) |
| 821 |
) |
| 822 |
resp = self.client.api.delete(uri) |
| 823 |
if resp.status == 200: |
| 824 |
return True |
| 825 |
|
| 826 |
|
| 827 |
class ArgumentSpec(object): |
| 828 |
def __init__(self): |
| 829 |
self.supports_check_mode = True |
| 830 |
argument_spec = dict( |
| 831 |
state=dict( |
| 832 |
default='present', |
| 833 |
choices=['present', 'absent', 'disabled', 'enabled'] |
| 834 |
), |
| 835 |
name=dict(), |
| 836 |
address=dict(), |
| 837 |
netmask=dict(), |
| 838 |
connection_limit=dict( |
| 839 |
type='int' |
| 840 |
), |
| 841 |
|
| 842 |
auto_delete=dict( |
| 843 |
type='bool' |
| 844 |
), |
| 845 |
icmp_echo=dict( |
| 846 |
choices=['enabled', 'disabled', 'selective'], |
| 847 |
), |
| 848 |
availability_calculation=dict( |
| 849 |
choices=['always', 'when_all_available', 'when_any_available'], |
| 850 |
aliases=['advertise_route'] |
| 851 |
), |
| 852 |
traffic_group=dict(), |
| 853 |
partition=dict( |
| 854 |
default='Common', |
| 855 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 856 |
), |
| 857 |
route_domain=dict(), |
| 858 |
spanning=dict(type='bool'), |
| 859 |
route_advertisement=dict( |
| 860 |
choices=[ |
| 861 |
'disabled', |
| 862 |
'enabled', |
| 863 |
'always', |
| 864 |
'selective', |
| 865 |
'any', |
| 866 |
'all', |
| 867 |
] |
| 868 |
), |
| 869 |
arp=dict(type='bool'), |
| 870 |
) |
| 871 |
self.argument_spec = {} |
| 872 |
self.argument_spec.update(f5_argument_spec) |
| 873 |
self.argument_spec.update(argument_spec) |
| 874 |
self.required_one_of = [ |
| 875 |
['name', 'address'] |
| 876 |
] |
| 877 |
|
| 878 |
|
| 879 |
def main(): |
| 880 |
spec = ArgumentSpec() |
| 881 |
|
| 882 |
module = AnsibleModule( |
| 883 |
argument_spec=spec.argument_spec, |
| 884 |
supports_check_mode=spec.supports_check_mode, |
| 885 |
required_one_of=spec.required_one_of |
| 886 |
) |
| 887 |
|
| 888 |
if not HAS_PACKAGING: |
| 889 |
module.fail_json( |
| 890 |
msg=missing_required_lib('packaging'), |
| 891 |
exception=PACKAGING_IMPORT_ERROR |
| 892 |
) |
| 893 |
|
| 894 |
try: |
| 895 |
mm = ModuleManager(module=module) |
| 896 |
results = mm.exec_module() |
| 897 |
module.exit_json(**results) |
| 898 |
except F5ModuleError as ex: |
| 899 |
module.fail_json(msg=str(ex)) |
| 900 |
|
| 901 |
|
| 902 |
if __name__ == '__main__': |
| 903 |
main() |