| 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_ipsec_policy |
| 13 |
short_description: Manage IPSec policies on a BIG-IP |
| 14 |
description: |
| 15 |
- Manage IPSec policies on a BIG-IP device. |
| 16 |
version_added: "1.0.0" |
| 17 |
options: |
| 18 |
name: |
| 19 |
description: |
| 20 |
- Specifies the name of the IPSec policy. |
| 21 |
type: str |
| 22 |
required: True |
| 23 |
description: |
| 24 |
description: |
| 25 |
- Description of the policy |
| 26 |
type: str |
| 27 |
protocol: |
| 28 |
description: |
| 29 |
- Specifies the IPsec protocol. |
| 30 |
- Options include ESP (Encapsulating Security Protocol) or AH (Authentication Header). |
| 31 |
type: str |
| 32 |
choices: |
| 33 |
- esp |
| 34 |
- ah |
| 35 |
mode: |
| 36 |
description: |
| 37 |
- Specifies the processing mode. |
| 38 |
- When C(transport), specifies a mode that encapsulates only the payload (adding |
| 39 |
an ESP header, trailer, and authentication tag). |
| 40 |
- When C(tunnel), specifies a mode that includes encapsulation of the header as |
| 41 |
well as the payload (adding a new IP header, in addition to adding an ESP header, |
| 42 |
trailer, and authentication tag). If you select this option, you must also |
| 43 |
provide IP addresses for the local and remote endpoints of the IPsec tunnel. |
| 44 |
- When C(isession), specifies the use of iSession over an IPsec tunnel. To use |
| 45 |
this option, you must also configure the iSession endpoints with IPsec in the |
| 46 |
Acceleration section of the user interface. |
| 47 |
- When C(interface), specifies the IPsec policy can be used in the tunnel |
| 48 |
profile for network interfaces. |
| 49 |
type: str |
| 50 |
choices: |
| 51 |
- transport |
| 52 |
- interface |
| 53 |
- isession |
| 54 |
- tunnel |
| 55 |
tunnel_local_address: |
| 56 |
description: |
| 57 |
- Specifies the local endpoint IP address of the IPsec tunnel. |
| 58 |
- This parameter is only valid when C(mode) is C(tunnel). |
| 59 |
type: str |
| 60 |
tunnel_remote_address: |
| 61 |
description: |
| 62 |
- Specifies the remote endpoint IP address of the IPsec tunnel. |
| 63 |
- This parameter is only valid when C(mode) is C(tunnel). |
| 64 |
type: str |
| 65 |
encrypt_algorithm: |
| 66 |
description: |
| 67 |
- Specifies the algorithm to use for IKE encryption. |
| 68 |
type: str |
| 69 |
choices: |
| 70 |
- none |
| 71 |
- 3des |
| 72 |
- aes128 |
| 73 |
- aes192 |
| 74 |
- aes256 |
| 75 |
- aes-gmac256 |
| 76 |
- aes-gmac192 |
| 77 |
- aes-gmac128 |
| 78 |
- aes-gcm256 |
| 79 |
- aes-gcm192 |
| 80 |
- aes-gcm256 |
| 81 |
- aes-gcm128 |
| 82 |
route_domain: |
| 83 |
description: |
| 84 |
- Specifies the route domain, when C(interface) is selected for the C(mode) setting. |
| 85 |
type: int |
| 86 |
auth_algorithm: |
| 87 |
description: |
| 88 |
- Specifies the algorithm to use for IKE authentication. |
| 89 |
type: str |
| 90 |
choices: |
| 91 |
- sha1 |
| 92 |
- sha256 |
| 93 |
- sha384 |
| 94 |
- sha512 |
| 95 |
- aes-gcm128 |
| 96 |
- aes-gcm192 |
| 97 |
- aes-gcm256 |
| 98 |
- aes-gmac128 |
| 99 |
- aes-gmac192 |
| 100 |
- aes-gmac256 |
| 101 |
ipcomp: |
| 102 |
description: |
| 103 |
- Specifies whether to use IPComp encapsulation. |
| 104 |
- When C(none), specifies IPComp is disabled. |
| 105 |
- When C(deflate), specifies IPComp is enabled and uses the Deflate |
| 106 |
compression algorithm. |
| 107 |
type: str |
| 108 |
choices: |
| 109 |
- none |
| 110 |
- "null" |
| 111 |
- deflate |
| 112 |
lifetime: |
| 113 |
description: |
| 114 |
- Specifies the length of time before the IKE security association expires, |
| 115 |
in minutes. |
| 116 |
type: int |
| 117 |
kb_lifetime: |
| 118 |
description: |
| 119 |
- Specifies the length of time before the IKE security association, in kilobytes. |
| 120 |
expires. |
| 121 |
type: int |
| 122 |
perfect_forward_secrecy: |
| 123 |
description: |
| 124 |
- Specifies the Diffie-Hellman group to use for IKE Phase 2 negotiation. |
| 125 |
type: str |
| 126 |
choices: |
| 127 |
- none |
| 128 |
- modp768 |
| 129 |
- modp1024 |
| 130 |
- modp1536 |
| 131 |
- modp2048 |
| 132 |
- modp3072 |
| 133 |
- modp4096 |
| 134 |
- modp6144 |
| 135 |
- modp8192 |
| 136 |
ipv4_interface: |
| 137 |
description: |
| 138 |
- When C(mode) is C(interface), indicates if the IPv4 C(any) address should be used. |
| 139 |
By default C(BIG-IP) assumes C(any6) address for tunnel addresses when C(mode) is C(interface). |
| 140 |
- This option takes effect only when C(mode) is set to C(interface). |
| 141 |
type: bool |
| 142 |
partition: |
| 143 |
description: |
| 144 |
- Device partition to manage resources on. |
| 145 |
type: str |
| 146 |
default: Common |
| 147 |
state: |
| 148 |
description: |
| 149 |
- When C(present), ensures the resource exists. |
| 150 |
- When C(absent), ensures the resource is removed. |
| 151 |
type: str |
| 152 |
choices: |
| 153 |
- present |
| 154 |
- absent |
| 155 |
default: present |
| 156 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 157 |
author: |
| 158 |
- Tim Rupp (@caphrim007) |
| 159 |
- Wojciech Wypior (@wojtek0806) |
| 160 |
''' |
| 161 |
|
| 162 |
EXAMPLES = r''' |
| 163 |
- name: Create a IPSec policy |
| 164 |
bigip_ipsec_policy: |
| 165 |
name: policy1 |
| 166 |
mode: tunnel |
| 167 |
tunnel_local_address: 1.1.1.1 |
| 168 |
tunnel_remote_address: 2.2.2. |
| 169 |
auth_algorithm: sha1 |
| 170 |
encrypt_algorithm: 3des |
| 171 |
protocol: esp |
| 172 |
perfect_forward_secrecy: modp1024 |
| 173 |
provider: |
| 174 |
password: secret |
| 175 |
server: lb.mydomain.com |
| 176 |
user: admin |
| 177 |
delegate_to: localhost |
| 178 |
''' |
| 179 |
|
| 180 |
RETURN = r''' |
| 181 |
auth_algorithm: |
| 182 |
description: The new IKE Phase 2 Authentication Algorithm value. |
| 183 |
returned: changed |
| 184 |
type: str |
| 185 |
sample: sha512 |
| 186 |
encrypt_algorithm: |
| 187 |
description: The new IKE Phase 2 Encryption Algorithm value. |
| 188 |
returned: changed |
| 189 |
type: str |
| 190 |
sample: aes256 |
| 191 |
lifetime: |
| 192 |
description: The new IKE Phase 2 Lifetime value. |
| 193 |
returned: changed |
| 194 |
type: int |
| 195 |
sample: 1440 |
| 196 |
kb_lifetime: |
| 197 |
description: The new IKE Phase 2 KB Lifetime value. |
| 198 |
returned: changed |
| 199 |
type: int |
| 200 |
sample: 0 |
| 201 |
perfect_forward_secrecy: |
| 202 |
description: The new IKE Phase 2 Perfect Forward Secrecy value. |
| 203 |
returned: changed |
| 204 |
type: str |
| 205 |
sample: modp2048 |
| 206 |
tunnel_local_address: |
| 207 |
description: The new Tunnel Local Address value. |
| 208 |
returned: changed |
| 209 |
type: str |
| 210 |
sample: 1.2.2.1 |
| 211 |
tunnel_remote_address: |
| 212 |
description: The new Tunnel Remote Address value. |
| 213 |
returned: changed |
| 214 |
type: str |
| 215 |
sample: 2.1.1.2 |
| 216 |
mode: |
| 217 |
description: The new Mode value. |
| 218 |
returned: changed |
| 219 |
type: str |
| 220 |
sample: tunnel |
| 221 |
protocol: |
| 222 |
description: The new IPsec Protocol value. |
| 223 |
returned: changed |
| 224 |
type: str |
| 225 |
sample: ah |
| 226 |
ipcomp: |
| 227 |
description: The new IKE Phase 2 IPComp value. |
| 228 |
returned: changed |
| 229 |
type: str |
| 230 |
sample: deflate |
| 231 |
description: |
| 232 |
description: The new description value. |
| 233 |
returned: changed |
| 234 |
type: str |
| 235 |
sample: My policy |
| 236 |
route_domain: |
| 237 |
description: The new Route Domain value when in Tunnel mode. |
| 238 |
returned: changed |
| 239 |
type: int |
| 240 |
sample: 2 |
| 241 |
''' |
| 242 |
from datetime import datetime |
| 243 |
|
| 244 |
from ansible.module_utils.basic import ( |
| 245 |
AnsibleModule, env_fallback |
| 246 |
) |
| 247 |
|
| 248 |
from ..module_utils.bigip import F5RestClient |
| 249 |
from ..module_utils.common import ( |
| 250 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, flatten_boolean |
| 251 |
) |
| 252 |
from ..module_utils.compare import cmp_str_with_none |
| 253 |
from ..module_utils.icontrol import tmos_version |
| 254 |
from ..module_utils.teem import send_teem |
| 255 |
|
| 256 |
|
| 257 |
class Parameters(AnsibleF5Parameters): |
| 258 |
api_map = { |
| 259 |
'ikePhase2AuthAlgorithm': 'auth_algorithm', |
| 260 |
'ikePhase2EncryptAlgorithm': 'encrypt_algorithm', |
| 261 |
'ikePhase2Lifetime': 'lifetime', |
| 262 |
'ikePhase2LifetimeKilobytes': 'kb_lifetime', |
| 263 |
'ikePhase2PerfectForwardSecrecy': 'perfect_forward_secrecy', |
| 264 |
'tunnelLocalAddress': 'tunnel_local_address', |
| 265 |
'tunnelRemoteAddress': 'tunnel_remote_address', |
| 266 |
} |
| 267 |
|
| 268 |
api_attributes = [ |
| 269 |
'ikePhase2AuthAlgorithm', |
| 270 |
'ikePhase2EncryptAlgorithm', |
| 271 |
'ikePhase2Lifetime', |
| 272 |
'ikePhase2LifetimeKilobytes', |
| 273 |
'ikePhase2PerfectForwardSecrecy', |
| 274 |
'tunnelLocalAddress', |
| 275 |
'tunnelRemoteAddress', |
| 276 |
'mode', |
| 277 |
'protocol', |
| 278 |
'ipcomp', |
| 279 |
'description', |
| 280 |
] |
| 281 |
|
| 282 |
returnables = [ |
| 283 |
'auth_algorithm', |
| 284 |
'encrypt_algorithm', |
| 285 |
'lifetime', |
| 286 |
'kb_lifetime', |
| 287 |
'perfect_forward_secrecy', |
| 288 |
'tunnel_local_address', |
| 289 |
'tunnel_remote_address', |
| 290 |
'mode', |
| 291 |
'protocol', |
| 292 |
'ipcomp', |
| 293 |
'description', |
| 294 |
'route_domain', |
| 295 |
] |
| 296 |
|
| 297 |
updatables = [ |
| 298 |
'auth_algorithm', |
| 299 |
'encrypt_algorithm', |
| 300 |
'lifetime', |
| 301 |
'kb_lifetime', |
| 302 |
'perfect_forward_secrecy', |
| 303 |
'tunnel_local_address', |
| 304 |
'tunnel_remote_address', |
| 305 |
'mode', |
| 306 |
'protocol', |
| 307 |
'ipcomp', |
| 308 |
'description', |
| 309 |
'route_domain', |
| 310 |
] |
| 311 |
|
| 312 |
@property |
| 313 |
def tunnel_local_address(self): |
| 314 |
if self._values['tunnel_local_address'] is None: |
| 315 |
return None |
| 316 |
result = self._values['tunnel_local_address'].split('%')[0] |
| 317 |
return result |
| 318 |
|
| 319 |
@property |
| 320 |
def tunnel_remote_address(self): |
| 321 |
if self._values['tunnel_remote_address'] is None: |
| 322 |
return None |
| 323 |
result = self._values['tunnel_remote_address'].split('%')[0] |
| 324 |
return result |
| 325 |
|
| 326 |
|
| 327 |
class ApiParameters(Parameters): |
| 328 |
@property |
| 329 |
def description(self): |
| 330 |
if self._values['description'] in [None, 'none']: |
| 331 |
return None |
| 332 |
return self._values['description'] |
| 333 |
|
| 334 |
@property |
| 335 |
def encrypt_algorithm(self): |
| 336 |
if self._values['encrypt_algorithm'] is None: |
| 337 |
return None |
| 338 |
elif self._values['encrypt_algorithm'] == 'null': |
| 339 |
return 'none' |
| 340 |
return self._values['encrypt_algorithm'] |
| 341 |
|
| 342 |
@property |
| 343 |
def route_domain(self): |
| 344 |
if self._values['tunnel_local_address'] is None and self._values['tunnel_remote_address'] is None: |
| 345 |
return None |
| 346 |
elif self._values['tunnel_local_address'] is None and self._values['tunnel_remote_address'] is not None: |
| 347 |
if self._values['tunnel_remote_address'] == 'any6': |
| 348 |
result = 'any6' |
| 349 |
elif self._values['tunnel_remote_address'] == 'any': |
| 350 |
result = 'any' |
| 351 |
else: |
| 352 |
result = int(self._values['tunnel_remote_address'].split('%')[1]) |
| 353 |
elif self._values['tunnel_remote_address'] is None and self._values['tunnel_local_address'] is not None: |
| 354 |
if self._values['tunnel_local_address'] == 'any6': |
| 355 |
result = 'any6' |
| 356 |
elif self._values['tunnel_local_address'] == 'any': |
| 357 |
result = 'any' |
| 358 |
else: |
| 359 |
result = int(self._values['tunnel_local_address'].split('%')[1]) |
| 360 |
else: |
| 361 |
try: |
| 362 |
result = int(self._values['tunnel_local_address'].split('%')[1]) |
| 363 |
except Exception: |
| 364 |
if self._values['tunnel_local_address'] in ['any6', 'any']: |
| 365 |
return 0 |
| 366 |
return None |
| 367 |
try: |
| 368 |
if result in ['any6', 'any']: |
| 369 |
return 0 |
| 370 |
return int(self._values['tunnel_local_address'].split('%')[1]) |
| 371 |
except Exception: |
| 372 |
return None |
| 373 |
|
| 374 |
|
| 375 |
class ModuleParameters(Parameters): |
| 376 |
@property |
| 377 |
def ipv4_interface(self): |
| 378 |
result = flatten_boolean(self._values['ipv4_interface']) |
| 379 |
if result == 'yes': |
| 380 |
return True |
| 381 |
return False |
| 382 |
|
| 383 |
@property |
| 384 |
def description(self): |
| 385 |
if self._values['description'] is None: |
| 386 |
return None |
| 387 |
elif self._values['description'] in ['none', '']: |
| 388 |
return '' |
| 389 |
return self._values['description'] |
| 390 |
|
| 391 |
|
| 392 |
class Changes(Parameters): |
| 393 |
def to_return(self): |
| 394 |
result = {} |
| 395 |
try: |
| 396 |
for returnable in self.returnables: |
| 397 |
result[returnable] = getattr(self, returnable) |
| 398 |
result = self._filter_params(result) |
| 399 |
except Exception: |
| 400 |
raise |
| 401 |
return result |
| 402 |
|
| 403 |
|
| 404 |
class UsableChanges(Changes): |
| 405 |
@property |
| 406 |
def encrypt_algorithm(self): |
| 407 |
if self._values['encrypt_algorithm'] is None: |
| 408 |
return None |
| 409 |
elif self._values['encrypt_algorithm'] == 'none': |
| 410 |
return 'null' |
| 411 |
return self._values['encrypt_algorithm'] |
| 412 |
|
| 413 |
@property |
| 414 |
def tunnel_local_address(self): |
| 415 |
if self._values['tunnel_local_address'] is None: |
| 416 |
return None |
| 417 |
if self._values['route_domain'] and len(self._values['tunnel_local_address'].split('%')) == 1: |
| 418 |
result = '{0}%{1}'.format(self._values['tunnel_local_address'], self._values['route_domain']) |
| 419 |
return result |
| 420 |
return self._values['tunnel_local_address'] |
| 421 |
|
| 422 |
@property |
| 423 |
def tunnel_remote_address(self): |
| 424 |
if self._values['tunnel_remote_address'] is None: |
| 425 |
return None |
| 426 |
if self._values['route_domain'] and len(self._values['tunnel_remote_address'].split('%')) == 1: |
| 427 |
result = '{0}%{1}'.format(self._values['tunnel_remote_address'], self._values['route_domain']) |
| 428 |
return result |
| 429 |
return self._values['tunnel_remote_address'] |
| 430 |
|
| 431 |
|
| 432 |
class ReportableChanges(Changes): |
| 433 |
@property |
| 434 |
def encrypt_algorithm(self): |
| 435 |
if self._values['encrypt_algorithm'] is None: |
| 436 |
return None |
| 437 |
elif self._values['encrypt_algorithm'] == 'null': |
| 438 |
return 'none' |
| 439 |
return self._values['encrypt_algorithm'] |
| 440 |
|
| 441 |
|
| 442 |
class Difference(object): |
| 443 |
def __init__(self, want, have=None): |
| 444 |
self.want = want |
| 445 |
self.have = have |
| 446 |
|
| 447 |
def compare(self, param): |
| 448 |
try: |
| 449 |
result = getattr(self, param) |
| 450 |
return result |
| 451 |
except AttributeError: |
| 452 |
return self.__default(param) |
| 453 |
|
| 454 |
def __default(self, param): |
| 455 |
attr1 = getattr(self.want, param) |
| 456 |
try: |
| 457 |
attr2 = getattr(self.have, param) |
| 458 |
if attr1 != attr2: |
| 459 |
return attr1 |
| 460 |
except AttributeError: |
| 461 |
return attr1 |
| 462 |
|
| 463 |
@property |
| 464 |
def description(self): |
| 465 |
return cmp_str_with_none(self.want.description, self.have.description) |
| 466 |
|
| 467 |
@property |
| 468 |
def route_domain(self): |
| 469 |
if self.want.route_domain is None: |
| 470 |
return None |
| 471 |
if self.have.route_domain != self.want.route_domain: |
| 472 |
if self.want.route_domain == 0 and self.want.ipv4_interface: |
| 473 |
return dict( |
| 474 |
tunnel_local_address='any', |
| 475 |
tunnel_remote_address='any', |
| 476 |
route_domain=self.want.route_domain, |
| 477 |
) |
| 478 |
elif self.want.route_domain == 0 and not self.want.ipv4_interface: |
| 479 |
return dict( |
| 480 |
tunnel_local_address='any6', |
| 481 |
tunnel_remote_address='any6', |
| 482 |
route_domain=self.want.route_domain, |
| 483 |
) |
| 484 |
else: |
| 485 |
return dict( |
| 486 |
tunnel_local_address='any%{0}'.format(self.want.route_domain), |
| 487 |
tunnel_remote_address='any%{0}'.format(self.want.route_domain), |
| 488 |
route_domain=self.want.route_domain, |
| 489 |
) |
| 490 |
|
| 491 |
|
| 492 |
class ModuleManager(object): |
| 493 |
def __init__(self, *args, **kwargs): |
| 494 |
self.module = kwargs.get('module', None) |
| 495 |
self.client = F5RestClient(**self.module.params) |
| 496 |
self.want = ModuleParameters(params=self.module.params) |
| 497 |
self.have = ApiParameters() |
| 498 |
self.changes = UsableChanges() |
| 499 |
|
| 500 |
def _set_changed_options(self): |
| 501 |
changed = {} |
| 502 |
for key in Parameters.returnables: |
| 503 |
if getattr(self.want, key) is not None: |
| 504 |
changed[key] = getattr(self.want, key) |
| 505 |
if changed: |
| 506 |
self.changes = UsableChanges(params=changed) |
| 507 |
|
| 508 |
def _update_changed_options(self): |
| 509 |
diff = Difference(self.want, self.have) |
| 510 |
updatables = Parameters.updatables |
| 511 |
changed = dict() |
| 512 |
for k in updatables: |
| 513 |
change = diff.compare(k) |
| 514 |
if change is None: |
| 515 |
continue |
| 516 |
else: |
| 517 |
if isinstance(change, dict): |
| 518 |
changed.update(change) |
| 519 |
else: |
| 520 |
changed[k] = change |
| 521 |
if changed: |
| 522 |
self.changes = UsableChanges(params=changed) |
| 523 |
return True |
| 524 |
return False |
| 525 |
|
| 526 |
def should_update(self): |
| 527 |
result = self._update_changed_options() |
| 528 |
if result: |
| 529 |
return True |
| 530 |
return False |
| 531 |
|
| 532 |
def exec_module(self): |
| 533 |
start = datetime.now().isoformat() |
| 534 |
version = tmos_version(self.client) |
| 535 |
changed = False |
| 536 |
result = dict() |
| 537 |
state = self.want.state |
| 538 |
|
| 539 |
if state == "present": |
| 540 |
changed = self.present() |
| 541 |
elif state == "absent": |
| 542 |
changed = self.absent() |
| 543 |
|
| 544 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 545 |
changes = reportable.to_return() |
| 546 |
result.update(**changes) |
| 547 |
result.update(dict(changed=changed)) |
| 548 |
self._announce_deprecations(result) |
| 549 |
send_teem(start, self.client, self.module, version) |
| 550 |
return result |
| 551 |
|
| 552 |
def _announce_deprecations(self, result): |
| 553 |
warnings = result.pop('__warnings', []) |
| 554 |
for warning in warnings: |
| 555 |
self.client.module.deprecate( |
| 556 |
msg=warning['msg'], |
| 557 |
version=warning['version'] |
| 558 |
) |
| 559 |
|
| 560 |
def present(self): |
| 561 |
if self.exists(): |
| 562 |
return self.update() |
| 563 |
else: |
| 564 |
return self.create() |
| 565 |
|
| 566 |
def exists(self): |
| 567 |
uri = "https://{0}:{1}/mgmt/tm/net/ipsec/ipsec-policy/{2}".format( |
| 568 |
self.client.provider['server'], |
| 569 |
self.client.provider['server_port'], |
| 570 |
transform_name(self.want.partition, self.want.name) |
| 571 |
) |
| 572 |
resp = self.client.api.get(uri) |
| 573 |
try: |
| 574 |
response = resp.json() |
| 575 |
except ValueError as ex: |
| 576 |
raise F5ModuleError(str(ex)) |
| 577 |
|
| 578 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 579 |
return False |
| 580 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 581 |
return True |
| 582 |
|
| 583 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 584 |
|
| 585 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 586 |
if 'message' in response: |
| 587 |
raise F5ModuleError(response['message']) |
| 588 |
else: |
| 589 |
raise F5ModuleError(resp.content) |
| 590 |
|
| 591 |
def update(self): |
| 592 |
self.have = self.read_current_from_device() |
| 593 |
if not self.should_update(): |
| 594 |
return False |
| 595 |
if self.module.check_mode: |
| 596 |
return True |
| 597 |
self.update_on_device() |
| 598 |
return True |
| 599 |
|
| 600 |
def remove(self): |
| 601 |
if self.module.check_mode: |
| 602 |
return True |
| 603 |
self.remove_from_device() |
| 604 |
if self.exists(): |
| 605 |
raise F5ModuleError("Failed to delete the resource.") |
| 606 |
return True |
| 607 |
|
| 608 |
def create(self): |
| 609 |
if self.want.mode == 'interface': |
| 610 |
if self.want.ipv4_interface: |
| 611 |
self._set_any_on_interface(ip='ipv4') |
| 612 |
else: |
| 613 |
self._set_any_on_interface() |
| 614 |
self._set_changed_options() |
| 615 |
if self.module.check_mode: |
| 616 |
return True |
| 617 |
self.create_on_device() |
| 618 |
return True |
| 619 |
|
| 620 |
def _set_any_on_interface(self, ip='ipv6'): |
| 621 |
if ip == 'ipv4': |
| 622 |
self.want.update({'tunnel_local_address': 'any'}) |
| 623 |
self.want.update({'tunnel_remote_address': 'any'}) |
| 624 |
else: |
| 625 |
self.want.update({'tunnel_local_address': 'any6'}) |
| 626 |
self.want.update({'tunnel_remote_address': 'any6'}) |
| 627 |
|
| 628 |
def create_on_device(self): |
| 629 |
params = self.changes.api_params() |
| 630 |
params['name'] = self.want.name |
| 631 |
params['partition'] = self.want.partition |
| 632 |
uri = "https://{0}:{1}/mgmt/tm/net/ipsec/ipsec-policy/".format( |
| 633 |
self.client.provider['server'], |
| 634 |
self.client.provider['server_port'] |
| 635 |
) |
| 636 |
resp = self.client.api.post(uri, json=params) |
| 637 |
try: |
| 638 |
response = resp.json() |
| 639 |
except ValueError as ex: |
| 640 |
raise F5ModuleError(str(ex)) |
| 641 |
|
| 642 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 643 |
return True |
| 644 |
raise F5ModuleError(resp.content) |
| 645 |
|
| 646 |
def update_on_device(self): |
| 647 |
params = self.changes.api_params() |
| 648 |
uri = "https://{0}:{1}/mgmt/tm/net/ipsec/ipsec-policy/{2}".format( |
| 649 |
self.client.provider['server'], |
| 650 |
self.client.provider['server_port'], |
| 651 |
transform_name(self.want.partition, self.want.name) |
| 652 |
) |
| 653 |
resp = self.client.api.patch(uri, json=params) |
| 654 |
try: |
| 655 |
response = resp.json() |
| 656 |
except ValueError as ex: |
| 657 |
raise F5ModuleError(str(ex)) |
| 658 |
|
| 659 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 660 |
return True |
| 661 |
raise F5ModuleError(resp.content) |
| 662 |
|
| 663 |
def absent(self): |
| 664 |
if self.exists(): |
| 665 |
return self.remove() |
| 666 |
return False |
| 667 |
|
| 668 |
def remove_from_device(self): |
| 669 |
uri = "https://{0}:{1}/mgmt/tm/net/ipsec/ipsec-policy/{2}".format( |
| 670 |
self.client.provider['server'], |
| 671 |
self.client.provider['server_port'], |
| 672 |
transform_name(self.want.partition, self.want.name) |
| 673 |
) |
| 674 |
response = self.client.api.delete(uri) |
| 675 |
|
| 676 |
if response.status in [200, 201]: |
| 677 |
return True |
| 678 |
raise F5ModuleError(response.content) |
| 679 |
|
| 680 |
def read_current_from_device(self): |
| 681 |
uri = "https://{0}:{1}/mgmt/tm/net/ipsec/ipsec-policy/{2}".format( |
| 682 |
self.client.provider['server'], |
| 683 |
self.client.provider['server_port'], |
| 684 |
transform_name(self.want.partition, self.want.name) |
| 685 |
) |
| 686 |
resp = self.client.api.get(uri) |
| 687 |
try: |
| 688 |
response = resp.json() |
| 689 |
except ValueError as ex: |
| 690 |
raise F5ModuleError(str(ex)) |
| 691 |
|
| 692 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 693 |
return ApiParameters(params=response) |
| 694 |
raise F5ModuleError(resp.content) |
| 695 |
|
| 696 |
|
| 697 |
class ArgumentSpec(object): |
| 698 |
def __init__(self): |
| 699 |
self.supports_check_mode = True |
| 700 |
argument_spec = dict( |
| 701 |
name=dict(required=True), |
| 702 |
description=dict(), |
| 703 |
protocol=dict( |
| 704 |
choices=['esp', 'ah'] |
| 705 |
), |
| 706 |
mode=dict( |
| 707 |
choices=['transport', 'interface', 'isession', 'tunnel'] |
| 708 |
), |
| 709 |
ipv4_interface=dict(type='bool'), |
| 710 |
tunnel_local_address=dict(), |
| 711 |
tunnel_remote_address=dict(), |
| 712 |
encrypt_algorithm=dict( |
| 713 |
choices=[ |
| 714 |
'none', '3des', 'aes128', 'aes192', 'aes256', 'aes-gmac256', |
| 715 |
'aes-gmac192', 'aes-gmac128', 'aes-gcm256', 'aes-gcm192', |
| 716 |
'aes-gcm256', 'aes-gcm128' |
| 717 |
] |
| 718 |
), |
| 719 |
route_domain=dict(type='int'), |
| 720 |
auth_algorithm=dict( |
| 721 |
choices=[ |
| 722 |
'sha1', 'sha256', 'sha384', 'sha512', 'aes-gcm128', |
| 723 |
'aes-gcm192', 'aes-gcm256', 'aes-gmac128', 'aes-gmac192', |
| 724 |
'aes-gmac256', |
| 725 |
] |
| 726 |
), |
| 727 |
ipcomp=dict( |
| 728 |
choices=['none', 'null', 'deflate'] |
| 729 |
), |
| 730 |
lifetime=dict(type='int'), |
| 731 |
kb_lifetime=dict(type='int'), |
| 732 |
perfect_forward_secrecy=dict( |
| 733 |
choices=[ |
| 734 |
'none', 'modp768', 'modp1024', 'modp1536', 'modp2048', 'modp3072', |
| 735 |
'modp4096', 'modp6144', 'modp8192' |
| 736 |
] |
| 737 |
), |
| 738 |
state=dict(default='present', choices=['absent', 'present']), |
| 739 |
partition=dict( |
| 740 |
default='Common', |
| 741 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 742 |
) |
| 743 |
) |
| 744 |
self.argument_spec = {} |
| 745 |
self.argument_spec.update(f5_argument_spec) |
| 746 |
self.argument_spec.update(argument_spec) |
| 747 |
self.required_if = [ |
| 748 |
['mode', 'tunnel', ['tunnel_local_address', 'tunnel_remote_address']], |
| 749 |
['mode', 'interface', ['route_domain']] |
| 750 |
] |
| 751 |
|
| 752 |
|
| 753 |
def main(): |
| 754 |
spec = ArgumentSpec() |
| 755 |
|
| 756 |
module = AnsibleModule( |
| 757 |
argument_spec=spec.argument_spec, |
| 758 |
supports_check_mode=spec.supports_check_mode, |
| 759 |
required_if=spec.required_if |
| 760 |
) |
| 761 |
|
| 762 |
try: |
| 763 |
mm = ModuleManager(module=module) |
| 764 |
results = mm.exec_module() |
| 765 |
module.exit_json(**results) |
| 766 |
except F5ModuleError as ex: |
| 767 |
module.fail_json(msg=str(ex)) |
| 768 |
|
| 769 |
|
| 770 |
if __name__ == '__main__': |
| 771 |
main() |