| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: NetApp, Inc |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = """ |
| 11 |
module: na_ontap_snmp_config |
| 12 |
short_description: NetApp ONTAP module to modify SNMP configuration. |
| 13 |
extends_documentation_fragment: |
| 14 |
- netapp.ontap.netapp.na_ontap_rest |
| 15 |
version_added: '22.9.0' |
| 16 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 17 |
description: |
| 18 |
- Modify cluster wide SNMP configuration. |
| 19 |
- Enable or disable SNMP on a cluster. |
| 20 |
options: |
| 21 |
state: |
| 22 |
description: |
| 23 |
- Modify SNMP configuration, only present is supported. |
| 24 |
choices: ['present'] |
| 25 |
type: str |
| 26 |
default: present |
| 27 |
enabled: |
| 28 |
description: |
| 29 |
- Specifies whether to enable or disable SNMP. |
| 30 |
type: bool |
| 31 |
required: false |
| 32 |
auth_traps_enabled: |
| 33 |
description: |
| 34 |
- Specifies whether to enable or disable SNMP authentication traps. |
| 35 |
type: bool |
| 36 |
required: false |
| 37 |
traps_enabled: |
| 38 |
description: |
| 39 |
- Specifies whether to enable or disable SNMP traps. |
| 40 |
- Requires ONTAP 9.10.1 or later. |
| 41 |
type: bool |
| 42 |
required: false |
| 43 |
|
| 44 |
notes: |
| 45 |
- Only supported with REST and requires ONTAP 9.7 or later. |
| 46 |
""" |
| 47 |
|
| 48 |
EXAMPLES = """ |
| 49 |
- name: Disable SNMP on cluster |
| 50 |
netapp.ontap.na_ontap_snmp_config: |
| 51 |
state: present |
| 52 |
enabled: false |
| 53 |
hostname: "{{ netapp_hostname }}" |
| 54 |
username: "{{ netapp_username }}" |
| 55 |
password: "{{ netapp_password }}" |
| 56 |
https: true |
| 57 |
validate_certs: "{{ validate_certs }}" |
| 58 |
|
| 59 |
- name: Modify SNMP configuration |
| 60 |
netapp.ontap.na_ontap_snmp_config: |
| 61 |
state: present |
| 62 |
auth_traps_enabled: true |
| 63 |
traps_enabled: true |
| 64 |
hostname: "{{ netapp_hostname }}" |
| 65 |
username: "{{ netapp_username }}" |
| 66 |
password: "{{ netapp_password }}" |
| 67 |
https: true |
| 68 |
validate_certs: "{{ validate_certs }}" |
| 69 |
""" |
| 70 |
|
| 71 |
RETURN = """ |
| 72 |
""" |
| 73 |
|
| 74 |
import traceback |
| 75 |
from ansible.module_utils.basic import AnsibleModule |
| 76 |
from ansible.module_utils._text import to_native |
| 77 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 78 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 79 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 80 |
|
| 81 |
|
| 82 |
class NetAppOntapSNMPConfig: |
| 83 |
def __init__(self): |
| 84 |
self.argument_spec = netapp_utils.na_ontap_rest_only_spec() |
| 85 |
self.argument_spec.update(dict( |
| 86 |
state=dict(required=False, type='str', choices=['present'], default='present'), |
| 87 |
enabled=dict(required=False, type='bool'), |
| 88 |
auth_traps_enabled=dict(required=False, type='bool'), |
| 89 |
traps_enabled=dict(required=False, type='bool') |
| 90 |
)) |
| 91 |
self.module = AnsibleModule( |
| 92 |
argument_spec=self.argument_spec, |
| 93 |
supports_check_mode=True |
| 94 |
) |
| 95 |
self.uuid = None |
| 96 |
self.na_helper = NetAppModule(self.module) |
| 97 |
self.parameters = self.na_helper.check_and_set_parameters(self.module) |
| 98 |
self.rest_api = netapp_utils.OntapRestAPI(self.module) |
| 99 |
self.rest_api.fail_if_not_rest_minimum_version('na_ontap_snmp_config:', 9, 7) |
| 100 |
self.use_rest = self.rest_api.is_rest_supported_properties(self.parameters, None, [['traps_enabled', (9, 10, 1)]]) |
| 101 |
|
| 102 |
def get_snmp_config_rest(self): |
| 103 |
"""Retrieve cluster wide SNMP configuration""" |
| 104 |
fields = 'enabled' |
| 105 |
if self.parameters.get('auth_traps_enabled') is not None: |
| 106 |
fields += ',auth_traps_enabled' |
| 107 |
if 'traps_enabled' in self.parameters and self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 10, 1): |
| 108 |
fields += ',traps_enabled' |
| 109 |
record, error = rest_generic.get_one_record(self.rest_api, 'support/snmp', None, fields) |
| 110 |
if error: |
| 111 |
self.module.fail_json(msg="Error fetching SNMP configuration: %s" % to_native(error), exception=traceback.format_exc()) |
| 112 |
if record: |
| 113 |
return { |
| 114 |
'enabled': record.get('enabled'), |
| 115 |
'auth_traps_enabled': record.get('auth_traps_enabled'), |
| 116 |
'traps_enabled': record.get('traps_enabled') |
| 117 |
} |
| 118 |
return None |
| 119 |
|
| 120 |
def modify_snmp_config_rest(self, modify): |
| 121 |
"""Update cluster wide SNMP configuration""" |
| 122 |
dummy, error = rest_generic.patch_async(self.rest_api, 'support/snmp', None, modify) |
| 123 |
if error: |
| 124 |
self.module.fail_json(msg='Error modifying SNMP configuration: %s.' % to_native(error), exception=traceback.format_exc()) |
| 125 |
|
| 126 |
def apply(self): |
| 127 |
current = self.get_snmp_config_rest() |
| 128 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 129 |
|
| 130 |
if self.na_helper.changed and not self.module.check_mode: |
| 131 |
self.modify_snmp_config_rest(modify) |
| 132 |
result = netapp_utils.generate_result(changed=self.na_helper.changed, modify=modify) |
| 133 |
self.module.exit_json(**result) |
| 134 |
|
| 135 |
|
| 136 |
def main(): |
| 137 |
snmp_config = NetAppOntapSNMPConfig() |
| 138 |
snmp_config.apply() |
| 139 |
|
| 140 |
|
| 141 |
if __name__ == '__main__': |
| 142 |
main() |