| 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_vlan |
| 13 |
short_description: Manage VLANs on a BIG-IP system |
| 14 |
description: |
| 15 |
- Manage VLANs on a BIG-IP system |
| 16 |
version_added: "1.0.0" |
| 17 |
options: |
| 18 |
description: |
| 19 |
description: |
| 20 |
- The description of the VLAN. |
| 21 |
type: str |
| 22 |
tagged_interfaces: |
| 23 |
description: |
| 24 |
- Specifies a list of tagged interfaces and trunks you want to |
| 25 |
configure for the VLAN. Use tagged interfaces or trunks when |
| 26 |
you want to assign a single interface or trunk to multiple VLANs. |
| 27 |
- This parameter is mutually exclusive with the C(untagged_interfaces) |
| 28 |
and C(interfaces) parameters. |
| 29 |
type: list |
| 30 |
elements: str |
| 31 |
aliases: |
| 32 |
- tagged_interface |
| 33 |
untagged_interfaces: |
| 34 |
description: |
| 35 |
- Specifies a list of untagged interfaces and trunks you want to |
| 36 |
configure for the VLAN. |
| 37 |
- This parameter is mutually exclusive with the C(tagged_interfaces) |
| 38 |
and C(interfaces) parameters. |
| 39 |
type: list |
| 40 |
elements: str |
| 41 |
aliases: |
| 42 |
- untagged_interface |
| 43 |
name: |
| 44 |
description: |
| 45 |
- The VLAN to manage. If the special VLAN C(ALL) is specified with |
| 46 |
the C(state) value of C(absent), all VLANs will be removed. |
| 47 |
type: str |
| 48 |
required: True |
| 49 |
state: |
| 50 |
description: |
| 51 |
- The state of the VLAN on the system. When C(present), guarantees |
| 52 |
the VLAN exists with the provided attributes. When C(absent), |
| 53 |
removes the VLAN from the system. |
| 54 |
type: str |
| 55 |
choices: |
| 56 |
- absent |
| 57 |
- present |
| 58 |
default: present |
| 59 |
tag: |
| 60 |
description: |
| 61 |
- Tag number for the VLAN. The tag number can be any integer between 1 |
| 62 |
and 4094. The system automatically assigns a tag number if you do not |
| 63 |
specify a value. |
| 64 |
type: int |
| 65 |
mtu: |
| 66 |
description: |
| 67 |
- Specifies the maximum transmission unit (MTU) for traffic on this VLAN. |
| 68 |
When creating a new VLAN, if this parameter is not specified, the default |
| 69 |
value used is C(1500). |
| 70 |
- This number must be between 576 to 9198. |
| 71 |
type: int |
| 72 |
cmp_hash: |
| 73 |
description: |
| 74 |
- Specifies how the traffic on the VLAN is disaggregated. The value |
| 75 |
you select determines the traffic disaggregation method. You can choose to |
| 76 |
disaggregate traffic based on C(source-address) (the source IP address), |
| 77 |
C(destination-address) (destination IP address), or C(default), which |
| 78 |
specifies the default CMP hash uses L4 ports. |
| 79 |
- When creating a new VLAN, if this parameter is not specified, the default |
| 80 |
is C(default). |
| 81 |
type: str |
| 82 |
choices: |
| 83 |
- default |
| 84 |
- destination-address |
| 85 |
- source-address |
| 86 |
- dst-ip |
| 87 |
- src-ip |
| 88 |
- dest |
| 89 |
- destination |
| 90 |
- source |
| 91 |
- dst |
| 92 |
- src |
| 93 |
dag_tunnel: |
| 94 |
description: |
| 95 |
- Specifies how the disaggregator (DAG) distributes received tunnel-encapsulated |
| 96 |
packets to TMM instances. Select C(inner) to distribute packets based on information |
| 97 |
in inner headers. Select C(outer) to distribute packets based on information in |
| 98 |
outer headers without inspecting inner headers. |
| 99 |
- When creating a new VLAN, if this parameter is not specified, the default |
| 100 |
is C(outer). |
| 101 |
- This parameter is not supported on Virtual Editions (VEs) of BIG-IP. |
| 102 |
type: str |
| 103 |
choices: |
| 104 |
- inner |
| 105 |
- outer |
| 106 |
dag_round_robin: |
| 107 |
description: |
| 108 |
- Specifies whether some of the stateless traffic on the VLAN should be |
| 109 |
disaggregated in a round-robin order instead of using a static hash. The |
| 110 |
stateless traffic includes non-IP L2 traffic, ICMP, some UDP protocols, |
| 111 |
and so on. |
| 112 |
- When creating a new VLAN, if this parameter is not specified, the default |
| 113 |
is (false). |
| 114 |
type: bool |
| 115 |
partition: |
| 116 |
description: |
| 117 |
- Device partition to manage resources on. |
| 118 |
type: str |
| 119 |
default: Common |
| 120 |
source_check: |
| 121 |
description: |
| 122 |
- When C(true), specifies the system verifies the return route to an initial |
| 123 |
packet is the same VLAN from which the packet originated. |
| 124 |
- The system performs this verification only if the C(auto_last_hop) option is C(false). |
| 125 |
type: bool |
| 126 |
fail_safe: |
| 127 |
description: |
| 128 |
- When C(true), specifies the VLAN takes the specified C(fail_safe_action) if the |
| 129 |
system detects a loss of traffic on this VLAN's interfaces. |
| 130 |
type: bool |
| 131 |
fail_safe_timeout: |
| 132 |
description: |
| 133 |
- Specifies the number of seconds a system can run without detecting network |
| 134 |
traffic on this VLAN before it takes the C(fail_safe_action). |
| 135 |
type: int |
| 136 |
fail_safe_action: |
| 137 |
description: |
| 138 |
- Specifies the action the system takes when it does not detect any traffic on |
| 139 |
this VLAN, and the C(fail_safe_timeout) has expired. |
| 140 |
type: str |
| 141 |
choices: |
| 142 |
- reboot |
| 143 |
- restart-all |
| 144 |
- failover |
| 145 |
sflow_poll_interval: |
| 146 |
description: |
| 147 |
- Specifies the maximum interval in seconds between two pollings. |
| 148 |
type: int |
| 149 |
sflow_sampling_rate: |
| 150 |
description: |
| 151 |
- Specifies the ratio of packets observed to the samples generated. |
| 152 |
type: int |
| 153 |
interfaces: |
| 154 |
description: |
| 155 |
- Interfaces you want to add to the VLAN. This can include both tagged |
| 156 |
and untagged interfaces, as the C(tagging) parameter specifies. |
| 157 |
- This parameter is mutually exclusive with the C(untagged_interfaces) and |
| 158 |
C(tagged_interfaces) parameters. |
| 159 |
type: list |
| 160 |
elements: dict |
| 161 |
suboptions: |
| 162 |
interface: |
| 163 |
description: |
| 164 |
- The name of the interface |
| 165 |
type: str |
| 166 |
tagging: |
| 167 |
description: |
| 168 |
- Whether the interface is C(tagged) or C(untagged). |
| 169 |
type: str |
| 170 |
choices: |
| 171 |
- tagged |
| 172 |
- untagged |
| 173 |
hw_syn_cookie: |
| 174 |
description: |
| 175 |
- Enables hardware syncookie mode on a VLAN. |
| 176 |
- When C(true), the hardware per-VLAN SYN cookie protection is triggered when the certain traffic threshold |
| 177 |
is reached on supported platforms. |
| 178 |
type: bool |
| 179 |
version_added: "1.3.0" |
| 180 |
notes: |
| 181 |
- Requires BIG-IP versions >= 12.0.0 |
| 182 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 183 |
author: |
| 184 |
- Tim Rupp (@caphrim007) |
| 185 |
- Wojciech Wypior (@wojtek0806) |
| 186 |
''' |
| 187 |
|
| 188 |
EXAMPLES = r''' |
| 189 |
- name: Create VLAN |
| 190 |
bigip_vlan: |
| 191 |
name: net1 |
| 192 |
provider: |
| 193 |
password: secret |
| 194 |
server: lb.mydomain.com |
| 195 |
user: admin |
| 196 |
delegate_to: localhost |
| 197 |
|
| 198 |
- name: Set VLAN tag |
| 199 |
bigip_vlan: |
| 200 |
name: net1 |
| 201 |
tag: 2345 |
| 202 |
provider: |
| 203 |
user: admin |
| 204 |
password: secret |
| 205 |
server: lb.mydomain.com |
| 206 |
delegate_to: localhost |
| 207 |
|
| 208 |
- name: Add VLAN 2345 as tagged to interface 1.1 |
| 209 |
bigip_vlan: |
| 210 |
tagged_interface: 1.1 |
| 211 |
name: net1 |
| 212 |
tag: 2345 |
| 213 |
provider: |
| 214 |
password: secret |
| 215 |
server: lb.mydomain.com |
| 216 |
user: admin |
| 217 |
delegate_to: localhost |
| 218 |
|
| 219 |
- name: Add VLAN 1234 as tagged to interfaces 1.1 and 1.2 |
| 220 |
bigip_vlan: |
| 221 |
tagged_interfaces: |
| 222 |
- 1.1 |
| 223 |
- 1.2 |
| 224 |
name: net1 |
| 225 |
tag: 1234 |
| 226 |
provider: |
| 227 |
user: admin |
| 228 |
password: secret |
| 229 |
server: lb.mydomain.com |
| 230 |
delegate_to: localhost |
| 231 |
''' |
| 232 |
|
| 233 |
RETURN = r''' |
| 234 |
description: |
| 235 |
description: The description set on the VLAN. |
| 236 |
returned: changed |
| 237 |
type: str |
| 238 |
sample: foo VLAN |
| 239 |
interfaces: |
| 240 |
description: Interfaces the VLAN is assigned to. |
| 241 |
returned: changed |
| 242 |
type: list |
| 243 |
sample: ['1.1','1.2'] |
| 244 |
partition: |
| 245 |
description: The partition the VLAN was created on. |
| 246 |
returned: changed |
| 247 |
type: str |
| 248 |
sample: Common |
| 249 |
tag: |
| 250 |
description: The ID of the VLAN. |
| 251 |
returned: changed |
| 252 |
type: int |
| 253 |
sample: 2345 |
| 254 |
cmp_hash: |
| 255 |
description: New traffic disaggregation method. |
| 256 |
returned: changed |
| 257 |
type: str |
| 258 |
sample: source-address |
| 259 |
dag_tunnel: |
| 260 |
description: The new DAG tunnel setting. |
| 261 |
returned: changed |
| 262 |
type: str |
| 263 |
sample: outer |
| 264 |
source_check: |
| 265 |
description: The new Source Check setting. |
| 266 |
returned: changed |
| 267 |
type: bool |
| 268 |
sample: true |
| 269 |
fail_safe: |
| 270 |
description: The new Fail Safe setting. |
| 271 |
returned: changed |
| 272 |
type: bool |
| 273 |
sample: false |
| 274 |
fail_safe_timeout: |
| 275 |
description: The new Fail Safe Timeout setting. |
| 276 |
returned: changed |
| 277 |
type: int |
| 278 |
sample: 90 |
| 279 |
fail_safe_action: |
| 280 |
description: The new Fail Safe Action setting. |
| 281 |
returned: changed |
| 282 |
type: str |
| 283 |
sample: reboot |
| 284 |
sflow_poll_interval: |
| 285 |
description: The new sFlow Polling Interval setting. |
| 286 |
returned: changed |
| 287 |
type: int |
| 288 |
sample: 10 |
| 289 |
sflow_sampling_rate: |
| 290 |
description: The new sFlow Sampling Rate setting. |
| 291 |
returned: changed |
| 292 |
type: int |
| 293 |
sample: 20 |
| 294 |
hw_syn_cookie: |
| 295 |
description: Enables hardware syncookie mode on a VLAN. |
| 296 |
returned: changed |
| 297 |
type: bool |
| 298 |
sample: false |
| 299 |
''' |
| 300 |
from datetime import datetime |
| 301 |
|
| 302 |
from ansible.module_utils.basic import ( |
| 303 |
AnsibleModule, env_fallback |
| 304 |
) |
| 305 |
from ..module_utils.bigip import F5RestClient |
| 306 |
from ..module_utils.common import ( |
| 307 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, flatten_boolean |
| 308 |
) |
| 309 |
from ..module_utils.compare import compare_complex_list |
| 310 |
from ..module_utils.icontrol import tmos_version |
| 311 |
from ..module_utils.teem import send_teem |
| 312 |
|
| 313 |
|
| 314 |
class Parameters(AnsibleF5Parameters): |
| 315 |
api_map = { |
| 316 |
'cmpHash': 'cmp_hash', |
| 317 |
'dagTunnel': 'dag_tunnel', |
| 318 |
'dagRoundRobin': 'dag_round_robin', |
| 319 |
'interfacesReference': 'interfaces', |
| 320 |
'sourceChecking': 'source_check', |
| 321 |
'failsafe': 'fail_safe', |
| 322 |
'failsafeAction': 'fail_safe_action', |
| 323 |
'failsafeTimeout': 'fail_safe_timeout', |
| 324 |
'hardwareSyncookie': 'hw_syn_cookie', |
| 325 |
} |
| 326 |
|
| 327 |
api_attributes = [ |
| 328 |
'description', |
| 329 |
'interfaces', |
| 330 |
'tag', |
| 331 |
'mtu', |
| 332 |
'cmpHash', |
| 333 |
'dagTunnel', |
| 334 |
'dagRoundRobin', |
| 335 |
'sourceChecking', |
| 336 |
'failsafe', |
| 337 |
'failsafeAction', |
| 338 |
'failsafeTimeout', |
| 339 |
'sflow', |
| 340 |
'hardwareSyncookie', |
| 341 |
] |
| 342 |
|
| 343 |
updatables = [ |
| 344 |
'interfaces', |
| 345 |
'tagged_interfaces', |
| 346 |
'untagged_interfaces', |
| 347 |
'tag', |
| 348 |
'description', |
| 349 |
'mtu', |
| 350 |
'cmp_hash', |
| 351 |
'dag_tunnel', |
| 352 |
'dag_round_robin', |
| 353 |
'source_check', |
| 354 |
'fail_safe', |
| 355 |
'fail_safe_action', |
| 356 |
'fail_safe_timeout', |
| 357 |
'sflow_poll_interval', |
| 358 |
'sflow_sampling_rate', |
| 359 |
'sflow', |
| 360 |
'hw_syn_cookie', |
| 361 |
] |
| 362 |
|
| 363 |
returnables = [ |
| 364 |
'description', |
| 365 |
'partition', |
| 366 |
'tag', |
| 367 |
'interfaces', |
| 368 |
'tagged_interfaces', |
| 369 |
'untagged_interfaces', |
| 370 |
'mtu', |
| 371 |
'cmp_hash', |
| 372 |
'dag_tunnel', |
| 373 |
'dag_round_robin', |
| 374 |
'source_check', |
| 375 |
'fail_safe', |
| 376 |
'fail_safe_action', |
| 377 |
'fail_safe_timeout', |
| 378 |
'sflow_poll_interval', |
| 379 |
'sflow_sampling_rate', |
| 380 |
'sflow', |
| 381 |
'hw_syn_cookie', |
| 382 |
] |
| 383 |
|
| 384 |
@property |
| 385 |
def source_check(self): |
| 386 |
return flatten_boolean(self._values['source_check']) |
| 387 |
|
| 388 |
@property |
| 389 |
def fail_safe(self): |
| 390 |
return flatten_boolean(self._values['fail_safe']) |
| 391 |
|
| 392 |
|
| 393 |
class ApiParameters(Parameters): |
| 394 |
@property |
| 395 |
def interfaces(self): |
| 396 |
if self._values['interfaces'] is None: |
| 397 |
return None |
| 398 |
if 'items' not in self._values['interfaces']: |
| 399 |
return None |
| 400 |
result = [] |
| 401 |
for item in self._values['interfaces']['items']: |
| 402 |
name = item['name'] |
| 403 |
if 'tagged' in item: |
| 404 |
tagged = item['tagged'] |
| 405 |
result.append(dict(name=name, tagged=tagged)) |
| 406 |
if 'untagged' in item: |
| 407 |
untagged = item['untagged'] |
| 408 |
result.append(dict(name=name, untagged=untagged)) |
| 409 |
return result |
| 410 |
|
| 411 |
@property |
| 412 |
def tagged_interfaces(self): |
| 413 |
if self.interfaces is None: |
| 414 |
return None |
| 415 |
result = [str(x['name']) for x in self.interfaces if 'tagged' in x and x['tagged'] is True] |
| 416 |
result = sorted(result) |
| 417 |
return result |
| 418 |
|
| 419 |
@property |
| 420 |
def untagged_interfaces(self): |
| 421 |
if self.interfaces is None: |
| 422 |
return None |
| 423 |
result = [str(x['name']) for x in self.interfaces if 'untagged' in x and x['untagged'] is True] |
| 424 |
result = sorted(result) |
| 425 |
return result |
| 426 |
|
| 427 |
@property |
| 428 |
def sflow_poll_interval(self): |
| 429 |
try: |
| 430 |
return self._values['sflow']['pollInterval'] |
| 431 |
except (KeyError, TypeError): |
| 432 |
return None |
| 433 |
|
| 434 |
@property |
| 435 |
def sflow_sampling_rate(self): |
| 436 |
try: |
| 437 |
return self._values['sflow']['samplingRate'] |
| 438 |
except (KeyError, TypeError): |
| 439 |
return None |
| 440 |
|
| 441 |
|
| 442 |
class ModuleParameters(Parameters): |
| 443 |
@property |
| 444 |
def interfaces(self): |
| 445 |
if self._values['interfaces'] is None: |
| 446 |
return None |
| 447 |
elif len(self._values['interfaces']) == 1 and self._values['interfaces'][0] in ['', 'none']: |
| 448 |
return '' |
| 449 |
result = [] |
| 450 |
for item in self._values['interfaces']: |
| 451 |
if 'interface' not in item: |
| 452 |
raise F5ModuleError( |
| 453 |
"An 'interface' key must be provided when specifying a list of interfaces." |
| 454 |
) |
| 455 |
if 'tagging' not in item: |
| 456 |
raise F5ModuleError( |
| 457 |
"A 'tagging' key must be provided when specifying a list of interfaces." |
| 458 |
) |
| 459 |
name = str(item['interface']) |
| 460 |
tagging = item['tagging'] |
| 461 |
|
| 462 |
if tagging == 'tagged': |
| 463 |
result.append(dict(name=name, tagged=True)) |
| 464 |
else: |
| 465 |
result.append(dict(name=name, untagged=True)) |
| 466 |
return result |
| 467 |
|
| 468 |
@property |
| 469 |
def untagged_interfaces(self): |
| 470 |
if self._values['untagged_interfaces'] is None: |
| 471 |
return None |
| 472 |
if self._values['untagged_interfaces'] is None: |
| 473 |
return None |
| 474 |
if len(self._values['untagged_interfaces']) == 1 and self._values['untagged_interfaces'][0] == '': |
| 475 |
return '' |
| 476 |
result = sorted([str(x) for x in self._values['untagged_interfaces']]) |
| 477 |
return result |
| 478 |
|
| 479 |
@property |
| 480 |
def tagged_interfaces(self): |
| 481 |
if self._values['tagged_interfaces'] is None: |
| 482 |
return None |
| 483 |
if self._values['tagged_interfaces'] is None: |
| 484 |
return None |
| 485 |
if len(self._values['tagged_interfaces']) == 1 and self._values['tagged_interfaces'][0] == '': |
| 486 |
return '' |
| 487 |
result = sorted([str(x) for x in self._values['tagged_interfaces']]) |
| 488 |
return result |
| 489 |
|
| 490 |
@property |
| 491 |
def mtu(self): |
| 492 |
if self._values['mtu'] is None: |
| 493 |
return None |
| 494 |
if int(self._values['mtu']) < 576 or int(self._values['mtu']) > 9198: |
| 495 |
raise F5ModuleError( |
| 496 |
"The mtu value must be between 576 - 9198" |
| 497 |
) |
| 498 |
return int(self._values['mtu']) |
| 499 |
|
| 500 |
@property |
| 501 |
def cmp_hash(self): |
| 502 |
if self._values['cmp_hash'] is None: |
| 503 |
return None |
| 504 |
if self._values['cmp_hash'] in ['source-address', 'src', 'src-ip', 'source']: |
| 505 |
return 'src-ip' |
| 506 |
if self._values['cmp_hash'] in ['destination-address', 'dest', 'dst-ip', 'destination', 'dst']: |
| 507 |
return 'dst-ip' |
| 508 |
else: |
| 509 |
return 'default' |
| 510 |
|
| 511 |
@property |
| 512 |
def dag_round_robin(self): |
| 513 |
if self._values['dag_round_robin'] is None: |
| 514 |
return None |
| 515 |
if self._values['dag_round_robin'] is True: |
| 516 |
return 'enabled' |
| 517 |
else: |
| 518 |
return 'disabled' |
| 519 |
|
| 520 |
@property |
| 521 |
def hw_syn_cookie(self): |
| 522 |
result = flatten_boolean(self._values['hw_syn_cookie']) |
| 523 |
if result == 'yes': |
| 524 |
return 'enabled' |
| 525 |
if result == 'no': |
| 526 |
return 'disabled' |
| 527 |
|
| 528 |
|
| 529 |
class Changes(Parameters): |
| 530 |
def to_return(self): |
| 531 |
result = {} |
| 532 |
try: |
| 533 |
for returnable in self.returnables: |
| 534 |
result[returnable] = getattr(self, returnable) |
| 535 |
result = self._filter_params(result) |
| 536 |
except Exception: |
| 537 |
raise |
| 538 |
return result |
| 539 |
|
| 540 |
|
| 541 |
class UsableChanges(Changes): |
| 542 |
@property |
| 543 |
def source_check(self): |
| 544 |
if self._values['source_check'] is None: |
| 545 |
return None |
| 546 |
if self._values['source_check'] == 'yes': |
| 547 |
return 'enabled' |
| 548 |
return 'disabled' |
| 549 |
|
| 550 |
@property |
| 551 |
def fail_safe(self): |
| 552 |
if self._values['fail_safe'] is None: |
| 553 |
return None |
| 554 |
if self._values['fail_safe'] == 'yes': |
| 555 |
return 'enabled' |
| 556 |
return 'disabled' |
| 557 |
|
| 558 |
|
| 559 |
class ReportableChanges(Changes): |
| 560 |
@property |
| 561 |
def tagged_interfaces(self): |
| 562 |
if self.interfaces is None: |
| 563 |
return None |
| 564 |
result = [str(x['name']) for x in self.interfaces if 'tagged' in x and x['tagged'] is True] |
| 565 |
result = sorted(result) |
| 566 |
return result |
| 567 |
|
| 568 |
@property |
| 569 |
def untagged_interfaces(self): |
| 570 |
if self.interfaces is None: |
| 571 |
return None |
| 572 |
result = [str(x['name']) for x in self.interfaces if 'untagged' in x and x['untagged'] is True] |
| 573 |
result = sorted(result) |
| 574 |
return result |
| 575 |
|
| 576 |
@property |
| 577 |
def source_check(self): |
| 578 |
return flatten_boolean(self._values['source_check']) |
| 579 |
|
| 580 |
@property |
| 581 |
def fail_safe(self): |
| 582 |
return flatten_boolean(self._values['fail_safe']) |
| 583 |
|
| 584 |
@property |
| 585 |
def sflow(self): |
| 586 |
return None |
| 587 |
|
| 588 |
@property |
| 589 |
def sflow_poll_interval(self): |
| 590 |
try: |
| 591 |
return self._values['sflow']['pollInterval'] |
| 592 |
except (KeyError, TypeError): |
| 593 |
return None |
| 594 |
|
| 595 |
@property |
| 596 |
def sflow_sampling_rate(self): |
| 597 |
try: |
| 598 |
return self._values['sflow']['samplingRate'] |
| 599 |
except (KeyError, TypeError): |
| 600 |
return None |
| 601 |
|
| 602 |
@property |
| 603 |
def hw_syn_cookie(self): |
| 604 |
return flatten_boolean(self._values['hw_syn_cookie']) |
| 605 |
|
| 606 |
|
| 607 |
class Difference(object): |
| 608 |
def __init__(self, want, have=None): |
| 609 |
self.want = want |
| 610 |
self.have = have |
| 611 |
|
| 612 |
def compare(self, param): |
| 613 |
try: |
| 614 |
result = getattr(self, param) |
| 615 |
return result |
| 616 |
except AttributeError: |
| 617 |
return self.__default(param) |
| 618 |
|
| 619 |
def __default(self, param): |
| 620 |
attr1 = getattr(self.want, param) |
| 621 |
try: |
| 622 |
attr2 = getattr(self.have, param) |
| 623 |
if attr1 != attr2: |
| 624 |
return attr1 |
| 625 |
except AttributeError: |
| 626 |
return attr1 |
| 627 |
|
| 628 |
@property |
| 629 |
def interfaces(self): |
| 630 |
if self.want.interfaces is None: |
| 631 |
return None |
| 632 |
if self.have.interfaces is None and self.want.interfaces in ['', 'none']: |
| 633 |
return None |
| 634 |
if self.have.interfaces is not None and self.want.interfaces in ['', 'none']: |
| 635 |
return [] |
| 636 |
if self.have.interfaces is None: |
| 637 |
return dict( |
| 638 |
interfaces=self.want.interfaces |
| 639 |
) |
| 640 |
return compare_complex_list(self.want.interfaces, self.have.interfaces) |
| 641 |
|
| 642 |
@property |
| 643 |
def untagged_interfaces(self): |
| 644 |
result = self.cmp_interfaces(self.want.untagged_interfaces, self.have.untagged_interfaces, False) |
| 645 |
return result |
| 646 |
|
| 647 |
@property |
| 648 |
def tagged_interfaces(self): |
| 649 |
result = self.cmp_interfaces(self.want.tagged_interfaces, self.have.tagged_interfaces, True) |
| 650 |
return result |
| 651 |
|
| 652 |
def cmp_interfaces(self, want, have, tagged): |
| 653 |
result = [] |
| 654 |
if tagged: |
| 655 |
tag_key = 'tagged' |
| 656 |
else: |
| 657 |
tag_key = 'untagged' |
| 658 |
if want is None: |
| 659 |
return None |
| 660 |
elif want == '' and have is None: |
| 661 |
return None |
| 662 |
elif want == '' and len(have) > 0: |
| 663 |
pass |
| 664 |
elif not have: |
| 665 |
result = dict( |
| 666 |
interfaces=[{'name': x, tag_key: True} for x in want] |
| 667 |
) |
| 668 |
elif set(want) != set(have): |
| 669 |
result = dict( |
| 670 |
interfaces=[{'name': x, tag_key: True} for x in want] |
| 671 |
) |
| 672 |
else: |
| 673 |
return None |
| 674 |
return result |
| 675 |
|
| 676 |
@property |
| 677 |
def sflow(self): |
| 678 |
result = {} |
| 679 |
s = self.sflow_poll_interval |
| 680 |
if s: |
| 681 |
result.update(s) |
| 682 |
s = self.sflow_sampling_rate |
| 683 |
if s: |
| 684 |
result.update(s) |
| 685 |
if result: |
| 686 |
return dict( |
| 687 |
sflow=result |
| 688 |
) |
| 689 |
|
| 690 |
@property |
| 691 |
def sflow_poll_interval(self): |
| 692 |
if self.want.sflow_poll_interval is None: |
| 693 |
return None |
| 694 |
if self.want.sflow_poll_interval != self.have.sflow_poll_interval: |
| 695 |
return dict( |
| 696 |
pollInterval=self.want.sflow_poll_interval |
| 697 |
) |
| 698 |
|
| 699 |
@property |
| 700 |
def sflow_sampling_rate(self): |
| 701 |
if self.want.sflow_sampling_rate is None: |
| 702 |
return None |
| 703 |
if self.want.sflow_sampling_rate != self.have.sflow_sampling_rate: |
| 704 |
return dict( |
| 705 |
samplingRate=self.want.sflow_sampling_rate |
| 706 |
) |
| 707 |
|
| 708 |
|
| 709 |
class ModuleManager(object): |
| 710 |
def __init__(self, *args, **kwargs): |
| 711 |
self.module = kwargs.get('module', None) |
| 712 |
self.client = F5RestClient(**self.module.params) |
| 713 |
self.want = ModuleParameters(params=self.module.params) |
| 714 |
self.have = ApiParameters() |
| 715 |
self.changes = UsableChanges() |
| 716 |
|
| 717 |
def _update_changed_options(self): |
| 718 |
diff = Difference(self.want, self.have) |
| 719 |
updatables = Parameters.updatables |
| 720 |
changed = dict() |
| 721 |
for k in updatables: |
| 722 |
change = diff.compare(k) |
| 723 |
if change is None: |
| 724 |
continue |
| 725 |
else: |
| 726 |
if isinstance(change, dict): |
| 727 |
changed.update(change) |
| 728 |
else: |
| 729 |
changed[k] = change |
| 730 |
if changed: |
| 731 |
self.changes = UsableChanges(params=changed) |
| 732 |
return True |
| 733 |
return False |
| 734 |
|
| 735 |
def exec_module(self): |
| 736 |
start = datetime.now().isoformat() |
| 737 |
version = tmos_version(self.client) |
| 738 |
changed = False |
| 739 |
result = dict() |
| 740 |
state = self.want.state |
| 741 |
|
| 742 |
if state == "present": |
| 743 |
changed = self.present() |
| 744 |
elif state == "absent": |
| 745 |
changed = self.absent() |
| 746 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 747 |
changes = reportable.to_return() |
| 748 |
result.update(**changes) |
| 749 |
result.update(dict(changed=changed)) |
| 750 |
self._announce_deprecations(result) |
| 751 |
send_teem(start, self.client, self.module, version) |
| 752 |
return result |
| 753 |
|
| 754 |
def _announce_deprecations(self, result): |
| 755 |
warnings = result.pop('__warnings', []) |
| 756 |
for warning in warnings: |
| 757 |
self.module.deprecate( |
| 758 |
msg=warning['msg'], |
| 759 |
version=warning['version'] |
| 760 |
) |
| 761 |
|
| 762 |
def present(self): |
| 763 |
if self.exists(): |
| 764 |
return self.update() |
| 765 |
else: |
| 766 |
return self.create() |
| 767 |
|
| 768 |
def absent(self): |
| 769 |
if self.exists(): |
| 770 |
return self.remove() |
| 771 |
return False |
| 772 |
|
| 773 |
def should_update(self): |
| 774 |
result = self._update_changed_options() |
| 775 |
if result: |
| 776 |
return True |
| 777 |
return False |
| 778 |
|
| 779 |
def update(self): |
| 780 |
self.have = self.read_current_from_device() |
| 781 |
if not self.should_update(): |
| 782 |
return False |
| 783 |
if self.module.check_mode: |
| 784 |
return True |
| 785 |
self.update_on_device() |
| 786 |
return True |
| 787 |
|
| 788 |
def remove(self): |
| 789 |
if self.module.check_mode: |
| 790 |
return True |
| 791 |
self.remove_from_device() |
| 792 |
if self.exists(): |
| 793 |
raise F5ModuleError("Failed to delete the VLAN") |
| 794 |
return True |
| 795 |
|
| 796 |
def create(self): |
| 797 |
self.have = ApiParameters() |
| 798 |
if self.want.mtu is None: |
| 799 |
self.want.update({'mtu': 1500}) |
| 800 |
self._update_changed_options() |
| 801 |
if self.module.check_mode: |
| 802 |
return True |
| 803 |
self.create_on_device() |
| 804 |
return True |
| 805 |
|
| 806 |
def create_on_device(self): |
| 807 |
params = self.changes.api_params() |
| 808 |
params['name'] = self.want.name |
| 809 |
params['partition'] = self.want.partition |
| 810 |
uri = "https://{0}:{1}/mgmt/tm/net/vlan".format( |
| 811 |
self.client.provider['server'], |
| 812 |
self.client.provider['server_port'] |
| 813 |
) |
| 814 |
resp = self.client.api.post(uri, json=params) |
| 815 |
try: |
| 816 |
response = resp.json() |
| 817 |
except ValueError as ex: |
| 818 |
raise F5ModuleError(str(ex)) |
| 819 |
|
| 820 |
if 'code' in response and response['code'] in [400, 403]: |
| 821 |
if 'message' in response: |
| 822 |
raise F5ModuleError(response['message']) |
| 823 |
else: |
| 824 |
raise F5ModuleError(resp.content) |
| 825 |
return response['selfLink'] |
| 826 |
|
| 827 |
def update_on_device(self): |
| 828 |
params = self.changes.api_params() |
| 829 |
uri = "https://{0}:{1}/mgmt/tm/net/vlan/{2}".format( |
| 830 |
self.client.provider['server'], |
| 831 |
self.client.provider['server_port'], |
| 832 |
transform_name(self.want.partition, self.want.name) |
| 833 |
) |
| 834 |
resp = self.client.api.patch(uri, json=params) |
| 835 |
try: |
| 836 |
response = resp.json() |
| 837 |
except ValueError as ex: |
| 838 |
raise F5ModuleError(str(ex)) |
| 839 |
|
| 840 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 841 |
return True |
| 842 |
raise F5ModuleError(resp.content) |
| 843 |
|
| 844 |
def exists(self): |
| 845 |
uri = "https://{0}:{1}/mgmt/tm/net/vlan/{2}".format( |
| 846 |
self.client.provider['server'], |
| 847 |
self.client.provider['server_port'], |
| 848 |
transform_name(self.want.partition, self.want.name) |
| 849 |
) |
| 850 |
resp = self.client.api.get(uri) |
| 851 |
try: |
| 852 |
response = resp.json() |
| 853 |
except ValueError as ex: |
| 854 |
raise F5ModuleError(str(ex)) |
| 855 |
|
| 856 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 857 |
return False |
| 858 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 859 |
return True |
| 860 |
|
| 861 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 862 |
|
| 863 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 864 |
if 'message' in response: |
| 865 |
raise F5ModuleError(response['message']) |
| 866 |
else: |
| 867 |
raise F5ModuleError(resp.content) |
| 868 |
|
| 869 |
def remove_from_device(self): |
| 870 |
uri = "https://{0}:{1}/mgmt/tm/net/vlan/{2}".format( |
| 871 |
self.client.provider['server'], |
| 872 |
self.client.provider['server_port'], |
| 873 |
transform_name(self.want.partition, self.want.name) |
| 874 |
) |
| 875 |
resp = self.client.api.delete(uri) |
| 876 |
if resp.status == 200: |
| 877 |
return True |
| 878 |
|
| 879 |
def read_current_from_device(self): |
| 880 |
uri = "https://{0}:{1}/mgmt/tm/net/vlan/{2}".format( |
| 881 |
self.client.provider['server'], |
| 882 |
self.client.provider['server_port'], |
| 883 |
transform_name(self.want.partition, self.want.name) |
| 884 |
) |
| 885 |
query = '?expandSubcollections=true' |
| 886 |
resp = self.client.api.get(uri + query) |
| 887 |
try: |
| 888 |
response = resp.json() |
| 889 |
except ValueError as ex: |
| 890 |
raise F5ModuleError(str(ex)) |
| 891 |
|
| 892 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 893 |
return ApiParameters(params=response) |
| 894 |
raise F5ModuleError(resp.content) |
| 895 |
|
| 896 |
|
| 897 |
class ArgumentSpec(object): |
| 898 |
def __init__(self): |
| 899 |
self.supports_check_mode = True |
| 900 |
argument_spec = dict( |
| 901 |
name=dict( |
| 902 |
required=True, |
| 903 |
), |
| 904 |
tagged_interfaces=dict( |
| 905 |
type='list', |
| 906 |
elements='str', |
| 907 |
aliases=['tagged_interface'] |
| 908 |
), |
| 909 |
untagged_interfaces=dict( |
| 910 |
type='list', |
| 911 |
elements='str', |
| 912 |
aliases=['untagged_interface'] |
| 913 |
), |
| 914 |
interfaces=dict( |
| 915 |
type='list', |
| 916 |
elements='dict', |
| 917 |
options=dict( |
| 918 |
interface=dict(), |
| 919 |
tagging=dict( |
| 920 |
choices=['tagged', 'untagged'] |
| 921 |
) |
| 922 |
) |
| 923 |
), |
| 924 |
description=dict(), |
| 925 |
tag=dict( |
| 926 |
type='int' |
| 927 |
), |
| 928 |
mtu=dict(type='int'), |
| 929 |
cmp_hash=dict( |
| 930 |
choices=[ |
| 931 |
'default', |
| 932 |
'destination-address', 'dest', 'dst-ip', 'destination', 'dst', |
| 933 |
'source-address', 'src', 'src-ip', 'source' |
| 934 |
] |
| 935 |
), |
| 936 |
dag_tunnel=dict( |
| 937 |
choices=['inner', 'outer'] |
| 938 |
), |
| 939 |
dag_round_robin=dict(type='bool'), |
| 940 |
source_check=dict(type='bool'), |
| 941 |
fail_safe=dict(type='bool'), |
| 942 |
fail_safe_timeout=dict(type='int'), |
| 943 |
fail_safe_action=dict( |
| 944 |
choices=['reboot', 'restart-all', 'failover'] |
| 945 |
), |
| 946 |
sflow_poll_interval=dict(type='int'), |
| 947 |
sflow_sampling_rate=dict(type='int'), |
| 948 |
hw_syn_cookie=dict(type='bool'), |
| 949 |
state=dict( |
| 950 |
default='present', |
| 951 |
choices=['present', 'absent'] |
| 952 |
), |
| 953 |
partition=dict( |
| 954 |
default='Common', |
| 955 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 956 |
), |
| 957 |
) |
| 958 |
self.argument_spec = {} |
| 959 |
self.argument_spec.update(f5_argument_spec) |
| 960 |
self.argument_spec.update(argument_spec) |
| 961 |
self.mutually_exclusive = [ |
| 962 |
['tagged_interfaces', 'untagged_interfaces', 'interfaces'], |
| 963 |
] |
| 964 |
|
| 965 |
|
| 966 |
def main(): |
| 967 |
spec = ArgumentSpec() |
| 968 |
|
| 969 |
module = AnsibleModule( |
| 970 |
argument_spec=spec.argument_spec, |
| 971 |
supports_check_mode=spec.supports_check_mode, |
| 972 |
mutually_exclusive=spec.mutually_exclusive |
| 973 |
) |
| 974 |
try: |
| 975 |
mm = ModuleManager(module=module) |
| 976 |
results = mm.exec_module() |
| 977 |
module.exit_json(**results) |
| 978 |
except F5ModuleError as ex: |
| 979 |
module.fail_json(msg=str(ex)) |
| 980 |
|
| 981 |
|
| 982 |
if __name__ == '__main__': |
| 983 |
main() |