Debian Codegraph @

ansible_collections/cisco/meraki/plugins/modules/meraki_snmp.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Kevin Breit (@kbreit) <[email protected]>
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 ANSIBLE_METADATA = {
11 'metadata_version': '1.1',
12 "status": ['deprecated'],
13 'supported_by': 'community'
14 }
15
16 DOCUMENTATION = r'''
17 ---
18 module: meraki_snmp
19 short_description: Manage organizations in the Meraki cloud
20 description:
21 - Allows for management of SNMP settings for Meraki.
22 deprecated:
23 removed_in: '3.0.0'
24 why: Updated modules released with increased functionality
25 alternative: cisco.meraki.networks_snmp
26 options:
27 state:
28 description:
29 - Specifies whether SNMP information should be queried or modified.
30 choices: ['query', 'present']
31 default: present
32 type: str
33 v2c_enabled:
34 description:
35 - Specifies whether SNMPv2c is enabled.
36 type: bool
37 v3_enabled:
38 description:
39 - Specifies whether SNMPv3 is enabled.
40 type: bool
41 v3_auth_mode:
42 description:
43 - Sets authentication mode for SNMPv3.
44 choices: ['MD5', 'SHA']
45 type: str
46 v3_auth_pass:
47 description:
48 - Authentication password for SNMPv3.
49 - Must be at least 8 characters long.
50 type: str
51 v3_priv_mode:
52 description:
53 - Specifies privacy mode for SNMPv3.
54 choices: ['DES', 'AES128']
55 type: str
56 v3_priv_pass:
57 description:
58 - Privacy password for SNMPv3.
59 - Must be at least 8 characters long.
60 type: str
61 peer_ips:
62 description:
63 - List of IP addresses which can perform SNMP queries.
64 type: list
65 elements: str
66 net_name:
67 description:
68 - Name of network.
69 type: str
70 net_id:
71 description:
72 - ID of network.
73 type: str
74 access:
75 description:
76 - Type of SNMP access.
77 choices: [community, none, users]
78 type: str
79 community_string:
80 description:
81 - SNMP community string.
82 - Only relevant if C(access) is set to C(community).
83 type: str
84 users:
85 description:
86 - Information about users with access to SNMP.
87 - Only relevant if C(access) is set to C(users).
88 type: list
89 elements: dict
90 suboptions:
91 username:
92 description: Username of user with access.
93 type: str
94 passphrase:
95 description: Passphrase for user SNMP access.
96 type: str
97 author:
98 - Kevin Breit (@kbreit)
99 extends_documentation_fragment: cisco.meraki.meraki
100 '''
101
102 EXAMPLES = r'''
103 - name: Query SNMP values
104 meraki_snmp:
105 auth_key: abc12345
106 org_name: YourOrg
107 state: query
108 delegate_to: localhost
109
110 - name: Enable SNMPv2
111 meraki_snmp:
112 auth_key: abc12345
113 org_name: YourOrg
114 state: present
115 v2c_enabled: true
116 delegate_to: localhost
117
118 - name: Disable SNMPv2
119 meraki_snmp:
120 auth_key: abc12345
121 org_name: YourOrg
122 state: present
123 v2c_enabled: false
124 delegate_to: localhost
125
126 - name: Enable SNMPv3
127 meraki_snmp:
128 auth_key: abc12345
129 org_name: YourOrg
130 state: present
131 v3_enabled: true
132 v3_auth_mode: SHA
133 v3_auth_pass: ansiblepass
134 v3_priv_mode: AES128
135 v3_priv_pass: ansiblepass
136 peer_ips: 192.0.1.1;192.0.1.2
137 delegate_to: localhost
138
139 - name: Set network access type to community string
140 meraki_snmp:
141 auth_key: abc1235
142 org_name: YourOrg
143 net_name: YourNet
144 state: present
145 access: community
146 community_string: abc123
147 delegate_to: localhost
148
149 - name: Set network access type to username
150 meraki_snmp:
151 auth_key: abc1235
152 org_name: YourOrg
153 net_name: YourNet
154 state: present
155 access: users
156 users:
157 - username: ansibleuser
158 passphrase: ansiblepass
159 delegate_to: localhost
160 '''
161
162 RETURN = r'''
163 data:
164 description: Information about SNMP settings.
165 type: complex
166 returned: always
167 contains:
168 hostname:
169 description: Hostname of SNMP server.
170 returned: success and no network specified.
171 type: str
172 sample: n1.meraki.com
173 peer_ips:
174 description: Semi-colon delimited list of IPs which can poll SNMP information.
175 returned: success and no network specified.
176 type: str
177 sample: 192.0.1.1
178 port:
179 description: Port number of SNMP.
180 returned: success and no network specified.
181 type: str
182 sample: 16100
183 v2c_enabled:
184 description: Shows enabled state of SNMPv2c
185 returned: success and no network specified.
186 type: bool
187 sample: true
188 v3_enabled:
189 description: Shows enabled state of SNMPv3
190 returned: success and no network specified.
191 type: bool
192 sample: true
193 v3_auth_mode:
194 description: The SNMP version 3 authentication mode either MD5 or SHA.
195 returned: success and no network specified.
196 type: str
197 sample: SHA
198 v3_priv_mode:
199 description: The SNMP version 3 privacy mode DES or AES128.
200 returned: success and no network specified.
201 type: str
202 sample: AES128
203 v2_community_string:
204 description: Automatically generated community string for SNMPv2c.
205 returned: When SNMPv2c is enabled and no network specified.
206 type: str
207 sample: o/8zd-JaSb
208 v3_user:
209 description: Automatically generated username for SNMPv3.
210 returned: When SNMPv3c is enabled and no network specified.
211 type: str
212 sample: o/8zd-JaSb
213 access:
214 description: Type of SNMP access.
215 type: str
216 returned: success, when network specified
217 community_string:
218 description: SNMP community string. Only relevant if C(access) is set to C(community).
219 type: str
220 returned: success, when network specified
221 users:
222 description: Information about users with access to SNMP. Only relevant if C(access) is set to C(users).
223 type: complex
224 contains:
225 username:
226 description: Username of user with access.
227 type: str
228 returned: success, when network specified
229 passphrase:
230 description: Passphrase for user SNMP access.
231 type: str
232 returned: success, when network specified
233 '''
234
235 from ansible.module_utils.basic import AnsibleModule, json
236 from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict
237 from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec
238
239
240 def get_snmp(meraki, org_id):
241 path = meraki.construct_path('get_all', org_id=org_id)
242 r = meraki.request(path,
243 method='GET',
244 )
245 if meraki.status == 200:
246 return r
247
248
249 def set_snmp(meraki, org_id):
250 payload = dict()
251 if meraki.params['v2c_enabled'] is not None:
252 payload = {'v2cEnabled': meraki.params['v2c_enabled'],
253 }
254 if meraki.params['v3_enabled'] is True:
255 if len(meraki.params['v3_auth_pass']) < 8 or len(meraki.params['v3_priv_pass']) < 8:
256 meraki.fail_json(msg='v3_auth_pass and v3_priv_pass must both be at least 8 characters long.')
257 if meraki.params['v3_auth_mode'] is None or \
258 meraki.params['v3_auth_pass'] is None or \
259 meraki.params['v3_priv_mode'] is None or \
260 meraki.params['v3_priv_pass'] is None:
261 meraki.fail_json(msg='v3_auth_mode, v3_auth_pass, v3_priv_mode, and v3_auth_pass are required')
262 payload = {'v3Enabled': meraki.params['v3_enabled'],
263 'v3AuthMode': meraki.params['v3_auth_mode'].upper(),
264 'v3AuthPass': meraki.params['v3_auth_pass'],
265 'v3PrivMode': meraki.params['v3_priv_mode'].upper(),
266 'v3PrivPass': meraki.params['v3_priv_pass'],
267 }
268 if meraki.params['peer_ips'] is not None:
269 payload['peerIps'] = meraki.params['peer_ips']
270 elif meraki.params['v3_enabled'] is False:
271 payload = {'v3Enabled': False}
272 full_compare = snake_dict_to_camel_dict(payload)
273 path = meraki.construct_path('create', org_id=org_id)
274 snmp = get_snmp(meraki, org_id)
275 ignored_parameters = ['v3AuthPass', 'v3PrivPass', 'hostname', 'port', 'v2CommunityString', 'v3User']
276 if meraki.is_update_required(snmp, full_compare, optional_ignore=ignored_parameters):
277 if meraki.module.check_mode is True:
278 meraki.generate_diff(snmp, full_compare)
279 snmp.update(payload)
280 meraki.result['data'] = snmp
281 meraki.result['changed'] = True
282 meraki.exit_json(**meraki.result)
283 r = meraki.request(path,
284 method='PUT',
285 payload=json.dumps(payload))
286 if meraki.status == 200:
287 meraki.generate_diff(snmp, r)
288 meraki.result['changed'] = True
289 return r
290 else:
291 return snmp
292
293
294 def main():
295
296 # define the available arguments/parameters that a user can pass to
297 # the module
298 user_arg_spec = dict(username=dict(type='str'),
299 passphrase=dict(type='str', no_log=True),
300 )
301
302 argument_spec = meraki_argument_spec()
303 argument_spec.update(state=dict(type='str', choices=['present', 'query'], default='present'),
304 v2c_enabled=dict(type='bool'),
305 v3_enabled=dict(type='bool'),
306 v3_auth_mode=dict(type='str', choices=['SHA', 'MD5']),
307 v3_auth_pass=dict(type='str', no_log=True),
308 v3_priv_mode=dict(type='str', choices=['DES', 'AES128']),
309 v3_priv_pass=dict(type='str', no_log=True),
310 peer_ips=dict(type='list', default=None, elements='str'),
311 access=dict(type='str', choices=['none', 'community', 'users']),
312 community_string=dict(type='str', no_log=True),
313 users=dict(type='list', default=None, elements='dict', options=user_arg_spec),
314 net_name=dict(type='str'),
315 net_id=dict(type='str'),
316 )
317
318 # the AnsibleModule object will be our abstraction working with Ansible
319 # this includes instantiation, a couple of common attr would be the
320 # args/params passed to the execution, as well as if the module
321 # supports check mode
322 module = AnsibleModule(argument_spec=argument_spec,
323 supports_check_mode=True,
324 )
325 meraki = MerakiModule(module, function='snmp')
326 meraki.params['follow_redirects'] = 'all'
327
328 query_urls = {'snmp': '/organizations/{org_id}/snmp'}
329 query_net_urls = {'snmp': '/networks/{net_id}/snmp'}
330 update_urls = {'snmp': '/organizations/{org_id}/snmp'}
331 update_net_urls = {'snmp': '/networks/{net_id}/snmp'}
332
333 meraki.url_catalog['get_all'].update(query_urls)
334 meraki.url_catalog['query_net_all'] = query_net_urls
335 meraki.url_catalog['create'] = update_urls
336 meraki.url_catalog['create_net'] = update_net_urls
337
338 payload = None
339
340 if not meraki.params['org_name'] and not meraki.params['org_id']:
341 meraki.fail_json(msg='org_name or org_id is required')
342
343 org_id = meraki.params['org_id']
344 if org_id is None:
345 org_id = meraki.get_org_id(meraki.params['org_name'])
346 net_id = meraki.params['net_id']
347 if net_id is None and meraki.params['net_name']:
348 nets = meraki.get_nets(org_id=org_id)
349 net_id = meraki.get_net_id(org_id, meraki.params['net_name'], data=nets)
350
351 if meraki.params['state'] == 'present':
352 if net_id is not None:
353 payload = {'access': meraki.params['access']}
354 if meraki.params['community_string'] is not None:
355 payload['communityString'] = meraki.params['community_string']
356 elif meraki.params['users'] is not None:
357 payload['users'] = meraki.params['users']
358
359 if meraki.params['state'] == 'query':
360 if net_id is None:
361 meraki.result['data'] = get_snmp(meraki, org_id)
362 else:
363 path = meraki.construct_path('query_net_all', net_id=net_id)
364 response = meraki.request(path, method='GET')
365 if meraki.status == 200:
366 meraki.result['data'] = response
367 elif meraki.params['state'] == 'present':
368 if net_id is None:
369 meraki.result['data'] = set_snmp(meraki, org_id)
370 else:
371 path = meraki.construct_path('query_net_all', net_id=net_id)
372 original = meraki.request(path, method='GET')
373 if meraki.is_update_required(original, payload):
374 path = meraki.construct_path('create_net', net_id=net_id)
375 response = meraki.request(path, method='PUT', payload=json.dumps(payload))
376 if meraki.status == 200:
377 if response['access'] == 'none':
378 meraki.result['data'] = {}
379 else:
380 meraki.result['data'] = response
381 meraki.result['changed'] = True
382 else:
383 meraki.result['data'] = original
384
385 # in the event of a successful module execution, you will want to
386 # simple AnsibleModule.exit_json(), passing the key/value results
387 meraki.exit_json(**meraki.result)
388
389
390 if __name__ == '__main__':
391 main()