| 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_selfip |
| 13 |
short_description: Manage Self-IPs on a BIG-IP system |
| 14 |
description: |
| 15 |
- Manage Self-IP addresses on a BIG-IP system. |
| 16 |
version_added: "1.0.0" |
| 17 |
options: |
| 18 |
address: |
| 19 |
description: |
| 20 |
- The IP addresses for the new self IP. This value is ignored upon update |
| 21 |
as addresses themselves cannot be changed after they are created. |
| 22 |
- This value is required when creating new self IPs. |
| 23 |
- IPv4 and IPv6 are supported. |
| 24 |
type: str |
| 25 |
allow_service: |
| 26 |
description: |
| 27 |
- Configure port lockdown for the self IP. By default, the self IP has a |
| 28 |
"default deny" policy. This can be changed to allow TCP and UDP ports, |
| 29 |
as well as specific protocols. This list should contain C(protocol):C(port) |
| 30 |
values. |
| 31 |
type: list |
| 32 |
elements: str |
| 33 |
name: |
| 34 |
description: |
| 35 |
- The name of the self IP to create. |
| 36 |
- If this parameter is not specified, it defaults to the value supplied |
| 37 |
in the C(address) parameter. |
| 38 |
type: str |
| 39 |
required: True |
| 40 |
description: |
| 41 |
description: |
| 42 |
- Description of the traffic selector. |
| 43 |
type: str |
| 44 |
netmask: |
| 45 |
description: |
| 46 |
- The netmask for the self IP. When creating a new self IP, this value |
| 47 |
is required. |
| 48 |
type: str |
| 49 |
state: |
| 50 |
description: |
| 51 |
- When C(present), guarantees the self IP exists with the provided |
| 52 |
attributes. |
| 53 |
- When C(absent), removes the self IP from the system. |
| 54 |
type: str |
| 55 |
choices: |
| 56 |
- absent |
| 57 |
- present |
| 58 |
default: present |
| 59 |
traffic_group: |
| 60 |
description: |
| 61 |
- The traffic group for the self IP addresses in an active-active, |
| 62 |
redundant load balancer configuration. When creating a new self IP, if |
| 63 |
this value is not specified, the default is C(/Common/traffic-group-local-only). |
| 64 |
type: str |
| 65 |
vlan: |
| 66 |
description: |
| 67 |
- The VLAN for the new self IPs. When creating a new self |
| 68 |
IP, this value is required. |
| 69 |
type: str |
| 70 |
route_domain: |
| 71 |
description: |
| 72 |
- The route domain id of the system. When creating a new self IP, if |
| 73 |
this value is not specified, the default value is C(0). |
| 74 |
- This value cannot be changed after it is set. |
| 75 |
type: int |
| 76 |
fw_enforced_policy: |
| 77 |
description: |
| 78 |
- Specifies an AFM policy to attach to Self IP. |
| 79 |
type: str |
| 80 |
version_added: "1.1.0" |
| 81 |
partition: |
| 82 |
description: |
| 83 |
- Device partition to manage resources on. You can set different partitions |
| 84 |
for self IPs, but the address used may not match any other address used |
| 85 |
by a self IP. Thus, self IPs are not isolated by partitions as |
| 86 |
other resources on a BIG-IP are. |
| 87 |
type: str |
| 88 |
default: Common |
| 89 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 90 |
author: |
| 91 |
- Tim Rupp (@caphrim007) |
| 92 |
- Wojciech Wypior (@wojtek0806) |
| 93 |
''' |
| 94 |
|
| 95 |
EXAMPLES = r''' |
| 96 |
- name: Create Self IP |
| 97 |
bigip_selfip: |
| 98 |
address: 10.10.10.10 |
| 99 |
name: self1 |
| 100 |
netmask: 255.255.255.0 |
| 101 |
vlan: vlan1 |
| 102 |
provider: |
| 103 |
password: secret |
| 104 |
server: lb.mydomain.com |
| 105 |
user: admin |
| 106 |
delegate_to: localhost |
| 107 |
|
| 108 |
- name: Create Self IPv6 |
| 109 |
bigip_selfip: |
| 110 |
address: fe80::10 |
| 111 |
name: self2 |
| 112 |
netmask: "ffff:ffff:ffff:ffff::" |
| 113 |
vlan: vlan1 |
| 114 |
provider: |
| 115 |
password: secret |
| 116 |
server: lb.mydomain.com |
| 117 |
user: admin |
| 118 |
delegate_to: localhost |
| 119 |
|
| 120 |
- name: Create Self IP with a Route Domain |
| 121 |
bigip_selfip: |
| 122 |
name: self1 |
| 123 |
address: 10.10.10.10 |
| 124 |
netmask: 255.255.255.0 |
| 125 |
vlan: vlan1 |
| 126 |
route_domain: 10 |
| 127 |
allow_service: default |
| 128 |
provider: |
| 129 |
server: lb.mydomain.com |
| 130 |
user: admin |
| 131 |
password: secret |
| 132 |
delegate_to: localhost |
| 133 |
|
| 134 |
- name: Delete Self IP |
| 135 |
bigip_selfip: |
| 136 |
name: self1 |
| 137 |
state: absent |
| 138 |
provider: |
| 139 |
user: admin |
| 140 |
password: secret |
| 141 |
server: lb.mydomain.com |
| 142 |
delegate_to: localhost |
| 143 |
|
| 144 |
- name: Allow management web UI to be accessed on this Self IP |
| 145 |
bigip_selfip: |
| 146 |
name: self1 |
| 147 |
state: absent |
| 148 |
allow_service: |
| 149 |
- tcp:443 |
| 150 |
provider: |
| 151 |
password: secret |
| 152 |
server: lb.mydomain.com |
| 153 |
user: admin |
| 154 |
delegate_to: localhost |
| 155 |
|
| 156 |
- name: Allow HTTPS and SSH access to this Self IP |
| 157 |
bigip_selfip: |
| 158 |
name: self1 |
| 159 |
state: absent |
| 160 |
allow_service: |
| 161 |
- tcp:443 |
| 162 |
- tcp:22 |
| 163 |
provider: |
| 164 |
password: secret |
| 165 |
server: lb.mydomain.com |
| 166 |
user: admin |
| 167 |
delegate_to: localhost |
| 168 |
|
| 169 |
- name: Allow all services access to this Self IP |
| 170 |
bigip_selfip: |
| 171 |
name: self1 |
| 172 |
state: absent |
| 173 |
allow_service: |
| 174 |
- all |
| 175 |
provider: |
| 176 |
password: secret |
| 177 |
server: lb.mydomain.com |
| 178 |
user: admin |
| 179 |
delegate_to: localhost |
| 180 |
|
| 181 |
- name: Allow only GRE and IGMP protocols access to this Self IP |
| 182 |
bigip_selfip: |
| 183 |
name: self1 |
| 184 |
state: absent |
| 185 |
allow_service: |
| 186 |
- gre:0 |
| 187 |
- igmp:0 |
| 188 |
provider: |
| 189 |
user: admin |
| 190 |
password: secret |
| 191 |
server: lb.mydomain.com |
| 192 |
delegate_to: localhost |
| 193 |
|
| 194 |
- name: Allow all TCP, but no other protocols access to this Self IP |
| 195 |
bigip_selfip: |
| 196 |
name: self1 |
| 197 |
state: absent |
| 198 |
allow_service: |
| 199 |
- tcp:0 |
| 200 |
provider: |
| 201 |
password: secret |
| 202 |
server: lb.mydomain.com |
| 203 |
user: admin |
| 204 |
delegate_to: localhost |
| 205 |
''' |
| 206 |
|
| 207 |
RETURN = r''' |
| 208 |
allow_service: |
| 209 |
description: Services that are allowed via this self IP. |
| 210 |
returned: changed |
| 211 |
type: list |
| 212 |
sample: ['igmp:0','tcp:22','udp:53'] |
| 213 |
address: |
| 214 |
description: The address for the self IP. |
| 215 |
returned: changed |
| 216 |
type: str |
| 217 |
sample: 192.0.2.10 |
| 218 |
name: |
| 219 |
description: The name of the self IP. |
| 220 |
returned: created |
| 221 |
type: str |
| 222 |
sample: self1 |
| 223 |
netmask: |
| 224 |
description: The netmask of the self IP. |
| 225 |
returned: changed |
| 226 |
type: str |
| 227 |
sample: 255.255.255.0 |
| 228 |
traffic_group: |
| 229 |
description: The traffic group of which the self IP is a member. |
| 230 |
returned: changed |
| 231 |
type: str |
| 232 |
sample: traffic-group-local-only |
| 233 |
vlan: |
| 234 |
description: The VLAN set on the self IP. |
| 235 |
returned: changed |
| 236 |
type: str |
| 237 |
sample: vlan1 |
| 238 |
fw_enforced_policy: |
| 239 |
description: Specifies an AFM policy to be attached to the self IP. |
| 240 |
returned: changed |
| 241 |
type: str |
| 242 |
sample: /Common/afm-blocking-policy |
| 243 |
''' |
| 244 |
|
| 245 |
import re |
| 246 |
from datetime import datetime |
| 247 |
|
| 248 |
from ansible.module_utils.basic import ( |
| 249 |
AnsibleModule, env_fallback |
| 250 |
) |
| 251 |
|
| 252 |
from ipaddress import ( |
| 253 |
ip_network, ip_interface, ip_address |
| 254 |
) |
| 255 |
|
| 256 |
from ..module_utils.bigip import F5RestClient |
| 257 |
from ..module_utils.common import ( |
| 258 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, fq_name |
| 259 |
) |
| 260 |
from ..module_utils.compare import cmp_str_with_none |
| 261 |
from ..module_utils.ipaddress import ( |
| 262 |
is_valid_ip, ipv6_netmask_to_cidr |
| 263 |
) |
| 264 |
from ..module_utils.icontrol import tmos_version |
| 265 |
from ..module_utils.teem import send_teem |
| 266 |
|
| 267 |
|
| 268 |
class Parameters(AnsibleF5Parameters): |
| 269 |
api_map = { |
| 270 |
'trafficGroup': 'traffic_group', |
| 271 |
'allowService': 'allow_service', |
| 272 |
'fwEnforcedPolicy': 'fw_enforced_policy', |
| 273 |
'fwEnforcedPolicyReference': 'fw_policy_link', |
| 274 |
} |
| 275 |
|
| 276 |
api_attributes = [ |
| 277 |
'trafficGroup', |
| 278 |
'allowService', |
| 279 |
'vlan', |
| 280 |
'address', |
| 281 |
'description', |
| 282 |
'fwEnforcedPolicy', |
| 283 |
'fwEnforcedPolicyReference', |
| 284 |
] |
| 285 |
|
| 286 |
updatables = [ |
| 287 |
'traffic_group', |
| 288 |
'allow_service', |
| 289 |
'vlan', |
| 290 |
'netmask', |
| 291 |
'address', |
| 292 |
'description', |
| 293 |
'fw_enforced_policy', |
| 294 |
'fw_policy_link', |
| 295 |
] |
| 296 |
|
| 297 |
returnables = [ |
| 298 |
'traffic_group', |
| 299 |
'allow_service', |
| 300 |
'vlan', |
| 301 |
'route_domain', |
| 302 |
'netmask', |
| 303 |
'address', |
| 304 |
'description', |
| 305 |
] |
| 306 |
|
| 307 |
@property |
| 308 |
def vlan(self): |
| 309 |
if self._values['vlan'] is None: |
| 310 |
return None |
| 311 |
return fq_name(self.partition, self._values['vlan']) |
| 312 |
|
| 313 |
|
| 314 |
class ModuleParameters(Parameters): |
| 315 |
@property |
| 316 |
def address(self): |
| 317 |
address = "{0}%{1}/{2}".format( |
| 318 |
self.ip, self.route_domain, self.netmask |
| 319 |
) |
| 320 |
return address |
| 321 |
|
| 322 |
@property |
| 323 |
def ip(self): |
| 324 |
if self._values['address'] is None: |
| 325 |
return None |
| 326 |
if is_valid_ip(self._values['address']): |
| 327 |
return self._values['address'] |
| 328 |
else: |
| 329 |
raise F5ModuleError( |
| 330 |
'The provided address is not a valid IP address' |
| 331 |
) |
| 332 |
|
| 333 |
@property |
| 334 |
def traffic_group(self): |
| 335 |
if self._values['traffic_group'] is None: |
| 336 |
return None |
| 337 |
return fq_name(self.partition, self._values['traffic_group']) |
| 338 |
|
| 339 |
@property |
| 340 |
def route_domain(self): |
| 341 |
if self._values['route_domain'] is None: |
| 342 |
return None |
| 343 |
result = int(self._values['route_domain']) |
| 344 |
return result |
| 345 |
|
| 346 |
@property |
| 347 |
def netmask(self): |
| 348 |
if self._values['netmask'] is None: |
| 349 |
return None |
| 350 |
result = -1 |
| 351 |
try: |
| 352 |
result = int(self._values['netmask']) |
| 353 |
if 0 < result < 256: |
| 354 |
pass |
| 355 |
except ValueError: |
| 356 |
if is_valid_ip(self._values['netmask']): |
| 357 |
addr = ip_address(u'{0}'.format(str(self._values['netmask']))) |
| 358 |
if addr.version == 4: |
| 359 |
ip = ip_network(u'0.0.0.0/%s' % str(self._values['netmask'])) |
| 360 |
result = ip.prefixlen |
| 361 |
else: |
| 362 |
result = ipv6_netmask_to_cidr(self._values['netmask']) |
| 363 |
if result < 0: |
| 364 |
raise F5ModuleError( |
| 365 |
'The provided netmask {0} is neither in IP or CIDR format'.format(result) |
| 366 |
) |
| 367 |
return result |
| 368 |
|
| 369 |
@property |
| 370 |
def allow_service(self): |
| 371 |
"""Verifies that a supplied service string has correct format |
| 372 |
|
| 373 |
The string format for port lockdown is PROTOCOL:PORT. This method |
| 374 |
will verify that the provided input matches the allowed protocols |
| 375 |
and the port ranges before submitting to BIG-IP. |
| 376 |
|
| 377 |
The only allowed exceptions to this rule are the following values |
| 378 |
|
| 379 |
* all |
| 380 |
* default |
| 381 |
* none |
| 382 |
|
| 383 |
These are special cases that are handled differently in the API. |
| 384 |
"all" is set as a string, "default" is set as a one item list, and |
| 385 |
"none" removes the key entirely from the REST API. |
| 386 |
|
| 387 |
:raises F5ModuleError: |
| 388 |
""" |
| 389 |
if self._values['allow_service'] is None: |
| 390 |
return None |
| 391 |
result = [] |
| 392 |
allowed_protocols = [ |
| 393 |
'eigrp', 'egp', 'gre', 'icmp', 'igmp', 'igp', 'ipip', |
| 394 |
'l2tp', 'ospf', 'pim', 'tcp', 'udp' |
| 395 |
] |
| 396 |
special_protocols = [ |
| 397 |
'all', 'none', 'default' |
| 398 |
] |
| 399 |
for svc in self._values['allow_service']: |
| 400 |
if svc in special_protocols: |
| 401 |
result = [svc] |
| 402 |
break |
| 403 |
elif svc in allowed_protocols: |
| 404 |
full_service = '{0}:0'.format(svc) |
| 405 |
result.append(full_service) |
| 406 |
else: |
| 407 |
tmp = svc.split(':') |
| 408 |
if tmp[0] not in allowed_protocols: |
| 409 |
raise F5ModuleError( |
| 410 |
"The provided protocol '%s' is invalid" % (tmp[0]) |
| 411 |
) |
| 412 |
try: |
| 413 |
port = int(tmp[1]) |
| 414 |
except Exception: |
| 415 |
raise F5ModuleError( |
| 416 |
"The provided port '%s' is not a number" % (tmp[1]) |
| 417 |
) |
| 418 |
|
| 419 |
if port < 0 or port > 65535: |
| 420 |
raise F5ModuleError( |
| 421 |
"The provided port '{0}' must be between 0 and 65535".format(port) |
| 422 |
) |
| 423 |
else: |
| 424 |
result.append(svc) |
| 425 |
result = sorted(list(set(result))) |
| 426 |
return result |
| 427 |
|
| 428 |
@property |
| 429 |
def fw_enforced_policy(self): |
| 430 |
if self._values['fw_enforced_policy'] is None: |
| 431 |
return None |
| 432 |
if self._values['fw_enforced_policy'] in ['none', '']: |
| 433 |
return None |
| 434 |
name = self._values['fw_enforced_policy'] |
| 435 |
return fq_name(self.partition, name) |
| 436 |
|
| 437 |
@property |
| 438 |
def fw_policy_link(self): |
| 439 |
policy = self.fw_enforced_policy |
| 440 |
if policy is None: |
| 441 |
return None |
| 442 |
tmp = policy.split('/') |
| 443 |
link = dict(link='https://localhost/mgmt/tm/security/firewall/policy/~{0}~{1}'.format(tmp[1], tmp[2])) |
| 444 |
return link |
| 445 |
|
| 446 |
@property |
| 447 |
def description(self): |
| 448 |
if self._values['description'] is None: |
| 449 |
return None |
| 450 |
elif self._values['description'] in ['none', '']: |
| 451 |
return '' |
| 452 |
return self._values['description'] |
| 453 |
|
| 454 |
|
| 455 |
class ApiParameters(Parameters): |
| 456 |
@property |
| 457 |
def allow_service(self): |
| 458 |
if self._values['allow_service'] is None: |
| 459 |
return None |
| 460 |
if self._values['allow_service'] == 'all': |
| 461 |
self._values['allow_service'] = ['all'] |
| 462 |
return sorted(self._values['allow_service']) |
| 463 |
|
| 464 |
@property |
| 465 |
def destination_ip(self): |
| 466 |
if self._values['address'] is None: |
| 467 |
return None |
| 468 |
try: |
| 469 |
pattern = r'(?P<rd>%[0-9]+)' |
| 470 |
addr = re.sub(pattern, '', self._values['address']) |
| 471 |
ip = ip_interface(u'{0}'.format(addr)) |
| 472 |
return ip.with_prefixlen |
| 473 |
except ValueError: |
| 474 |
raise F5ModuleError( |
| 475 |
"The provided destination is not an IP address" |
| 476 |
) |
| 477 |
|
| 478 |
@property |
| 479 |
def netmask(self): |
| 480 |
ip = ip_interface(self.destination_ip) |
| 481 |
return int(ip.network.prefixlen) |
| 482 |
|
| 483 |
@property |
| 484 |
def ip(self): |
| 485 |
result = ip_interface(self.destination_ip) |
| 486 |
return str(result.ip) |
| 487 |
|
| 488 |
@property |
| 489 |
def description(self): |
| 490 |
if self._values['description'] in [None, 'none']: |
| 491 |
return None |
| 492 |
return self._values['description'] |
| 493 |
|
| 494 |
|
| 495 |
class Changes(Parameters): |
| 496 |
def to_return(self): |
| 497 |
result = {} |
| 498 |
try: |
| 499 |
for returnable in self.returnables: |
| 500 |
result[returnable] = getattr(self, returnable) |
| 501 |
result = self._filter_params(result) |
| 502 |
except Exception: |
| 503 |
pass |
| 504 |
return result |
| 505 |
|
| 506 |
|
| 507 |
class UsableChanges(Changes): |
| 508 |
@property |
| 509 |
def allow_service(self): |
| 510 |
if self._values['allow_service'] is None: |
| 511 |
return None |
| 512 |
if self._values['allow_service'] == ['all']: |
| 513 |
return 'all' |
| 514 |
return sorted(self._values['allow_service']) |
| 515 |
|
| 516 |
|
| 517 |
class ReportableChanges(Changes): |
| 518 |
pass |
| 519 |
|
| 520 |
|
| 521 |
class Difference(object): |
| 522 |
def __init__(self, want, have=None): |
| 523 |
self.want = want |
| 524 |
self.have = have |
| 525 |
|
| 526 |
def compare(self, param): |
| 527 |
try: |
| 528 |
result = getattr(self, param) |
| 529 |
return result |
| 530 |
except AttributeError: |
| 531 |
return self.__default(param) |
| 532 |
|
| 533 |
def __default(self, param): |
| 534 |
attr1 = getattr(self.want, param) |
| 535 |
try: |
| 536 |
attr2 = getattr(self.have, param) |
| 537 |
if attr1 != attr2: |
| 538 |
return attr1 |
| 539 |
except AttributeError: |
| 540 |
return attr1 |
| 541 |
|
| 542 |
@property |
| 543 |
def address(self): |
| 544 |
return None |
| 545 |
|
| 546 |
@property |
| 547 |
def allow_service(self): |
| 548 |
"""Returns services formatted for consumption by f5-sdk update |
| 549 |
|
| 550 |
The BIG-IP endpoint for services takes different values depending on |
| 551 |
what you want the "allowed services" to be. It can be any of the |
| 552 |
following |
| 553 |
|
| 554 |
- a list containing "protocol:port" values |
| 555 |
- the string "all" |
| 556 |
- a null value, or None |
| 557 |
|
| 558 |
This is a convenience function to massage the values the user has |
| 559 |
supplied so that they are formatted in such a way that BIG-IP will |
| 560 |
accept them and apply the specified policy. |
| 561 |
""" |
| 562 |
if self.want.allow_service is None: |
| 563 |
return None |
| 564 |
result = self.want.allow_service |
| 565 |
if result[0] == 'none' and self.have.allow_service is None: |
| 566 |
return None |
| 567 |
elif self.have.allow_service is None: |
| 568 |
return result |
| 569 |
elif result[0] == 'all' and self.have.allow_service[0] != 'all': |
| 570 |
return ['all'] |
| 571 |
elif result[0] == 'none': |
| 572 |
return [] |
| 573 |
elif set(self.want.allow_service) != set(self.have.allow_service): |
| 574 |
return result |
| 575 |
|
| 576 |
@property |
| 577 |
def netmask(self): |
| 578 |
if self.want.netmask is None: |
| 579 |
return None |
| 580 |
ip = self.have.ip |
| 581 |
if is_valid_ip(ip): |
| 582 |
if self.want.route_domain is not None: |
| 583 |
want = "{0}%{1}/{2}".format(ip, self.want.route_domain, self.want.netmask) |
| 584 |
have = "{0}%{1}/{2}".format(ip, self.want.route_domain, self.have.netmask) |
| 585 |
elif self.have.route_domain is not None: |
| 586 |
want = "{0}%{1}/{2}".format(ip, self.have.route_domain, self.want.netmask) |
| 587 |
have = "{0}%{1}/{2}".format(ip, self.have.route_domain, self.have.netmask) |
| 588 |
else: |
| 589 |
want = "{0}/{1}".format(ip, self.want.netmask) |
| 590 |
have = "{0}/{1}".format(ip, self.have.netmask) |
| 591 |
if want != have: |
| 592 |
return want |
| 593 |
else: |
| 594 |
raise F5ModuleError( |
| 595 |
'The provided address/netmask value "{0}" was invalid'.format(self.have.ip) |
| 596 |
) |
| 597 |
|
| 598 |
@property |
| 599 |
def traffic_group(self): |
| 600 |
if self.want.traffic_group != self.have.traffic_group: |
| 601 |
return self.want.traffic_group |
| 602 |
|
| 603 |
@property |
| 604 |
def description(self): |
| 605 |
return cmp_str_with_none(self.want.description, self.have.description) |
| 606 |
|
| 607 |
@property |
| 608 |
def fw_policy_link(self): |
| 609 |
if self.want.fw_enforced_policy is None: |
| 610 |
return None |
| 611 |
if self.want.fw_enforced_policy == self.have.fw_enforced_policy: |
| 612 |
return None |
| 613 |
if self.want.fw_policy_link != self.have.fw_policy_link: |
| 614 |
return self.want.fw_policy_link |
| 615 |
|
| 616 |
|
| 617 |
class ModuleManager(object): |
| 618 |
def __init__(self, *args, **kwargs): |
| 619 |
self.module = kwargs.get('module', None) |
| 620 |
self.client = F5RestClient(**self.module.params) |
| 621 |
self.have = None |
| 622 |
self.want = ModuleParameters(params=self.module.params) |
| 623 |
self.changes = UsableChanges() |
| 624 |
|
| 625 |
def _set_changed_options(self): |
| 626 |
changed = {} |
| 627 |
for key in Parameters.returnables: |
| 628 |
if getattr(self.want, key) is not None: |
| 629 |
changed[key] = getattr(self.want, key) |
| 630 |
if changed: |
| 631 |
self.changes = UsableChanges(params=changed) |
| 632 |
|
| 633 |
def _update_changed_options(self): |
| 634 |
diff = Difference(self.want, self.have) |
| 635 |
updatables = ApiParameters.updatables |
| 636 |
changed = dict() |
| 637 |
for k in updatables: |
| 638 |
change = diff.compare(k) |
| 639 |
if change is None: |
| 640 |
continue |
| 641 |
else: |
| 642 |
if k in ['netmask']: |
| 643 |
changed['address'] = change |
| 644 |
else: |
| 645 |
changed[k] = change |
| 646 |
if changed: |
| 647 |
self.changes = UsableChanges(params=changed) |
| 648 |
return True |
| 649 |
return False |
| 650 |
|
| 651 |
def _announce_deprecations(self, result): |
| 652 |
warnings = result.pop('__warnings', []) |
| 653 |
for warning in warnings: |
| 654 |
self.client.module.deprecate( |
| 655 |
msg=warning['msg'], |
| 656 |
version=warning['version'] |
| 657 |
) |
| 658 |
|
| 659 |
def exec_module(self): |
| 660 |
start = datetime.now().isoformat() |
| 661 |
version = tmos_version(self.client) |
| 662 |
changed = False |
| 663 |
result = dict() |
| 664 |
state = self.want.state |
| 665 |
|
| 666 |
if state == "present": |
| 667 |
changed = self.present() |
| 668 |
elif state == "absent": |
| 669 |
changed = self.absent() |
| 670 |
|
| 671 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 672 |
changes = reportable.to_return() |
| 673 |
result.update(**changes) |
| 674 |
result.update(dict(changed=changed)) |
| 675 |
self._announce_deprecations(result) |
| 676 |
send_teem(start, self.client, self.module, version) |
| 677 |
return result |
| 678 |
|
| 679 |
def present(self): |
| 680 |
if self.exists(): |
| 681 |
changed = self.update() |
| 682 |
else: |
| 683 |
changed = self.create() |
| 684 |
return changed |
| 685 |
|
| 686 |
def absent(self): |
| 687 |
changed = False |
| 688 |
if self.exists(): |
| 689 |
changed = self.remove() |
| 690 |
return changed |
| 691 |
|
| 692 |
def remove(self): |
| 693 |
if self.module.check_mode: |
| 694 |
return True |
| 695 |
self.remove_from_device() |
| 696 |
if self.exists(): |
| 697 |
raise F5ModuleError("Failed to delete the Self IP") |
| 698 |
return True |
| 699 |
|
| 700 |
def should_update(self): |
| 701 |
result = self._update_changed_options() |
| 702 |
if result: |
| 703 |
return True |
| 704 |
return False |
| 705 |
|
| 706 |
def update(self): |
| 707 |
self.have = self.read_current_from_device() |
| 708 |
if not self.should_update(): |
| 709 |
return False |
| 710 |
if self.module.check_mode: |
| 711 |
return True |
| 712 |
self.update_on_device() |
| 713 |
return True |
| 714 |
|
| 715 |
def create(self): |
| 716 |
if self.want.address is None or self.want.netmask is None: |
| 717 |
raise F5ModuleError( |
| 718 |
'An address and a netmask must be specified' |
| 719 |
) |
| 720 |
if self.want.vlan is None: |
| 721 |
raise F5ModuleError( |
| 722 |
'A VLAN name must be specified' |
| 723 |
) |
| 724 |
if self.want.route_domain is None: |
| 725 |
rd = self.read_partition_default_route_domain_from_device() |
| 726 |
self.want.update({'route_domain': rd}) |
| 727 |
|
| 728 |
if self.want.traffic_group is None: |
| 729 |
self.want.update({'traffic_group': '/Common/traffic-group-local-only'}) |
| 730 |
if self.want.route_domain is None: |
| 731 |
self.want.update({'route_domain': 0}) |
| 732 |
if self.want.allow_service: |
| 733 |
if 'all' in self.want.allow_service: |
| 734 |
self.want.update(dict(allow_service=['all'])) |
| 735 |
elif 'none' in self.want.allow_service: |
| 736 |
self.want.update(dict(allow_service=[])) |
| 737 |
elif 'default' in self.want.allow_service: |
| 738 |
self.want.update(dict(allow_service=['default'])) |
| 739 |
self._set_changed_options() |
| 740 |
if self.module.check_mode: |
| 741 |
return True |
| 742 |
self.create_on_device() |
| 743 |
if self.exists(): |
| 744 |
return True |
| 745 |
else: |
| 746 |
raise F5ModuleError("Failed to create the Self IP") |
| 747 |
|
| 748 |
def exists(self): |
| 749 |
uri = "https://{0}:{1}/mgmt/tm/net/self/{2}".format( |
| 750 |
self.client.provider['server'], |
| 751 |
self.client.provider['server_port'], |
| 752 |
transform_name(self.want.partition, self.want.name) |
| 753 |
) |
| 754 |
resp = self.client.api.get(uri) |
| 755 |
try: |
| 756 |
response = resp.json() |
| 757 |
except ValueError as ex: |
| 758 |
raise F5ModuleError(str(ex)) |
| 759 |
|
| 760 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 761 |
return False |
| 762 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 763 |
return True |
| 764 |
|
| 765 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 766 |
|
| 767 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 768 |
if 'message' in response: |
| 769 |
raise F5ModuleError(response['message']) |
| 770 |
else: |
| 771 |
raise F5ModuleError(resp.content) |
| 772 |
|
| 773 |
def create_on_device(self): |
| 774 |
params = self.changes.api_params() |
| 775 |
params['name'] = self.want.name |
| 776 |
params['partition'] = self.want.partition |
| 777 |
uri = "https://{0}:{1}/mgmt/tm/net/self/".format( |
| 778 |
self.client.provider['server'], |
| 779 |
self.client.provider['server_port'], |
| 780 |
) |
| 781 |
resp = self.client.api.post(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'] in [400, 403]: |
| 788 |
if 'message' in response: |
| 789 |
raise F5ModuleError(response['message']) |
| 790 |
else: |
| 791 |
raise F5ModuleError(resp.content) |
| 792 |
|
| 793 |
if self.want.fw_enforced_policy: |
| 794 |
payload = dict( |
| 795 |
fwEnforcedPolicy=self.want.fw_enforced_policy, |
| 796 |
fwEnforcedPolicyReference=self.want.fw_policy_link |
| 797 |
) |
| 798 |
uri = "https://{0}:{1}/mgmt/tm/net/self/{2}".format( |
| 799 |
self.client.provider['server'], |
| 800 |
self.client.provider['server_port'], |
| 801 |
transform_name(self.want.partition, self.want.name), |
| 802 |
) |
| 803 |
resp = self.client.api.patch(uri, json=payload) |
| 804 |
|
| 805 |
try: |
| 806 |
response = resp.json() |
| 807 |
except ValueError as ex: |
| 808 |
raise F5ModuleError(str(ex)) |
| 809 |
|
| 810 |
if 'code' in response and response['code'] in [400, 403]: |
| 811 |
if 'message' in response: |
| 812 |
raise F5ModuleError(response['message']) |
| 813 |
else: |
| 814 |
raise F5ModuleError(resp.content) |
| 815 |
return True |
| 816 |
|
| 817 |
def update_on_device(self): |
| 818 |
params = self.changes.api_params() |
| 819 |
uri = "https://{0}:{1}/mgmt/tm/net/self/{2}".format( |
| 820 |
self.client.provider['server'], |
| 821 |
self.client.provider['server_port'], |
| 822 |
transform_name(self.want.partition, self.want.name) |
| 823 |
) |
| 824 |
resp = self.client.api.patch(uri, json=params) |
| 825 |
try: |
| 826 |
response = resp.json() |
| 827 |
except ValueError as ex: |
| 828 |
raise F5ModuleError(str(ex)) |
| 829 |
|
| 830 |
if 'code' in response and response['code'] == 400: |
| 831 |
if 'message' in response: |
| 832 |
raise F5ModuleError(response['message']) |
| 833 |
else: |
| 834 |
raise F5ModuleError(resp.content) |
| 835 |
|
| 836 |
def remove_from_device(self): |
| 837 |
uri = "https://{0}:{1}/mgmt/tm/net/self/{2}".format( |
| 838 |
self.client.provider['server'], |
| 839 |
self.client.provider['server_port'], |
| 840 |
transform_name(self.want.partition, self.want.name) |
| 841 |
) |
| 842 |
resp = self.client.api.delete(uri) |
| 843 |
if resp.status == 200: |
| 844 |
return True |
| 845 |
|
| 846 |
def read_current_from_device(self): |
| 847 |
uri = "https://{0}:{1}/mgmt/tm/net/self/{2}".format( |
| 848 |
self.client.provider['server'], |
| 849 |
self.client.provider['server_port'], |
| 850 |
transform_name(self.want.partition, self.want.name) |
| 851 |
) |
| 852 |
resp = self.client.api.get(uri) |
| 853 |
try: |
| 854 |
response = resp.json() |
| 855 |
except ValueError as ex: |
| 856 |
raise F5ModuleError(str(ex)) |
| 857 |
|
| 858 |
if 'code' in response and response['code'] == 400: |
| 859 |
if 'message' in response: |
| 860 |
raise F5ModuleError(response['message']) |
| 861 |
else: |
| 862 |
raise F5ModuleError(resp.content) |
| 863 |
return ApiParameters(params=response) |
| 864 |
|
| 865 |
def read_partition_default_route_domain_from_device(self): |
| 866 |
uri = "https://{0}:{1}/mgmt/tm/auth/partition/{2}".format( |
| 867 |
self.client.provider['server'], |
| 868 |
self.client.provider['server_port'], |
| 869 |
self.want.partition |
| 870 |
) |
| 871 |
resp = self.client.api.get(uri) |
| 872 |
try: |
| 873 |
response = resp.json() |
| 874 |
except ValueError as ex: |
| 875 |
raise F5ModuleError(str(ex)) |
| 876 |
|
| 877 |
if 'code' in response and response['code'] == 400: |
| 878 |
if 'message' in response: |
| 879 |
raise F5ModuleError(response['message']) |
| 880 |
else: |
| 881 |
raise F5ModuleError(resp.content) |
| 882 |
return int(response['defaultRouteDomain']) |
| 883 |
|
| 884 |
|
| 885 |
class ArgumentSpec(object): |
| 886 |
def __init__(self): |
| 887 |
self.supports_check_mode = True |
| 888 |
argument_spec = dict( |
| 889 |
address=dict(), |
| 890 |
allow_service=dict( |
| 891 |
elements='str', |
| 892 |
type='list', |
| 893 |
), |
| 894 |
name=dict(required=True), |
| 895 |
netmask=dict(), |
| 896 |
traffic_group=dict(), |
| 897 |
vlan=dict(), |
| 898 |
route_domain=dict(type='int'), |
| 899 |
description=dict(), |
| 900 |
fw_enforced_policy=dict(), |
| 901 |
state=dict( |
| 902 |
default='present', |
| 903 |
choices=['present', 'absent'] |
| 904 |
), |
| 905 |
partition=dict( |
| 906 |
default='Common', |
| 907 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 908 |
) |
| 909 |
) |
| 910 |
self.argument_spec = {} |
| 911 |
self.argument_spec.update(f5_argument_spec) |
| 912 |
self.argument_spec.update(argument_spec) |
| 913 |
|
| 914 |
|
| 915 |
def main(): |
| 916 |
spec = ArgumentSpec() |
| 917 |
|
| 918 |
module = AnsibleModule( |
| 919 |
argument_spec=spec.argument_spec, |
| 920 |
supports_check_mode=spec.supports_check_mode |
| 921 |
) |
| 922 |
|
| 923 |
try: |
| 924 |
mm = ModuleManager(module=module) |
| 925 |
results = mm.exec_module() |
| 926 |
module.exit_json(**results) |
| 927 |
except F5ModuleError as ex: |
| 928 |
module.fail_json(msg=str(ex)) |
| 929 |
|
| 930 |
|
| 931 |
if __name__ == '__main__': |
| 932 |
main() |