| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
ANSIBLE_METADATA = { |
| 12 |
"metadata_version": "1.1", |
| 13 |
"status": ["preview"], |
| 14 |
"supported_by": "community", |
| 15 |
} |
| 16 |
|
| 17 |
DOCUMENTATION = r""" |
| 18 |
--- |
| 19 |
module: purefb_snmp_mgr |
| 20 |
version_added: '1.0.0' |
| 21 |
short_description: Configure FlashBlade SNMP Managers |
| 22 |
description: |
| 23 |
- Manage SNMP managers on a Pure Storage FlashBlade. |
| 24 |
- This module is not idempotent and will always modify an |
| 25 |
existing SNMP manager due to hidden parameters that cannot |
| 26 |
be compared to the play parameters. |
| 27 |
author: |
| 28 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 29 |
options: |
| 30 |
name: |
| 31 |
description: |
| 32 |
- Name of SNMP Manager |
| 33 |
required: true |
| 34 |
type: str |
| 35 |
state: |
| 36 |
description: |
| 37 |
- Create, delete or test SNMP manager |
| 38 |
type: str |
| 39 |
default: present |
| 40 |
choices: [ absent, present, test ] |
| 41 |
auth_passphrase: |
| 42 |
type: str |
| 43 |
description: |
| 44 |
- SNMPv3 only. Passphrase of 8 - 32 characters. |
| 45 |
auth_protocol: |
| 46 |
type: str |
| 47 |
description: |
| 48 |
- SNMP v3 only. Hash algorithm to use |
| 49 |
choices: [ MD5, SHA ] |
| 50 |
community: |
| 51 |
type: str |
| 52 |
description: |
| 53 |
- SNMP v2c only. Manager community ID. Between 1 and 32 characters long. |
| 54 |
host: |
| 55 |
type: str |
| 56 |
description: |
| 57 |
- IPv4 or IPv6 address or FQDN to send trap messages to. |
| 58 |
user: |
| 59 |
type: str |
| 60 |
description: |
| 61 |
- SNMP v3 only. User ID recognized by the specified SNMP manager. |
| 62 |
Must be between 1 and 32 characters. |
| 63 |
version: |
| 64 |
type: str |
| 65 |
description: |
| 66 |
- Version of SNMP protocol to use for the manager. |
| 67 |
choices: [ v2c, v3 ] |
| 68 |
notification: |
| 69 |
type: str |
| 70 |
description: |
| 71 |
- Action to perform on event. |
| 72 |
default: trap |
| 73 |
choices: [ inform, trap ] |
| 74 |
privacy_passphrase: |
| 75 |
type: str |
| 76 |
description: |
| 77 |
- SNMPv3 only. Passphrase to encrypt SNMP messages. |
| 78 |
Must be between 8 and 63 non-space ASCII characters. |
| 79 |
privacy_protocol: |
| 80 |
type: str |
| 81 |
description: |
| 82 |
- SNMP v3 only. Encryption protocol to use |
| 83 |
choices: [ AES, DES ] |
| 84 |
extends_documentation_fragment: |
| 85 |
- purestorage.flashblade.purestorage.fb |
| 86 |
""" |
| 87 |
|
| 88 |
EXAMPLES = r""" |
| 89 |
- name: Delete exisitng SNMP manager |
| 90 |
purestorage.flashblade.purefb_snmp_mgr: |
| 91 |
name: manager1 |
| 92 |
state: absent |
| 93 |
fb_url: 10.10.10.2 |
| 94 |
api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6 |
| 95 |
- name: Create v2c SNMP manager |
| 96 |
purestorage.flashblade.purefb_snmp_mgr: |
| 97 |
name: manager1 |
| 98 |
community: public |
| 99 |
host: 10.21.22.23 |
| 100 |
fb_url: 10.10.10.2 |
| 101 |
api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6 |
| 102 |
- name: Create v3 SNMP manager |
| 103 |
purestorage.flashblade.purefb_snmp_mgr: |
| 104 |
name: manager2 |
| 105 |
version: v3 |
| 106 |
auth_protocol: MD5 |
| 107 |
auth_passphrase: password |
| 108 |
host: 10.21.22.23 |
| 109 |
fb_url: 10.10.10.2 |
| 110 |
api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6 |
| 111 |
- name: Update existing SNMP manager |
| 112 |
purestorage.flashblade.purefb_snmp_mgr: |
| 113 |
name: manager1 |
| 114 |
community: private |
| 115 |
fb_url: 10.10.10.2 |
| 116 |
api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6 |
| 117 |
""" |
| 118 |
|
| 119 |
RETURN = r""" |
| 120 |
""" |
| 121 |
|
| 122 |
|
| 123 |
HAS_PURITY_FB = True |
| 124 |
try: |
| 125 |
from pypureclient.flashblade import SnmpManager, SnmpV2c, SnmpV3 |
| 126 |
except ImportError: |
| 127 |
HAS_PURITY_FB = False |
| 128 |
|
| 129 |
|
| 130 |
from ansible.module_utils.basic import AnsibleModule |
| 131 |
from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import ( |
| 132 |
get_system, |
| 133 |
purefb_argument_spec, |
| 134 |
) |
| 135 |
|
| 136 |
|
| 137 |
def update_manager(module, blade): |
| 138 |
"""Update SNMP Manager""" |
| 139 |
changed = False |
| 140 |
res = blade.get_snmp_managers(names=[module.params["name"]]) |
| 141 |
mgr = list(blade.get_snmp_managers(names=[module.params["name"]]).items)[0] |
| 142 |
if res.status_code != 200: |
| 143 |
module.fail_json( |
| 144 |
msg="Failed to get configuration for SNMP manager {0}. Error: {1}".format( |
| 145 |
module.params["name"], mgr.errors[0].message |
| 146 |
) |
| 147 |
) |
| 148 |
mgr = list(res.items)[0] |
| 149 |
current_attr = { |
| 150 |
"community": getattr(mgr.v2c, "community", None), |
| 151 |
"notification": mgr.notification, |
| 152 |
"host": mgr.host, |
| 153 |
"version": mgr.version, |
| 154 |
"auth_passphrase": getattr(mgr.v3, "auth_passphrase", None), |
| 155 |
"auth_protocol": getattr(mgr.v3, "auth_protocol", None), |
| 156 |
"privacy_passphrase": getattr(mgr.v3, "privacy_passphrase", None), |
| 157 |
"privacy_protocol": getattr(mgr.v3, "privacy_protocol", None), |
| 158 |
"user": getattr(mgr.v3, "user", None), |
| 159 |
} |
| 160 |
new_attr = { |
| 161 |
"community": module.params["community"], |
| 162 |
"notification": module.params["notification"], |
| 163 |
"host": module.params["host"], |
| 164 |
"version": module.params["version"], |
| 165 |
"auth_passphrase": module.params["auth_passphrase"], |
| 166 |
"auth_protocol": module.params["auth_protocol"], |
| 167 |
"privacy_passphrase": module.params["privacy_passphrase"], |
| 168 |
"privacy_protocol": module.params["privacy_protocol"], |
| 169 |
"user": module.params["user"], |
| 170 |
} |
| 171 |
if current_attr != new_attr: |
| 172 |
changed = True |
| 173 |
if not module.check_mode: |
| 174 |
if new_attr["version"] == "v2c": |
| 175 |
updated_v2c_attrs = SnmpV2c(community=new_attr["community"]) |
| 176 |
updated_v2c_manager = SnmpManager( |
| 177 |
host=new_attr["host"], |
| 178 |
notification=new_attr["notification"], |
| 179 |
version="v2c", |
| 180 |
v2c=updated_v2c_attrs, |
| 181 |
) |
| 182 |
res = blade.patch_snmp_managers( |
| 183 |
names=[module.params["name"]], snmp_manager=updated_v2c_manager |
| 184 |
) |
| 185 |
if res.status_code != 200: |
| 186 |
module.fail_json( |
| 187 |
msg="Failed to update v2c SNMP manager {0}. Error: {1}".format( |
| 188 |
module.params["name"], res.errors[0].message |
| 189 |
) |
| 190 |
) |
| 191 |
else: |
| 192 |
updated_v3_attrs = SnmpV3( |
| 193 |
auth_protocol=new_attr["auth_protocol"], |
| 194 |
auth_passphrase=new_attr["auth_passphrase"], |
| 195 |
privacy_protocol=new_attr["privacy_protocol"], |
| 196 |
privacy_passphrase=new_attr["privacy_passphrase"], |
| 197 |
user=new_attr["user"], |
| 198 |
) |
| 199 |
updated_v3_manager = SnmpManager( |
| 200 |
host=new_attr["host"], |
| 201 |
notification=new_attr["notification"], |
| 202 |
version="v3", |
| 203 |
v3=updated_v3_attrs, |
| 204 |
) |
| 205 |
res = blade.patch_snmp_managers( |
| 206 |
names=[module.params["name"]], snmp_manager=updated_v3_manager |
| 207 |
) |
| 208 |
if res.status_code != 200: |
| 209 |
module.fail_json( |
| 210 |
msg="Failed to update v3 SNMP manager {0}. Error: {1}".format( |
| 211 |
module.params["name"], res.errors[0].message |
| 212 |
) |
| 213 |
) |
| 214 |
|
| 215 |
module.exit_json(changed=changed) |
| 216 |
|
| 217 |
|
| 218 |
def delete_manager(module, blade): |
| 219 |
"""Delete SNMP Manager""" |
| 220 |
changed = True |
| 221 |
if not module.check_mode: |
| 222 |
res = blade.delete_snmp_managers(names=[module.params["name"]]) |
| 223 |
if res.status_code != 200: |
| 224 |
module.fail_json( |
| 225 |
msg="Delete SNMP manager {0} failed. Error: {1}".format( |
| 226 |
module.params["name"], res.errors[0].message |
| 227 |
) |
| 228 |
) |
| 229 |
module.exit_json(changed=changed) |
| 230 |
|
| 231 |
|
| 232 |
def create_manager(module, blade): |
| 233 |
"""Create SNMP Manager""" |
| 234 |
changed = True |
| 235 |
if not module.check_mode: |
| 236 |
if not module.params["version"]: |
| 237 |
module.fail_json(msg="SNMP version required to create a new manager") |
| 238 |
if module.params["version"] == "v2c": |
| 239 |
v2_attrs = SnmpV2c(community=module.params["community"]) |
| 240 |
new_v2_manager = SnmpManager( |
| 241 |
host=module.params["host"], |
| 242 |
notification=module.params["notification"], |
| 243 |
version="v2c", |
| 244 |
v2c=v2_attrs, |
| 245 |
) |
| 246 |
res = blade.post_snmp_managers( |
| 247 |
names=[module.params["name"]], snmp_manager=new_v2_manager |
| 248 |
) |
| 249 |
if res.status_code != 200: |
| 250 |
module.fail_json( |
| 251 |
msg="Failed to create v2c SNMP manager {0}. Error: {1}".format( |
| 252 |
module.params["name"], res.errors[0].message |
| 253 |
) |
| 254 |
) |
| 255 |
else: |
| 256 |
v3_attrs = SnmpV3( |
| 257 |
auth_protocol=module.params["auth_protocol"], |
| 258 |
auth_passphrase=module.params["auth_passphrase"], |
| 259 |
privacy_protocol=module.params["privacy_protocol"], |
| 260 |
privacy_passphrase=module.params["privacy_passphrase"], |
| 261 |
user=module.params["user"], |
| 262 |
) |
| 263 |
new_v3_manager = SnmpManager( |
| 264 |
host=module.params["host"], |
| 265 |
notification=module.params["notification"], |
| 266 |
version="v3", |
| 267 |
v3=v3_attrs, |
| 268 |
) |
| 269 |
res = blade.post_snmp_managers( |
| 270 |
names=[module.params["name"]], snmp_manager=new_v3_manager |
| 271 |
) |
| 272 |
if res.status_code != 200: |
| 273 |
module.fail_json( |
| 274 |
msg="Failed to create v3 SNMP manager {0}. Error: {1}".format( |
| 275 |
module.params["name"], res.errors[0].message |
| 276 |
) |
| 277 |
) |
| 278 |
module.exit_json(changed=changed) |
| 279 |
|
| 280 |
|
| 281 |
def test_manager(module, blade): |
| 282 |
"""Test SNMP manager configuration""" |
| 283 |
test_response = [] |
| 284 |
response = list(blade.get_snmp_managers_test(names=[module.params["name"]]).items) |
| 285 |
for component in range(len(response)): |
| 286 |
if response[component].enabled: |
| 287 |
enabled = "true" |
| 288 |
else: |
| 289 |
enabled = "false" |
| 290 |
if response[component].success: |
| 291 |
success = "true" |
| 292 |
else: |
| 293 |
success = "false" |
| 294 |
test_response.append( |
| 295 |
{ |
| 296 |
"component_address": response[component].component_address, |
| 297 |
"component_name": response[component].component_name, |
| 298 |
"description": response[component].description, |
| 299 |
"destination": response[component].destination, |
| 300 |
"enabled": enabled, |
| 301 |
"result_details": getattr(response[component], "result_details", ""), |
| 302 |
"success": success, |
| 303 |
"test_type": response[component].test_type, |
| 304 |
"resource_name": response[component].resource.name, |
| 305 |
} |
| 306 |
) |
| 307 |
module.exit_json(changed=False, test_response=test_response) |
| 308 |
|
| 309 |
|
| 310 |
def main(): |
| 311 |
argument_spec = purefb_argument_spec() |
| 312 |
argument_spec.update( |
| 313 |
dict( |
| 314 |
name=dict(type="str", required=True), |
| 315 |
host=dict(type="str"), |
| 316 |
state=dict( |
| 317 |
type="str", default="present", choices=["absent", "present", "test"] |
| 318 |
), |
| 319 |
user=dict(type="str"), |
| 320 |
notification=dict(type="str", choices=["inform", "trap"], default="trap"), |
| 321 |
auth_passphrase=dict(type="str", no_log=True), |
| 322 |
auth_protocol=dict(type="str", choices=["MD5", "SHA"]), |
| 323 |
privacy_passphrase=dict(type="str", no_log=True), |
| 324 |
privacy_protocol=dict(type="str", choices=["AES", "DES"]), |
| 325 |
version=dict(type="str", choices=["v2c", "v3"]), |
| 326 |
community=dict(type="str"), |
| 327 |
) |
| 328 |
) |
| 329 |
|
| 330 |
required_together = [ |
| 331 |
["auth_passphrase", "auth_protocol"], |
| 332 |
["privacy_passphrase", "privacy_protocol"], |
| 333 |
] |
| 334 |
required_if = [ |
| 335 |
["version", "v2c", ["community", "host"]], |
| 336 |
["version", "v3", ["host", "user"]], |
| 337 |
] |
| 338 |
|
| 339 |
module = AnsibleModule( |
| 340 |
argument_spec, |
| 341 |
required_together=required_together, |
| 342 |
required_if=required_if, |
| 343 |
supports_check_mode=True, |
| 344 |
) |
| 345 |
|
| 346 |
state = module.params["state"] |
| 347 |
blade = get_system(module) |
| 348 |
|
| 349 |
if not HAS_PURITY_FB: |
| 350 |
module.fail_json(msg="py-pure-client SDK is required for this module") |
| 351 |
|
| 352 |
mgr_configured = False |
| 353 |
mgrs = list(blade.get_snmp_managers().items) |
| 354 |
for mgr in range(len(mgrs)): |
| 355 |
if mgrs[mgr].name == module.params["name"]: |
| 356 |
mgr_configured = True |
| 357 |
break |
| 358 |
if module.params["version"] == "v3": |
| 359 |
if module.params["auth_passphrase"] and ( |
| 360 |
8 > len(module.params["auth_passphrase"]) > 32 |
| 361 |
): |
| 362 |
module.fail_json(msg="auth_password must be between 8 and 32 characters") |
| 363 |
if ( |
| 364 |
module.params["privacy_passphrase"] |
| 365 |
and 8 > len(module.params["privacy_passphrase"]) > 63 |
| 366 |
): |
| 367 |
module.fail_json(msg="privacy_password must be between 8 and 63 characters") |
| 368 |
if state == "absent" and mgr_configured: |
| 369 |
delete_manager(module, blade) |
| 370 |
elif mgr_configured and state == "present": |
| 371 |
update_manager(module, blade) |
| 372 |
elif not mgr_configured and state == "present": |
| 373 |
create_manager(module, blade) |
| 374 |
elif state == "test" and mgr_configured: |
| 375 |
test_manager(module, blade) |
| 376 |
elif state == "test" and not mgr_configured: |
| 377 |
module.fail_json( |
| 378 |
msg="SNMP Manager {0} not configured".format(module.params["name"]) |
| 379 |
) |
| 380 |
else: |
| 381 |
module.exit_json(changed=False) |
| 382 |
|
| 383 |
|
| 384 |
if __name__ == "__main__": |
| 385 |
main() |