| 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_snmp_community |
| 13 |
short_description: Manages SNMP communities on a BIG-IP. |
| 14 |
description: |
| 15 |
- Assists in managing Simple Network Management Protocol (SNMP) communities on a BIG-IP system. Different SNMP versions are supported |
| 16 |
by this module. Note the different parameters offered by this module, as different |
| 17 |
parameters work for different versions of SNMP. This is important if you |
| 18 |
are mixing versions C(v2c) and C(3). |
| 19 |
version_added: "1.0.0" |
| 20 |
options: |
| 21 |
state: |
| 22 |
description: |
| 23 |
- When C(present), ensures the address list and entries exists. |
| 24 |
- When C(absent), ensures the address list is removed. |
| 25 |
type: str |
| 26 |
choices: |
| 27 |
- present |
| 28 |
- absent |
| 29 |
default: present |
| 30 |
version: |
| 31 |
description: |
| 32 |
- Specifies to which SNMP version the trap destination applies. |
| 33 |
type: str |
| 34 |
choices: |
| 35 |
- v1 |
| 36 |
- v2c |
| 37 |
- v3 |
| 38 |
default: v2c |
| 39 |
name: |
| 40 |
description: |
| 41 |
- Name that identifies the SNMP community. |
| 42 |
- When C(version) is C(v1) or C(v2c), this parameter is required. |
| 43 |
- The name C(public) is a reserved name on the BIG-IP. This module handles that name differently |
| 44 |
than others. Functionally, you should not see a difference. |
| 45 |
type: str |
| 46 |
community: |
| 47 |
description: |
| 48 |
- Specifies the community string (password) for access to the MIB. |
| 49 |
- This parameter is only relevant when C(version) is C(v1) or C(v2c). If C(version) is |
| 50 |
something else, this parameter is ignored. |
| 51 |
type: str |
| 52 |
source: |
| 53 |
description: |
| 54 |
- Specifies the source address for access to the MIB. |
| 55 |
- This parameter can accept a value of C(all). |
| 56 |
- If this parameter is not specified, the value is C(all). |
| 57 |
- This parameter is only relevant when C(version) is C(v1) or C(v2c). If C(version) is |
| 58 |
something else, this parameter is ignored. |
| 59 |
- If C(source) is set to C(all), it is not possible to specify an C(oid). This will |
| 60 |
raise an error. |
| 61 |
- You should provide this parameter when C(state) is C(absent), so the correct community |
| 62 |
is removed. To remove the C(public) SNMP community that comes with a BIG-IP, this parameter |
| 63 |
should be C(default). |
| 64 |
type: str |
| 65 |
port: |
| 66 |
description: |
| 67 |
- Specifies the port for the trap destination. |
| 68 |
- This parameter is only relevant when C(version) is C(v1) or C(v2c). If C(version) is |
| 69 |
something else, this parameter is ignored. |
| 70 |
type: int |
| 71 |
oid: |
| 72 |
description: |
| 73 |
- Specifies the object identifier (OID) for the record. |
| 74 |
- When C(version) is C(v3), this parameter is required. |
| 75 |
- When C(version) is either C(v1) or C(v2c), if this value is specified, then C(source) |
| 76 |
must not be set to C(all). |
| 77 |
type: str |
| 78 |
access: |
| 79 |
description: |
| 80 |
- Specifies the user's access level to the MIB. |
| 81 |
- When creating a new community, if this parameter is not specified, the default is C(ro). |
| 82 |
- When C(ro), specifies the user can view the MIB, but cannot modify the MIB. |
| 83 |
- When C(rw), specifies the user can view and modify the MIB. |
| 84 |
type: str |
| 85 |
choices: |
| 86 |
- ro |
| 87 |
- rw |
| 88 |
- read-only |
| 89 |
- read-write |
| 90 |
ip_version: |
| 91 |
description: |
| 92 |
- Specifies whether the record applies to IPv4 or IPv6 addresses. |
| 93 |
- When creating a new community, if this value is not specified, the default is C(4). |
| 94 |
- This parameter is only relevant when C(version) is C(v1) or C(v2c). If C(version) is |
| 95 |
something else, this parameter is ignored. |
| 96 |
type: str |
| 97 |
choices: |
| 98 |
- '4' |
| 99 |
- '6' |
| 100 |
snmp_username: |
| 101 |
description: |
| 102 |
- Specifies the name of the user for whom you want to grant access to the SNMP v3 MIB. |
| 103 |
- This parameter is only relevant when C(version) is C(v3). If C(version) is something |
| 104 |
else, this parameter is ignored. |
| 105 |
- When creating a new SNMP C(v3) community, this parameter is required. |
| 106 |
- This parameter cannot be changed once it has been set. |
| 107 |
type: str |
| 108 |
snmp_auth_protocol: |
| 109 |
description: |
| 110 |
- Specifies the authentication method for the user. |
| 111 |
- When C(md5), specifies the system uses the MD5 algorithm to authenticate the user. |
| 112 |
- When C(sha), specifies the secure hash algorithm (SHA) to authenticate the user. |
| 113 |
- When C(none), specifies the user does not require authentication. |
| 114 |
- When creating a new SNMP C(v3) community, if this parameter is not specified, the default |
| 115 |
is C(sha). |
| 116 |
type: str |
| 117 |
choices: |
| 118 |
- md5 |
| 119 |
- sha |
| 120 |
- none |
| 121 |
snmp_auth_password: |
| 122 |
description: |
| 123 |
- Specifies the password for the user. |
| 124 |
- When creating a new SNMP C(v3) community, this parameter is required. |
| 125 |
- This value must be at least 8 characters long. |
| 126 |
type: str |
| 127 |
snmp_privacy_protocol: |
| 128 |
description: |
| 129 |
- Specifies the encryption protocol. |
| 130 |
- When C(aes), specifies the system encrypts the user information using AES |
| 131 |
(Advanced Encryption Standard). |
| 132 |
- When C(des), specifies the system encrypts the user information using DES |
| 133 |
(Data Encryption Standard). |
| 134 |
- When C(none), specifies the system does not encrypt the user information. |
| 135 |
- When creating a new SNMP C(v3) community, if this parameter is not specified, the |
| 136 |
default is C(aes). |
| 137 |
type: str |
| 138 |
choices: |
| 139 |
- aes |
| 140 |
- des |
| 141 |
- none |
| 142 |
snmp_privacy_password: |
| 143 |
description: |
| 144 |
- Specifies the password for the user. |
| 145 |
- When creating a new SNMP C(v3) community, this parameter is required. |
| 146 |
- This value must be at least 8 characters long. |
| 147 |
type: str |
| 148 |
update_password: |
| 149 |
description: |
| 150 |
- C(always) allows users to update passwords. |
| 151 |
C(on_create) only sets the password for newly created resources. |
| 152 |
type: str |
| 153 |
choices: |
| 154 |
- always |
| 155 |
- on_create |
| 156 |
default: always |
| 157 |
partition: |
| 158 |
description: |
| 159 |
- Device partition to manage resources on. |
| 160 |
type: str |
| 161 |
default: Common |
| 162 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 163 |
author: |
| 164 |
- Tim Rupp (@caphrim007) |
| 165 |
- Wojciech Wypior (@wojtek0806) |
| 166 |
''' |
| 167 |
|
| 168 |
EXAMPLES = r''' |
| 169 |
- name: Create an SMNP v2c read-only community |
| 170 |
bigip_snmp_community: |
| 171 |
name: foo |
| 172 |
version: v2c |
| 173 |
source: all |
| 174 |
oid: .1 |
| 175 |
access: ro |
| 176 |
provider: |
| 177 |
password: secret |
| 178 |
server: lb.mydomain.com |
| 179 |
user: admin |
| 180 |
delegate_to: localhost |
| 181 |
|
| 182 |
- name: Create an SMNP v3 read-write community |
| 183 |
bigip_snmp_community: |
| 184 |
name: foo |
| 185 |
version: v3 |
| 186 |
snmp_username: foo |
| 187 |
snmp_auth_protocol: sha |
| 188 |
snmp_auth_password: secret |
| 189 |
snmp_privacy_protocol: aes |
| 190 |
snmp_privacy_password: secret |
| 191 |
oid: .1 |
| 192 |
access: rw |
| 193 |
provider: |
| 194 |
password: secret |
| 195 |
server: lb.mydomain.com |
| 196 |
user: admin |
| 197 |
delegate_to: localhost |
| 198 |
|
| 199 |
- name: Remove the default 'public' SNMP community |
| 200 |
bigip_snmp_community: |
| 201 |
name: public |
| 202 |
source: default |
| 203 |
state: absent |
| 204 |
provider: |
| 205 |
password: secret |
| 206 |
server: lb.mydomain.com |
| 207 |
user: admin |
| 208 |
delegate_to: localhost |
| 209 |
''' |
| 210 |
|
| 211 |
RETURN = r''' |
| 212 |
community: |
| 213 |
description: The new community value. |
| 214 |
returned: changed |
| 215 |
type: str |
| 216 |
sample: community1 |
| 217 |
oid: |
| 218 |
description: The new OID value. |
| 219 |
returned: changed |
| 220 |
type: str |
| 221 |
sample: .1 |
| 222 |
ip_version: |
| 223 |
description: The new IP version value. |
| 224 |
returned: changed |
| 225 |
type: str |
| 226 |
sample: .1 |
| 227 |
snmp_auth_protocol: |
| 228 |
description: The new SNMP auth protocol. |
| 229 |
returned: changed |
| 230 |
type: str |
| 231 |
sample: sha |
| 232 |
snmp_privacy_protocol: |
| 233 |
description: The new SNMP privacy protocol. |
| 234 |
returned: changed |
| 235 |
type: str |
| 236 |
sample: aes |
| 237 |
access: |
| 238 |
description: The new access level for the MIB. |
| 239 |
returned: changed |
| 240 |
type: str |
| 241 |
sample: ro |
| 242 |
source: |
| 243 |
description: The new source address to access the MIB. |
| 244 |
returned: changed |
| 245 |
type: str |
| 246 |
sample: 1.1.1.1 |
| 247 |
snmp_username: |
| 248 |
description: The new SNMP username. |
| 249 |
returned: changed |
| 250 |
type: str |
| 251 |
sample: user1 |
| 252 |
snmp_auth_password: |
| 253 |
description: The new password of the given snmp_username. |
| 254 |
returned: changed |
| 255 |
type: str |
| 256 |
sample: secret1 |
| 257 |
snmp_privacy_password: |
| 258 |
description: The new password of the given snmp_username. |
| 259 |
returned: changed |
| 260 |
type: str |
| 261 |
sample: secret2 |
| 262 |
''' |
| 263 |
from datetime import datetime |
| 264 |
import re |
| 265 |
from ansible.module_utils.basic import ( |
| 266 |
AnsibleModule, env_fallback |
| 267 |
) |
| 268 |
|
| 269 |
from ..module_utils.bigip import F5RestClient |
| 270 |
from ..module_utils.common import ( |
| 271 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec |
| 272 |
) |
| 273 |
from ..module_utils.icontrol import tmos_version |
| 274 |
from ..module_utils.teem import send_teem |
| 275 |
|
| 276 |
|
| 277 |
class Parameters(AnsibleF5Parameters): |
| 278 |
api_map = { |
| 279 |
'communityName': 'community', |
| 280 |
'oidSubset': 'oid', |
| 281 |
'ipv6': 'ip_version', |
| 282 |
'authProtocol': 'snmp_auth_protocol', |
| 283 |
'privacyProtocol': 'snmp_privacy_protocol', |
| 284 |
'username': 'snmp_username', |
| 285 |
'securityLevel': 'security_level', |
| 286 |
'authPassword': 'snmp_auth_password', |
| 287 |
'privacyPassword': 'snmp_privacy_password', |
| 288 |
} |
| 289 |
|
| 290 |
api_attributes = [ |
| 291 |
'source', |
| 292 |
'oidSubset', |
| 293 |
'ipv6', |
| 294 |
'communityName', |
| 295 |
'access', |
| 296 |
'authPassword', |
| 297 |
'authProtocol', |
| 298 |
'username', |
| 299 |
'securityLevel', |
| 300 |
'privacyProtocol', |
| 301 |
'privacyPassword', |
| 302 |
] |
| 303 |
|
| 304 |
returnables = [ |
| 305 |
'community', |
| 306 |
'oid', |
| 307 |
'ip_version', |
| 308 |
'snmp_auth_protocol', |
| 309 |
'snmp_privacy_protocol', |
| 310 |
'access', |
| 311 |
'source', |
| 312 |
'snmp_username', |
| 313 |
'snmp_auth_password', |
| 314 |
'snmp_privacy_password', |
| 315 |
] |
| 316 |
|
| 317 |
updatables = [ |
| 318 |
'community', |
| 319 |
'oid', |
| 320 |
'ip_version', |
| 321 |
'snmp_auth_protocol', |
| 322 |
'snmp_privacy_protocol', |
| 323 |
'access', |
| 324 |
'source', |
| 325 |
'snmp_auth_password', |
| 326 |
'snmp_privacy_password', |
| 327 |
'security_level', |
| 328 |
'snmp_username', |
| 329 |
] |
| 330 |
|
| 331 |
@property |
| 332 |
def port(self): |
| 333 |
if self._values['port'] is None: |
| 334 |
return None |
| 335 |
return int(self._values['port']) |
| 336 |
|
| 337 |
|
| 338 |
class ApiParameters(Parameters): |
| 339 |
@property |
| 340 |
def ip_version(self): |
| 341 |
if self._values['ip_version'] is None: |
| 342 |
return None |
| 343 |
if self._values['ip_version'] == 'enabled': |
| 344 |
return 6 |
| 345 |
return 4 |
| 346 |
|
| 347 |
@property |
| 348 |
def source(self): |
| 349 |
if self._values['source'] is None: |
| 350 |
return 'all' |
| 351 |
return self._values['source'] |
| 352 |
|
| 353 |
|
| 354 |
class ModuleParameters(Parameters): |
| 355 |
@property |
| 356 |
def ip_version(self): |
| 357 |
if self._values['ip_version'] is None: |
| 358 |
return None |
| 359 |
return int(self._values['ip_version']) |
| 360 |
|
| 361 |
@property |
| 362 |
def source(self): |
| 363 |
if self._values['source'] is None: |
| 364 |
return None |
| 365 |
if self._values['source'] == '': |
| 366 |
return 'all' |
| 367 |
return self._values['source'] |
| 368 |
|
| 369 |
@property |
| 370 |
def access(self): |
| 371 |
if self._values['access'] is None: |
| 372 |
return None |
| 373 |
elif self._values['access'] in ['ro', 'read-only']: |
| 374 |
return 'ro' |
| 375 |
elif self._values['access'] in ['rw', 'read-write']: |
| 376 |
return 'rw' |
| 377 |
else: |
| 378 |
raise F5ModuleError( |
| 379 |
"Unknown access format specified: '{0}'.".format(self._values['access']) |
| 380 |
) |
| 381 |
|
| 382 |
@property |
| 383 |
def snmp_auth_password(self): |
| 384 |
if self._values['snmp_auth_password'] is None: |
| 385 |
return None |
| 386 |
if len(self._values['snmp_auth_password']) < 8: |
| 387 |
raise F5ModuleError( |
| 388 |
"snmp_auth_password must be at least 8 characters long." |
| 389 |
) |
| 390 |
return self._values['snmp_auth_password'] |
| 391 |
|
| 392 |
@property |
| 393 |
def snmp_privacy_password(self): |
| 394 |
if self._values['snmp_privacy_password'] is None: |
| 395 |
return None |
| 396 |
if len(self._values['snmp_privacy_password']) < 8: |
| 397 |
raise F5ModuleError( |
| 398 |
"snmp_privacy_password must be at least 8 characters long." |
| 399 |
) |
| 400 |
return self._values['snmp_privacy_password'] |
| 401 |
|
| 402 |
@property |
| 403 |
def name(self): |
| 404 |
if self._values['name'] == 'public': |
| 405 |
return 'comm-public' |
| 406 |
return self._values['name'] |
| 407 |
|
| 408 |
|
| 409 |
class Changes(Parameters): |
| 410 |
def to_return(self): |
| 411 |
result = {} |
| 412 |
try: |
| 413 |
for returnable in self.returnables: |
| 414 |
result[returnable] = getattr(self, returnable) |
| 415 |
result = self._filter_params(result) |
| 416 |
except Exception: |
| 417 |
pass |
| 418 |
return result |
| 419 |
|
| 420 |
|
| 421 |
class UsableChanges(Changes): |
| 422 |
@property |
| 423 |
def ip_version(self): |
| 424 |
if self._values['ip_version'] is None: |
| 425 |
return None |
| 426 |
elif self._values['ip_version'] == 4: |
| 427 |
return 'disabled' |
| 428 |
return 'enabled' |
| 429 |
|
| 430 |
@property |
| 431 |
def source(self): |
| 432 |
if self._values['source'] is None: |
| 433 |
return None |
| 434 |
if self._values['source'] == 'all': |
| 435 |
return '' |
| 436 |
return self._values['source'] |
| 437 |
|
| 438 |
|
| 439 |
class ReportableChanges(Changes): |
| 440 |
pass |
| 441 |
|
| 442 |
|
| 443 |
class Difference(object): |
| 444 |
def __init__(self, want, have=None): |
| 445 |
self.want = want |
| 446 |
self.have = have |
| 447 |
|
| 448 |
def compare(self, param): |
| 449 |
try: |
| 450 |
result = getattr(self, param) |
| 451 |
return result |
| 452 |
except AttributeError: |
| 453 |
return self.__default(param) |
| 454 |
|
| 455 |
def __default(self, param): |
| 456 |
attr1 = getattr(self.want, param) |
| 457 |
try: |
| 458 |
attr2 = getattr(self.have, param) |
| 459 |
if attr1 != attr2: |
| 460 |
return attr1 |
| 461 |
except AttributeError: |
| 462 |
return attr1 |
| 463 |
|
| 464 |
def _check_source_and_oid(self): |
| 465 |
if self.have.oid is not None: |
| 466 |
if self.want.source == 'all' and self.want.oid != '': |
| 467 |
raise F5ModuleError( |
| 468 |
"When specifying an 'all' source for a resource with an existing OID, " |
| 469 |
"you must specify a new, empty, OID." |
| 470 |
) |
| 471 |
if self.want.source == 'all' and self.want.oid != '': |
| 472 |
raise F5ModuleError( |
| 473 |
"When specifying an 'all' source for a resource, you may not specify an OID." |
| 474 |
) |
| 475 |
|
| 476 |
@property |
| 477 |
def source(self): |
| 478 |
self._check_source_and_oid() |
| 479 |
if self.want.source != self.have.source: |
| 480 |
return self.want.source |
| 481 |
|
| 482 |
@property |
| 483 |
def oid(self): |
| 484 |
self._check_source_and_oid() |
| 485 |
if self.want.oid != self.have.oid: |
| 486 |
return self.want.oid |
| 487 |
|
| 488 |
@property |
| 489 |
def snmp_privacy_password(self): |
| 490 |
if self.want.update_password == 'always' and self.want.snmp_privacy_password is not None: |
| 491 |
return self.want.snmp_privacy_password |
| 492 |
|
| 493 |
@property |
| 494 |
def snmp_auth_password(self): |
| 495 |
if self.want.update_password == 'always' and self.want.snmp_auth_password is not None: |
| 496 |
return self.want.snmp_auth_password |
| 497 |
|
| 498 |
|
| 499 |
class ModuleManager(object): |
| 500 |
def __init__(self, *args, **kwargs): |
| 501 |
self.kwargs = kwargs |
| 502 |
self.client = kwargs.get('client', None) |
| 503 |
self.module = kwargs.get('module', None) |
| 504 |
|
| 505 |
def exec_module(self): |
| 506 |
if self.version_is_less_than_3(): |
| 507 |
manager = self.get_manager('v1') |
| 508 |
else: |
| 509 |
manager = self.get_manager('v2') |
| 510 |
return manager.exec_module() |
| 511 |
|
| 512 |
def get_manager(self, type): |
| 513 |
if type == 'v1': |
| 514 |
return V1Manager(**self.kwargs) |
| 515 |
elif type == 'v2': |
| 516 |
return V2Manager(**self.kwargs) |
| 517 |
|
| 518 |
def version_is_less_than_3(self): |
| 519 |
version = self.module.params.get('version') |
| 520 |
if version == 'v3': |
| 521 |
return False |
| 522 |
else: |
| 523 |
return True |
| 524 |
|
| 525 |
|
| 526 |
class BaseManager(object): |
| 527 |
def __init__(self, *args, **kwargs): |
| 528 |
self.module = kwargs.get('module', None) |
| 529 |
self.client = F5RestClient(**self.module.params) |
| 530 |
self.want = ModuleParameters(params=self.module.params) |
| 531 |
self.have = ApiParameters() |
| 532 |
self.changes = UsableChanges() |
| 533 |
|
| 534 |
def _set_changed_options(self): |
| 535 |
changed = {} |
| 536 |
for key in Parameters.returnables: |
| 537 |
if getattr(self.want, key) is not None: |
| 538 |
changed[key] = getattr(self.want, key) |
| 539 |
if changed: |
| 540 |
self.changes = UsableChanges(params=changed) |
| 541 |
|
| 542 |
def _update_changed_options(self): |
| 543 |
diff = Difference(self.want, self.have) |
| 544 |
updatables = Parameters.updatables |
| 545 |
changed = dict() |
| 546 |
for k in updatables: |
| 547 |
change = diff.compare(k) |
| 548 |
if change is None: |
| 549 |
continue |
| 550 |
else: |
| 551 |
if isinstance(change, dict): |
| 552 |
changed.update(change) |
| 553 |
else: |
| 554 |
changed[k] = change |
| 555 |
if changed: |
| 556 |
self.changes = UsableChanges(params=changed) |
| 557 |
return True |
| 558 |
return False |
| 559 |
|
| 560 |
def should_update(self): |
| 561 |
result = self._update_changed_options() |
| 562 |
if result: |
| 563 |
return True |
| 564 |
return False |
| 565 |
|
| 566 |
def exec_module(self): |
| 567 |
start = datetime.now().isoformat() |
| 568 |
version = tmos_version(self.client) |
| 569 |
changed = False |
| 570 |
result = dict() |
| 571 |
state = self.want.state |
| 572 |
|
| 573 |
if state == "present": |
| 574 |
changed = self.present() |
| 575 |
elif state == "absent": |
| 576 |
changed = self.absent() |
| 577 |
|
| 578 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 579 |
changes = reportable.to_return() |
| 580 |
result.update(**changes) |
| 581 |
result.update(dict(changed=changed)) |
| 582 |
self._announce_deprecations(result) |
| 583 |
send_teem(start, self.client, self.module, version) |
| 584 |
return result |
| 585 |
|
| 586 |
def _announce_deprecations(self, result): |
| 587 |
warnings = result.pop('__warnings', []) |
| 588 |
for warning in warnings: |
| 589 |
self.client.module.deprecate( |
| 590 |
msg=warning['msg'], |
| 591 |
version=warning['version'] |
| 592 |
) |
| 593 |
|
| 594 |
def present(self): |
| 595 |
if self.exists(): |
| 596 |
return self.update() |
| 597 |
else: |
| 598 |
return self.create() |
| 599 |
|
| 600 |
def update(self): |
| 601 |
self.have = self.read_current_from_device() |
| 602 |
if not self.should_update(): |
| 603 |
return False |
| 604 |
if self.module.check_mode: |
| 605 |
return True |
| 606 |
self.update_on_device() |
| 607 |
return True |
| 608 |
|
| 609 |
def remove(self): |
| 610 |
if self.module.check_mode: |
| 611 |
return True |
| 612 |
self.remove_from_device() |
| 613 |
if self.exists(): |
| 614 |
raise F5ModuleError("Failed to delete the resource.") |
| 615 |
return True |
| 616 |
|
| 617 |
def absent(self): |
| 618 |
if self.exists(): |
| 619 |
return self.remove() |
| 620 |
return False |
| 621 |
|
| 622 |
|
| 623 |
class V1Manager(BaseManager): |
| 624 |
"""Handles SNMP v1 and v2c |
| 625 |
|
| 626 |
""" |
| 627 |
def create(self): |
| 628 |
if self.want.ip_version is None: |
| 629 |
self.want.update({'ip_version': 4}) |
| 630 |
if self.want.access is None: |
| 631 |
self.want.update({'access': 'ro'}) |
| 632 |
self._set_changed_options() |
| 633 |
if self.want.oid is not None and self.want.source == 'all': |
| 634 |
raise F5ModuleError( |
| 635 |
"When specify an oid, source may not be set to 'all'." |
| 636 |
) |
| 637 |
if self.module.check_mode: |
| 638 |
return True |
| 639 |
self.create_on_device() |
| 640 |
return True |
| 641 |
|
| 642 |
def exists(self): |
| 643 |
uri = "https://{0}:{1}/mgmt/tm/sys/snmp/communities/{2}".format( |
| 644 |
self.client.provider['server'], |
| 645 |
self.client.provider['server_port'], |
| 646 |
transform_name(self.want.partition, self.want.name) |
| 647 |
) |
| 648 |
resp = self.client.api.get(uri) |
| 649 |
try: |
| 650 |
response = resp.json() |
| 651 |
except ValueError as ex: |
| 652 |
raise F5ModuleError(str(ex)) |
| 653 |
|
| 654 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 655 |
return False |
| 656 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 657 |
return True |
| 658 |
|
| 659 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 660 |
|
| 661 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 662 |
if 'message' in response: |
| 663 |
raise F5ModuleError(response['message']) |
| 664 |
else: |
| 665 |
raise F5ModuleError(resp.content) |
| 666 |
|
| 667 |
def create_on_device(self): |
| 668 |
params = self.changes.api_params() |
| 669 |
params['name'] = self.want.name |
| 670 |
params['partition'] = self.want.partition |
| 671 |
uri = "https://{0}:{1}/mgmt/tm/sys/snmp/communities/".format( |
| 672 |
self.client.provider['server'], |
| 673 |
self.client.provider['server_port'], |
| 674 |
) |
| 675 |
resp = self.client.api.post(uri, json=params) |
| 676 |
try: |
| 677 |
response = resp.json() |
| 678 |
except ValueError as ex: |
| 679 |
raise F5ModuleError(str(ex)) |
| 680 |
|
| 681 |
if 'code' in response and response['code'] in [400, 403]: |
| 682 |
if 'message' in response: |
| 683 |
raise F5ModuleError(response['message']) |
| 684 |
else: |
| 685 |
raise F5ModuleError(resp.content) |
| 686 |
|
| 687 |
def update_on_device(self): |
| 688 |
params = self.changes.api_params() |
| 689 |
uri = "https://{0}:{1}/mgmt/tm/sys/snmp/communities/{2}".format( |
| 690 |
self.client.provider['server'], |
| 691 |
self.client.provider['server_port'], |
| 692 |
transform_name(self.want.partition, self.want.name) |
| 693 |
) |
| 694 |
resp = self.client.api.patch(uri, json=params) |
| 695 |
try: |
| 696 |
response = resp.json() |
| 697 |
except ValueError as ex: |
| 698 |
raise F5ModuleError(str(ex)) |
| 699 |
|
| 700 |
if 'code' in response and response['code'] == 400: |
| 701 |
if 'message' in response: |
| 702 |
raise F5ModuleError(response['message']) |
| 703 |
else: |
| 704 |
raise F5ModuleError(resp.content) |
| 705 |
|
| 706 |
def remove_from_device(self): |
| 707 |
uri = "https://{0}:{1}/mgmt/tm/sys/snmp/communities/{2}".format( |
| 708 |
self.client.provider['server'], |
| 709 |
self.client.provider['server_port'], |
| 710 |
transform_name(self.want.partition, self.want.name) |
| 711 |
) |
| 712 |
resp = self.client.api.delete(uri) |
| 713 |
if resp.status == 200: |
| 714 |
return True |
| 715 |
|
| 716 |
def read_current_from_device(self): |
| 717 |
uri = "https://{0}:{1}/mgmt/tm/sys/snmp/communities/{2}".format( |
| 718 |
self.client.provider['server'], |
| 719 |
self.client.provider['server_port'], |
| 720 |
transform_name(self.want.partition, self.want.name) |
| 721 |
) |
| 722 |
resp = self.client.api.get(uri) |
| 723 |
try: |
| 724 |
response = resp.json() |
| 725 |
except ValueError as ex: |
| 726 |
raise F5ModuleError(str(ex)) |
| 727 |
|
| 728 |
if 'code' in response and response['code'] == 400: |
| 729 |
if 'message' in response: |
| 730 |
raise F5ModuleError(response['message']) |
| 731 |
else: |
| 732 |
raise F5ModuleError(resp.content) |
| 733 |
return ApiParameters(params=response) |
| 734 |
|
| 735 |
|
| 736 |
class V2Manager(BaseManager): |
| 737 |
"""Handles SNMP v3 |
| 738 |
|
| 739 |
SNMP v3 has (almost) a completely separate set of variables than v2c or v1. |
| 740 |
The functionality is placed in this separate class to handle these differences. |
| 741 |
|
| 742 |
""" |
| 743 |
def create(self): |
| 744 |
if self.want.access is None: |
| 745 |
self.want.update({'access': 'ro'}) |
| 746 |
if self.want.snmp_auth_protocol is None: |
| 747 |
self.want.update({'snmp_auth_protocol': 'sha'}) |
| 748 |
if self.want.snmp_privacy_protocol is None: |
| 749 |
self.want.update({'snmp_privacy_protocol': 'aes'}) |
| 750 |
|
| 751 |
self._set_changed_options() |
| 752 |
if self.want.snmp_username is None: |
| 753 |
raise F5ModuleError( |
| 754 |
"snmp_username must be specified when creating a new v3 community." |
| 755 |
) |
| 756 |
if self.want.snmp_auth_password is None: |
| 757 |
raise F5ModuleError( |
| 758 |
"snmp_auth_password must be specified when creating a new v3 community." |
| 759 |
) |
| 760 |
if self.want.snmp_privacy_password is None: |
| 761 |
raise F5ModuleError( |
| 762 |
"snmp_privacy_password must be specified when creating a new v3 community." |
| 763 |
) |
| 764 |
if self.want.oid is None: |
| 765 |
raise F5ModuleError( |
| 766 |
"oid must be specified when creating a new v3 community." |
| 767 |
) |
| 768 |
if self.module.check_mode: |
| 769 |
return True |
| 770 |
self.create_on_device() |
| 771 |
return True |
| 772 |
|
| 773 |
def exists(self): |
| 774 |
name = self.update_username() |
| 775 |
uri = "https://{0}:{1}/mgmt/tm/sys/snmp/users/{2}".format( |
| 776 |
self.client.provider['server'], |
| 777 |
self.client.provider['server_port'], |
| 778 |
transform_name(self.want.partition, name) |
| 779 |
) |
| 780 |
resp = self.client.api.get(uri) |
| 781 |
try: |
| 782 |
response = resp.json() |
| 783 |
except ValueError: |
| 784 |
return False |
| 785 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 786 |
return False |
| 787 |
return True |
| 788 |
|
| 789 |
def create_on_device(self): |
| 790 |
params = self.changes.api_params() |
| 791 |
params['name'] = self.update_username() |
| 792 |
params['partition'] = self.want.partition |
| 793 |
uri = "https://{0}:{1}/mgmt/tm/sys/snmp/users/".format( |
| 794 |
self.client.provider['server'], |
| 795 |
self.client.provider['server_port'], |
| 796 |
) |
| 797 |
resp = self.client.api.post(uri, json=params) |
| 798 |
try: |
| 799 |
response = resp.json() |
| 800 |
except ValueError as ex: |
| 801 |
raise F5ModuleError(str(ex)) |
| 802 |
|
| 803 |
if 'code' in response and response['code'] in [400, 403]: |
| 804 |
if 'message' in response: |
| 805 |
raise F5ModuleError(response['message']) |
| 806 |
else: |
| 807 |
raise F5ModuleError(resp.content) |
| 808 |
|
| 809 |
def update_on_device(self): |
| 810 |
params = self.changes.api_params() |
| 811 |
name = self.update_username() |
| 812 |
uri = "https://{0}:{1}/mgmt/tm/sys/snmp/users/{2}".format( |
| 813 |
self.client.provider['server'], |
| 814 |
self.client.provider['server_port'], |
| 815 |
transform_name(self.want.partition, name) |
| 816 |
) |
| 817 |
resp = self.client.api.patch(uri, json=params) |
| 818 |
try: |
| 819 |
response = resp.json() |
| 820 |
except ValueError as ex: |
| 821 |
raise F5ModuleError(str(ex)) |
| 822 |
|
| 823 |
if 'code' in response and response['code'] == 400: |
| 824 |
if 'message' in response: |
| 825 |
raise F5ModuleError(response['message']) |
| 826 |
else: |
| 827 |
raise F5ModuleError(resp.content) |
| 828 |
|
| 829 |
def remove_from_device(self): |
| 830 |
name = self.update_username() |
| 831 |
uri = "https://{0}:{1}/mgmt/tm/sys/snmp/users/{2}".format( |
| 832 |
self.client.provider['server'], |
| 833 |
self.client.provider['server_port'], |
| 834 |
transform_name(self.want.partition, name) |
| 835 |
) |
| 836 |
resp = self.client.api.delete(uri) |
| 837 |
if resp.status == 200: |
| 838 |
return True |
| 839 |
|
| 840 |
def read_current_from_device(self): |
| 841 |
name = self.update_username() |
| 842 |
uri = "https://{0}:{1}/mgmt/tm/sys/snmp/users/{2}".format( |
| 843 |
self.client.provider['server'], |
| 844 |
self.client.provider['server_port'], |
| 845 |
transform_name(self.want.partition, name) |
| 846 |
) |
| 847 |
resp = self.client.api.get(uri) |
| 848 |
try: |
| 849 |
response = resp.json() |
| 850 |
except ValueError as ex: |
| 851 |
raise F5ModuleError(str(ex)) |
| 852 |
|
| 853 |
if 'code' in response and response['code'] == 400: |
| 854 |
if 'message' in response: |
| 855 |
raise F5ModuleError(response['message']) |
| 856 |
else: |
| 857 |
raise F5ModuleError(resp.content) |
| 858 |
return ApiParameters(params=response) |
| 859 |
|
| 860 |
def update_username(self): |
| 861 |
name = self.want.snmp_username |
| 862 |
if self.want.snmp_username.startswith(tuple("0123456789")) or re.search(r'[^a-zA-Z0-9\s]', self.want.snmp_username): |
| 863 |
name = re.sub(r'[^a-zA-Z0-9\s]', '_', self.want.snmp_username) |
| 864 |
name = "i" + name + "_1" |
| 865 |
return name |
| 866 |
|
| 867 |
|
| 868 |
class ArgumentSpec(object): |
| 869 |
def __init__(self): |
| 870 |
self.supports_check_mode = True |
| 871 |
argument_spec = dict( |
| 872 |
version=dict( |
| 873 |
default='v2c', |
| 874 |
choices=['v1', 'v2c', 'v3'] |
| 875 |
), |
| 876 |
name=dict(), |
| 877 |
community=dict(), |
| 878 |
source=dict(), |
| 879 |
port=dict(type='int'), |
| 880 |
oid=dict(), |
| 881 |
access=dict( |
| 882 |
choices=['ro', 'rw', 'read-only', 'read-write'] |
| 883 |
), |
| 884 |
ip_version=dict( |
| 885 |
choices=['4', '6'] |
| 886 |
), |
| 887 |
snmp_username=dict(), |
| 888 |
snmp_auth_protocol=dict( |
| 889 |
choices=['md5', 'sha', 'none'] |
| 890 |
), |
| 891 |
snmp_auth_password=dict(no_log=True), |
| 892 |
snmp_privacy_protocol=dict( |
| 893 |
choices=['aes', 'des', 'none'] |
| 894 |
), |
| 895 |
snmp_privacy_password=dict(no_log=True), |
| 896 |
update_password=dict( |
| 897 |
default='always', |
| 898 |
choices=['always', 'on_create'], |
| 899 |
no_log=False |
| 900 |
), |
| 901 |
state=dict(default='present', choices=['absent', 'present']), |
| 902 |
partition=dict( |
| 903 |
default='Common', |
| 904 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 905 |
), |
| 906 |
) |
| 907 |
self.argument_spec = {} |
| 908 |
self.argument_spec.update(f5_argument_spec) |
| 909 |
self.argument_spec.update(argument_spec) |
| 910 |
self.required_if = [ |
| 911 |
['version', 'v1', ['name']], |
| 912 |
['version', 'v2', ['name']], |
| 913 |
['version', 'v3', ['snmp_username']] |
| 914 |
] |
| 915 |
|
| 916 |
|
| 917 |
def main(): |
| 918 |
spec = ArgumentSpec() |
| 919 |
|
| 920 |
module = AnsibleModule( |
| 921 |
argument_spec=spec.argument_spec, |
| 922 |
supports_check_mode=spec.supports_check_mode, |
| 923 |
required_if=spec.required_if |
| 924 |
) |
| 925 |
|
| 926 |
try: |
| 927 |
mm = ModuleManager(module=module) |
| 928 |
results = mm.exec_module() |
| 929 |
module.exit_json(**results) |
| 930 |
except F5ModuleError as ex: |
| 931 |
module.fail_json(msg=str(ex)) |
| 932 |
|
| 933 |
|
| 934 |
if __name__ == '__main__': |
| 935 |
main() |