| 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_dns_resolver |
| 13 |
short_description: Manage DNS resolvers on a BIG-IP |
| 14 |
description: |
| 15 |
- Manage DNS resolvers on a BIG-IP system. |
| 16 |
version_added: "1.0.0" |
| 17 |
options: |
| 18 |
name: |
| 19 |
description: |
| 20 |
- Specifies the name of the DNS resolver. |
| 21 |
type: str |
| 22 |
required: True |
| 23 |
route_domain: |
| 24 |
description: |
| 25 |
- Specifies the route domain the resolver uses for outbound traffic. |
| 26 |
type: int |
| 27 |
cache_size: |
| 28 |
description: |
| 29 |
- Specifies the size of the internal DNS resolver cache. |
| 30 |
- When creating a new resolver, if this parameter is not specified, the default |
| 31 |
is 5767168 bytes. |
| 32 |
- After the cache reaches this size, when new or refreshed content arrives, the |
| 33 |
system removes expired and older content and caches the new or updated content. |
| 34 |
type: int |
| 35 |
answer_default_zones: |
| 36 |
description: |
| 37 |
- Specifies whether the system answers DNS queries for the default zones localhost, |
| 38 |
reverse 127.0.0.1 and ::1, and AS112. |
| 39 |
- When creating a new resolver, if this parameter is not specified, the default |
| 40 |
is C(false), meaning the system passes along the DNS queries for the default zones. |
| 41 |
type: bool |
| 42 |
randomize_query_case: |
| 43 |
description: |
| 44 |
- When C(true), specifies the internal DNS resolver randomizes character case |
| 45 |
in domain name queries issued to the root DNS servers. |
| 46 |
- When creating a new resolver, if this parameter is not specified, the default |
| 47 |
is C(true). |
| 48 |
type: bool |
| 49 |
use_ipv4: |
| 50 |
description: |
| 51 |
- Specifies whether the system can use IPv4 to query backend nameservers. |
| 52 |
- An IPv4 Self IP and default route must be available for these queries to work |
| 53 |
successfully. |
| 54 |
- When creating a new resolver, if this parameter is not specified, the default |
| 55 |
is C(true). |
| 56 |
type: bool |
| 57 |
use_ipv6: |
| 58 |
description: |
| 59 |
- Specifies whether the system can use IPv6 to query backend nameservers. |
| 60 |
- An IPv6 Self IP and default route must be available for these queries to work |
| 61 |
successfully. |
| 62 |
- When creating a new resolver, if this parameter is not specified, the default |
| 63 |
is C(true). |
| 64 |
type: bool |
| 65 |
use_udp: |
| 66 |
description: |
| 67 |
- Specifies whether the system answers and issues UDP-formatted queries. |
| 68 |
- When creating a new resolver, if this parameter is not specified, the default |
| 69 |
is C(true). |
| 70 |
type: bool |
| 71 |
use_tcp: |
| 72 |
description: |
| 73 |
- Specifies whether the system answers and issues TCP-formatted queries. |
| 74 |
- When creating a new resolver, if this parameter is not specified, the default |
| 75 |
is C(true). |
| 76 |
type: bool |
| 77 |
state: |
| 78 |
description: |
| 79 |
- When C(present), ensures the resource exists. |
| 80 |
- When C(absent), ensures the resource is removed. |
| 81 |
type: str |
| 82 |
choices: |
| 83 |
- present |
| 84 |
- absent |
| 85 |
default: present |
| 86 |
partition: |
| 87 |
description: |
| 88 |
- Device partition to manage resources on. |
| 89 |
type: str |
| 90 |
default: Common |
| 91 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 92 |
author: |
| 93 |
- Tim Rupp (@caphrim007) |
| 94 |
''' |
| 95 |
|
| 96 |
EXAMPLES = r''' |
| 97 |
- name: Create a simple DNS responder for OCSP stapling |
| 98 |
bigip_dns_resolver: |
| 99 |
name: resolver1 |
| 100 |
provider: |
| 101 |
password: secret |
| 102 |
server: lb.mydomain.com |
| 103 |
user: admin |
| 104 |
delegate_to: localhost |
| 105 |
''' |
| 106 |
|
| 107 |
RETURN = r''' |
| 108 |
route_domain: |
| 109 |
description: The new route domain of the resource. |
| 110 |
returned: changed |
| 111 |
type: str |
| 112 |
sample: /Common/0 |
| 113 |
cache_size: |
| 114 |
description: The new cache size of the resource. |
| 115 |
returned: changed |
| 116 |
type: int |
| 117 |
sample: 50000 |
| 118 |
answer_default_zones: |
| 119 |
description: The new Answer Default Zones setting. |
| 120 |
returned: changed |
| 121 |
type: bool |
| 122 |
sample: true |
| 123 |
randomize_query_case: |
| 124 |
description: The new Randomize Query Character Case setting. |
| 125 |
returned: changed |
| 126 |
type: bool |
| 127 |
sample: false |
| 128 |
use_ipv4: |
| 129 |
description: The new Use IPv4 setting. |
| 130 |
returned: changed |
| 131 |
type: bool |
| 132 |
sample: true |
| 133 |
use_ipv6: |
| 134 |
description: The new Use IPv6 setting. |
| 135 |
returned: changed |
| 136 |
type: bool |
| 137 |
sample: default |
| 138 |
use_udp: |
| 139 |
description: The new Use UDP setting. |
| 140 |
returned: changed |
| 141 |
type: bool |
| 142 |
sample: true |
| 143 |
use_tcp: |
| 144 |
description: The new Use TCP setting. |
| 145 |
returned: changed |
| 146 |
type: bool |
| 147 |
sample: false |
| 148 |
''' |
| 149 |
from datetime import datetime |
| 150 |
|
| 151 |
from ansible.module_utils.basic import ( |
| 152 |
AnsibleModule, env_fallback |
| 153 |
) |
| 154 |
|
| 155 |
from ..module_utils.bigip import F5RestClient |
| 156 |
from ..module_utils.common import ( |
| 157 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, flatten_boolean, fq_name |
| 158 |
) |
| 159 |
from ..module_utils.icontrol import tmos_version |
| 160 |
from ..module_utils.teem import send_teem |
| 161 |
|
| 162 |
|
| 163 |
class Parameters(AnsibleF5Parameters): |
| 164 |
api_map = { |
| 165 |
'answerDefaultZones': 'answer_default_zones', |
| 166 |
'cacheSize': 'cache_size', |
| 167 |
'randomizeQueryNameCase': 'randomize_query_case', |
| 168 |
'routeDomain': 'route_domain', |
| 169 |
'useIpv4': 'use_ipv4', |
| 170 |
'useIpv6': 'use_ipv6', |
| 171 |
'useTcp': 'use_tcp', |
| 172 |
'useUdp': 'use_udp', |
| 173 |
} |
| 174 |
|
| 175 |
api_attributes = [ |
| 176 |
'answerDefaultZones', |
| 177 |
'cacheSize', |
| 178 |
'randomizeQueryNameCase', |
| 179 |
'routeDomain', |
| 180 |
'useIpv4', |
| 181 |
'useIpv6', |
| 182 |
'useTcp', |
| 183 |
'useUdp', |
| 184 |
] |
| 185 |
|
| 186 |
returnables = [ |
| 187 |
'answer_default_zones', |
| 188 |
'cache_size', |
| 189 |
'randomize_query_case', |
| 190 |
'route_domain', |
| 191 |
'use_ipv4', |
| 192 |
'use_ipv6', |
| 193 |
'use_tcp', |
| 194 |
'use_udp', |
| 195 |
] |
| 196 |
|
| 197 |
updatables = [ |
| 198 |
'answer_default_zones', |
| 199 |
'cache_size', |
| 200 |
'randomize_query_case', |
| 201 |
'route_domain', |
| 202 |
'use_ipv4', |
| 203 |
'use_ipv6', |
| 204 |
'use_tcp', |
| 205 |
'use_udp', |
| 206 |
] |
| 207 |
|
| 208 |
@property |
| 209 |
def answer_default_zones(self): |
| 210 |
result = flatten_boolean(self._values['answer_default_zones']) |
| 211 |
return result |
| 212 |
|
| 213 |
@property |
| 214 |
def randomize_query_case(self): |
| 215 |
result = flatten_boolean(self._values['randomize_query_case']) |
| 216 |
return result |
| 217 |
|
| 218 |
@property |
| 219 |
def use_ipv4(self): |
| 220 |
result = flatten_boolean(self._values['use_ipv4']) |
| 221 |
return result |
| 222 |
|
| 223 |
@property |
| 224 |
def use_ipv6(self): |
| 225 |
result = flatten_boolean(self._values['use_ipv6']) |
| 226 |
return result |
| 227 |
|
| 228 |
@property |
| 229 |
def use_tcp(self): |
| 230 |
result = flatten_boolean(self._values['use_tcp']) |
| 231 |
return result |
| 232 |
|
| 233 |
@property |
| 234 |
def use_udp(self): |
| 235 |
result = flatten_boolean(self._values['use_udp']) |
| 236 |
return result |
| 237 |
|
| 238 |
|
| 239 |
class ApiParameters(Parameters): |
| 240 |
pass |
| 241 |
|
| 242 |
|
| 243 |
class ModuleParameters(Parameters): |
| 244 |
@property |
| 245 |
def route_domain(self): |
| 246 |
if self._values['route_domain'] is None: |
| 247 |
return None |
| 248 |
result = fq_name(self.partition, self._values['route_domain']) |
| 249 |
return result |
| 250 |
|
| 251 |
|
| 252 |
class Changes(Parameters): |
| 253 |
def to_return(self): |
| 254 |
result = {} |
| 255 |
try: |
| 256 |
for returnable in self.returnables: |
| 257 |
result[returnable] = getattr(self, returnable) |
| 258 |
result = self._filter_params(result) |
| 259 |
except Exception: |
| 260 |
raise |
| 261 |
return result |
| 262 |
|
| 263 |
|
| 264 |
class UsableChanges(Changes): |
| 265 |
pass |
| 266 |
|
| 267 |
|
| 268 |
class ReportableChanges(Changes): |
| 269 |
pass |
| 270 |
|
| 271 |
|
| 272 |
class Difference(object): |
| 273 |
def __init__(self, want, have=None): |
| 274 |
self.want = want |
| 275 |
self.have = have |
| 276 |
|
| 277 |
def compare(self, param): |
| 278 |
try: |
| 279 |
result = getattr(self, param) |
| 280 |
return result |
| 281 |
except AttributeError: |
| 282 |
return self.__default(param) |
| 283 |
|
| 284 |
def __default(self, param): |
| 285 |
attr1 = getattr(self.want, param) |
| 286 |
try: |
| 287 |
attr2 = getattr(self.have, param) |
| 288 |
if attr1 != attr2: |
| 289 |
return attr1 |
| 290 |
except AttributeError: |
| 291 |
return attr1 |
| 292 |
|
| 293 |
|
| 294 |
class ModuleManager(object): |
| 295 |
def __init__(self, *args, **kwargs): |
| 296 |
self.module = kwargs.get('module', None) |
| 297 |
self.client = F5RestClient(**self.module.params) |
| 298 |
self.want = ModuleParameters(params=self.module.params) |
| 299 |
self.have = ApiParameters() |
| 300 |
self.changes = UsableChanges() |
| 301 |
|
| 302 |
def _set_changed_options(self): |
| 303 |
changed = {} |
| 304 |
for key in Parameters.returnables: |
| 305 |
if getattr(self.want, key) is not None: |
| 306 |
changed[key] = getattr(self.want, key) |
| 307 |
if changed: |
| 308 |
self.changes = UsableChanges(params=changed) |
| 309 |
|
| 310 |
def _update_changed_options(self): |
| 311 |
diff = Difference(self.want, self.have) |
| 312 |
updatables = Parameters.updatables |
| 313 |
changed = dict() |
| 314 |
for k in updatables: |
| 315 |
change = diff.compare(k) |
| 316 |
if change is None: |
| 317 |
continue |
| 318 |
else: |
| 319 |
if isinstance(change, dict): |
| 320 |
changed.update(change) |
| 321 |
else: |
| 322 |
changed[k] = change |
| 323 |
if changed: |
| 324 |
self.changes = UsableChanges(params=changed) |
| 325 |
return True |
| 326 |
return False |
| 327 |
|
| 328 |
def should_update(self): |
| 329 |
result = self._update_changed_options() |
| 330 |
if result: |
| 331 |
return True |
| 332 |
return False |
| 333 |
|
| 334 |
def exec_module(self): |
| 335 |
start = datetime.now().isoformat() |
| 336 |
version = tmos_version(self.client) |
| 337 |
changed = False |
| 338 |
result = dict() |
| 339 |
state = self.want.state |
| 340 |
|
| 341 |
if state == "present": |
| 342 |
changed = self.present() |
| 343 |
elif state == "absent": |
| 344 |
changed = self.absent() |
| 345 |
|
| 346 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 347 |
changes = reportable.to_return() |
| 348 |
result.update(**changes) |
| 349 |
result.update(dict(changed=changed)) |
| 350 |
self._announce_deprecations(result) |
| 351 |
send_teem(start, self.client, self.module, version) |
| 352 |
return result |
| 353 |
|
| 354 |
def _announce_deprecations(self, result): |
| 355 |
warnings = result.pop('__warnings', []) |
| 356 |
for warning in warnings: |
| 357 |
self.client.module.deprecate( |
| 358 |
msg=warning['msg'], |
| 359 |
version=warning['version'] |
| 360 |
) |
| 361 |
|
| 362 |
def present(self): |
| 363 |
if self.exists(): |
| 364 |
return self.update() |
| 365 |
else: |
| 366 |
return self.create() |
| 367 |
|
| 368 |
def exists(self): |
| 369 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 370 |
uri = "https://{0}:{1}/mgmt/tm/net/dns-resolver/{2}".format( |
| 371 |
self.client.provider['server'], |
| 372 |
self.client.provider['server_port'], |
| 373 |
transform_name(self.want.partition, self.want.name) |
| 374 |
) |
| 375 |
resp = self.client.api.get(uri) |
| 376 |
try: |
| 377 |
response = resp.json() |
| 378 |
except ValueError as ex: |
| 379 |
raise F5ModuleError(str(ex)) |
| 380 |
|
| 381 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 382 |
return False |
| 383 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 384 |
return True |
| 385 |
|
| 386 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 387 |
if 'message' in response: |
| 388 |
raise F5ModuleError(response['message']) |
| 389 |
else: |
| 390 |
raise F5ModuleError(resp.content) |
| 391 |
|
| 392 |
def update(self): |
| 393 |
self.have = self.read_current_from_device() |
| 394 |
if not self.should_update(): |
| 395 |
return False |
| 396 |
if self.module.check_mode: |
| 397 |
return True |
| 398 |
self.update_on_device() |
| 399 |
return True |
| 400 |
|
| 401 |
def remove(self): |
| 402 |
if self.module.check_mode: |
| 403 |
return True |
| 404 |
self.remove_from_device() |
| 405 |
if self.exists(): |
| 406 |
raise F5ModuleError("Failed to delete the resource.") |
| 407 |
return True |
| 408 |
|
| 409 |
def create(self): |
| 410 |
self._set_changed_options() |
| 411 |
if self.module.check_mode: |
| 412 |
return True |
| 413 |
self.create_on_device() |
| 414 |
return True |
| 415 |
|
| 416 |
def create_on_device(self): |
| 417 |
params = self.changes.api_params() |
| 418 |
params['name'] = self.want.name |
| 419 |
params['partition'] = self.want.partition |
| 420 |
uri = "https://{0}:{1}/mgmt/tm/net/dns-resolver/".format( |
| 421 |
self.client.provider['server'], |
| 422 |
self.client.provider['server_port'] |
| 423 |
) |
| 424 |
resp = self.client.api.post(uri, json=params) |
| 425 |
try: |
| 426 |
response = resp.json() |
| 427 |
except ValueError as ex: |
| 428 |
raise F5ModuleError(str(ex)) |
| 429 |
|
| 430 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 431 |
return True |
| 432 |
raise F5ModuleError(resp.content) |
| 433 |
|
| 434 |
def update_on_device(self): |
| 435 |
params = self.changes.api_params() |
| 436 |
uri = "https://{0}:{1}/mgmt/tm/net/dns-resolver/{2}".format( |
| 437 |
self.client.provider['server'], |
| 438 |
self.client.provider['server_port'], |
| 439 |
transform_name(self.want.partition, self.want.name) |
| 440 |
) |
| 441 |
resp = self.client.api.patch(uri, json=params) |
| 442 |
try: |
| 443 |
response = resp.json() |
| 444 |
except ValueError as ex: |
| 445 |
raise F5ModuleError(str(ex)) |
| 446 |
|
| 447 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 448 |
return True |
| 449 |
raise F5ModuleError(resp.content) |
| 450 |
|
| 451 |
def absent(self): |
| 452 |
if self.exists(): |
| 453 |
return self.remove() |
| 454 |
return False |
| 455 |
|
| 456 |
def remove_from_device(self): |
| 457 |
uri = "https://{0}:{1}/mgmt/tm/net/dns-resolver/{2}".format( |
| 458 |
self.client.provider['server'], |
| 459 |
self.client.provider['server_port'], |
| 460 |
transform_name(self.want.partition, self.want.name) |
| 461 |
) |
| 462 |
response = self.client.api.delete(uri) |
| 463 |
if response.status == 200: |
| 464 |
return True |
| 465 |
raise F5ModuleError(response.content) |
| 466 |
|
| 467 |
def read_current_from_device(self): |
| 468 |
uri = "https://{0}:{1}/mgmt/tm/net/dns-resolver/{2}".format( |
| 469 |
self.client.provider['server'], |
| 470 |
self.client.provider['server_port'], |
| 471 |
transform_name(self.want.partition, self.want.name) |
| 472 |
) |
| 473 |
resp = self.client.api.get(uri) |
| 474 |
try: |
| 475 |
response = resp.json() |
| 476 |
except ValueError as ex: |
| 477 |
raise F5ModuleError(str(ex)) |
| 478 |
|
| 479 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 480 |
return ApiParameters(params=response) |
| 481 |
raise F5ModuleError(resp.content) |
| 482 |
|
| 483 |
|
| 484 |
class ArgumentSpec(object): |
| 485 |
def __init__(self): |
| 486 |
self.supports_check_mode = True |
| 487 |
argument_spec = dict( |
| 488 |
name=dict(required=True), |
| 489 |
route_domain=dict(type='int'), |
| 490 |
cache_size=dict(type='int'), |
| 491 |
answer_default_zones=dict(type='bool'), |
| 492 |
randomize_query_case=dict(type='bool'), |
| 493 |
use_ipv4=dict(type='bool'), |
| 494 |
use_ipv6=dict(type='bool'), |
| 495 |
use_udp=dict(type='bool'), |
| 496 |
use_tcp=dict(type='bool'), |
| 497 |
state=dict( |
| 498 |
default='present', |
| 499 |
choices=['present', 'absent'] |
| 500 |
), |
| 501 |
partition=dict( |
| 502 |
default='Common', |
| 503 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 504 |
) |
| 505 |
) |
| 506 |
self.argument_spec = {} |
| 507 |
self.argument_spec.update(f5_argument_spec) |
| 508 |
self.argument_spec.update(argument_spec) |
| 509 |
|
| 510 |
|
| 511 |
def main(): |
| 512 |
spec = ArgumentSpec() |
| 513 |
|
| 514 |
module = AnsibleModule( |
| 515 |
argument_spec=spec.argument_spec, |
| 516 |
supports_check_mode=spec.supports_check_mode, |
| 517 |
) |
| 518 |
|
| 519 |
try: |
| 520 |
mm = ModuleManager(module=module) |
| 521 |
results = mm.exec_module() |
| 522 |
module.exit_json(**results) |
| 523 |
except F5ModuleError as ex: |
| 524 |
module.fail_json(msg=str(ex)) |
| 525 |
|
| 526 |
|
| 527 |
if __name__ == '__main__': |
| 528 |
main() |