| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2020, 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_ms_ospf |
| 19 |
short_description: Manage OSPF configuration on MS switches |
| 20 |
description: |
| 21 |
- Configure OSPF for compatible Meraki MS switches. |
| 22 |
deprecated: |
| 23 |
removed_in: '3.0.0' |
| 24 |
why: Updated modules released with increased functionality |
| 25 |
alternative: cisco.meraki.networks_switch_routing_ospf |
| 26 |
options: |
| 27 |
state: |
| 28 |
description: |
| 29 |
- Read or edit OSPF settings. |
| 30 |
type: str |
| 31 |
choices: [ present, query ] |
| 32 |
default: present |
| 33 |
net_name: |
| 34 |
description: |
| 35 |
- Name of network containing OSPF configuration. |
| 36 |
type: str |
| 37 |
aliases: [ name, network ] |
| 38 |
net_id: |
| 39 |
description: |
| 40 |
- ID of network containing OSPF configuration. |
| 41 |
type: str |
| 42 |
enabled: |
| 43 |
description: |
| 44 |
- Enable or disable OSPF on the network. |
| 45 |
type: bool |
| 46 |
hello_timer: |
| 47 |
description: |
| 48 |
- Time interval, in seconds, at which hello packets will be sent to OSPF neighbors to maintain connectivity. |
| 49 |
- Value must be between 1 and 255. |
| 50 |
- Default is 10 seconds. |
| 51 |
type: int |
| 52 |
dead_timer: |
| 53 |
description: |
| 54 |
- Time interval to determine when the peer will be declared inactive. |
| 55 |
- Value must be between 1 and 65535. |
| 56 |
type: int |
| 57 |
md5_authentication_enabled: |
| 58 |
description: |
| 59 |
- Whether to enable or disable MD5 authentication. |
| 60 |
type: bool |
| 61 |
md5_authentication_key: |
| 62 |
description: |
| 63 |
- MD5 authentication credentials. |
| 64 |
type: dict |
| 65 |
suboptions: |
| 66 |
id: |
| 67 |
description: |
| 68 |
- MD5 authentication key index. |
| 69 |
- Must be between 1 and 255. |
| 70 |
type: str |
| 71 |
passphrase: |
| 72 |
description: |
| 73 |
- Plain text authentication passphrase |
| 74 |
type: str |
| 75 |
areas: |
| 76 |
description: |
| 77 |
- List of areas in OSPF network. |
| 78 |
type: list |
| 79 |
elements: dict |
| 80 |
suboptions: |
| 81 |
area_id: |
| 82 |
description: |
| 83 |
- OSPF area ID |
| 84 |
type: int |
| 85 |
aliases: [ id ] |
| 86 |
area_name: |
| 87 |
description: |
| 88 |
- Descriptive name of OSPF area. |
| 89 |
type: str |
| 90 |
aliases: [ name ] |
| 91 |
area_type: |
| 92 |
description: |
| 93 |
- OSPF area type. |
| 94 |
choices: [normal, stub, nssa] |
| 95 |
type: str |
| 96 |
aliases: [ type ] |
| 97 |
author: |
| 98 |
- Kevin Breit (@kbreit) |
| 99 |
extends_documentation_fragment: cisco.meraki.meraki |
| 100 |
''' |
| 101 |
|
| 102 |
EXAMPLES = r''' |
| 103 |
- name: Query OSPF settings |
| 104 |
meraki_ms_ospf: |
| 105 |
auth_key: abc123 |
| 106 |
org_name: YourOrg |
| 107 |
net_name: YourNet |
| 108 |
state: query |
| 109 |
delegate_to: localhost |
| 110 |
|
| 111 |
- name: Enable OSPF with check mode |
| 112 |
meraki_ms_ospf: |
| 113 |
auth_key: abc123 |
| 114 |
org_name: YourOrg |
| 115 |
net_name: YourNet |
| 116 |
state: present |
| 117 |
enabled: true |
| 118 |
hello_timer: 20 |
| 119 |
dead_timer: 60 |
| 120 |
areas: |
| 121 |
area_id: 0 |
| 122 |
area_name: Backbone |
| 123 |
area_type: normal |
| 124 |
md5_authentication_enabled: false |
| 125 |
''' |
| 126 |
|
| 127 |
RETURN = r''' |
| 128 |
data: |
| 129 |
description: Information about queried object. |
| 130 |
returned: success |
| 131 |
type: complex |
| 132 |
contains: |
| 133 |
enabled: |
| 134 |
description: |
| 135 |
- Enable or disable OSPF on the network. |
| 136 |
type: bool |
| 137 |
hello_timer_in_seconds: |
| 138 |
description: |
| 139 |
- Time interval, in seconds, at which hello packets will be sent to OSPF neighbors to maintain connectivity. |
| 140 |
type: int |
| 141 |
dead_timer_in_seconds: |
| 142 |
description: |
| 143 |
- Time interval to determine when the peer will be declared inactive. |
| 144 |
type: int |
| 145 |
areas: |
| 146 |
description: |
| 147 |
- List of areas in OSPF network. |
| 148 |
type: complex |
| 149 |
contains: |
| 150 |
area_id: |
| 151 |
description: |
| 152 |
- OSPF area ID |
| 153 |
type: int |
| 154 |
area_name: |
| 155 |
description: |
| 156 |
- Descriptive name of OSPF area. |
| 157 |
type: str |
| 158 |
area_type: |
| 159 |
description: |
| 160 |
- OSPF area type. |
| 161 |
type: str |
| 162 |
md5_authentication_enabled: |
| 163 |
description: |
| 164 |
- Whether to enable or disable MD5 authentication. |
| 165 |
type: bool |
| 166 |
md5_authentication_key: |
| 167 |
description: |
| 168 |
- MD5 authentication credentials. |
| 169 |
type: complex |
| 170 |
contains: |
| 171 |
id: |
| 172 |
description: |
| 173 |
- MD5 key index. |
| 174 |
type: int |
| 175 |
passphrase: |
| 176 |
description: |
| 177 |
- Passphrase for MD5 key. |
| 178 |
type: str |
| 179 |
''' |
| 180 |
|
| 181 |
from ansible.module_utils.basic import AnsibleModule, json |
| 182 |
from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec |
| 183 |
|
| 184 |
|
| 185 |
def construct_payload(meraki): |
| 186 |
payload_key_mapping = {'enabled': 'enabled', |
| 187 |
'hello_timer': 'helloTimerInSeconds', |
| 188 |
'dead_timer': 'deadTimerInSeconds', |
| 189 |
'areas': 'areas', |
| 190 |
'area_id': 'areaId', |
| 191 |
'area_name': 'areaName', |
| 192 |
'area_type': 'areaType', |
| 193 |
'md5_authentication_enabled': 'md5AuthenticationEnabled', |
| 194 |
'md5_authentication_key': 'md5AuthenticationKey', |
| 195 |
'id': 'id', |
| 196 |
'passphrase': 'passphrase', |
| 197 |
} |
| 198 |
payload = {} |
| 199 |
|
| 200 |
# This may need to be reworked to avoid overwiting |
| 201 |
for snake, camel in payload_key_mapping.items(): |
| 202 |
try: |
| 203 |
if meraki.params[snake] is not None: |
| 204 |
payload[camel] = meraki.params[snake] |
| 205 |
if snake == 'areas': |
| 206 |
if meraki.params['areas'] is not None and len(meraki.params['areas']) > 0: |
| 207 |
payload['areas'] = [] |
| 208 |
for area in meraki.params['areas']: |
| 209 |
area_settings = {'areaName': area['area_name'], |
| 210 |
'areaId': area['area_id'], |
| 211 |
'areaType': area['area_type'], |
| 212 |
} |
| 213 |
payload['areas'].append(area_settings) |
| 214 |
# TODO: Does this code below have a purpose? |
| 215 |
# elif snake == 'md5_authentication_key': |
| 216 |
# if meraki.params['md5_authentication_key'] is not None: |
| 217 |
# md5_settings = {'id': meraki.params['md5_authentication_key']['id'], |
| 218 |
# 'passphrase': meraki.params['md5_authentication_key']['passphrase'], |
| 219 |
# } |
| 220 |
except KeyError: |
| 221 |
pass |
| 222 |
|
| 223 |
return payload |
| 224 |
|
| 225 |
|
| 226 |
def main(): |
| 227 |
# define the available arguments/parameters that a user can pass to |
| 228 |
# the module |
| 229 |
|
| 230 |
areas_arg_spec = dict(area_id=dict(type='int', aliases=['id']), |
| 231 |
area_name=dict(type='str', aliases=['name']), |
| 232 |
area_type=dict(type='str', aliases=['type'], choices=['normal', 'stub', 'nssa']), |
| 233 |
) |
| 234 |
|
| 235 |
md5_auth_arg_spec = dict(id=dict(type='str'), |
| 236 |
passphrase=dict(type='str', no_log=True), |
| 237 |
) |
| 238 |
|
| 239 |
argument_spec = meraki_argument_spec() |
| 240 |
argument_spec.update(state=dict(type='str', choices=['present', 'query'], default='present'), |
| 241 |
net_id=dict(type='str'), |
| 242 |
net_name=dict(type='str', aliases=['name', 'network']), |
| 243 |
enabled=dict(type='bool'), |
| 244 |
hello_timer=dict(type='int'), |
| 245 |
dead_timer=dict(type='int'), |
| 246 |
areas=dict(type='list', default=None, elements='dict', options=areas_arg_spec), |
| 247 |
md5_authentication_enabled=dict(type='bool'), |
| 248 |
md5_authentication_key=dict(type='dict', default=None, options=md5_auth_arg_spec, no_log=True), |
| 249 |
) |
| 250 |
|
| 251 |
# the AnsibleModule object will be our abstraction working with Ansible |
| 252 |
# this includes instantiation, a couple of common attr would be the |
| 253 |
# args/params passed to the execution, as well as if the module |
| 254 |
# supports check mode |
| 255 |
module = AnsibleModule(argument_spec=argument_spec, |
| 256 |
supports_check_mode=True, |
| 257 |
) |
| 258 |
meraki = MerakiModule(module, function='ms_ospf') |
| 259 |
|
| 260 |
meraki.params['follow_redirects'] = 'all' |
| 261 |
|
| 262 |
query_urls = {'ms_ospf': '/networks/{net_id}/switch/routing/ospf'} |
| 263 |
update_urls = {'ms_ospf': '/networks/{net_id}/switch/routing/ospf'} |
| 264 |
|
| 265 |
meraki.url_catalog['get_all'].update(query_urls) |
| 266 |
meraki.url_catalog['update'] = update_urls |
| 267 |
|
| 268 |
payload = None |
| 269 |
|
| 270 |
# execute checks for argument completeness |
| 271 |
|
| 272 |
if meraki.params['dead_timer'] is not None: |
| 273 |
if meraki.params['dead_timer'] < 1 or meraki.params['dead_timer'] > 65535: |
| 274 |
meraki.fail_json(msg='dead_timer must be between 1 and 65535') |
| 275 |
if meraki.params['hello_timer'] is not None: |
| 276 |
if meraki.params['hello_timer'] < 1 or meraki.params['hello_timer'] > 255: |
| 277 |
meraki.fail_json(msg='hello_timer must be between 1 and 65535') |
| 278 |
if meraki.params['md5_authentication_enabled'] is False: |
| 279 |
if meraki.params['md5_authentication_key'] is not None: |
| 280 |
meraki.fail_json(msg='md5_authentication_key must not be configured when md5_authentication_enabled is false') |
| 281 |
|
| 282 |
# manipulate or modify the state as needed (this is going to be the |
| 283 |
# part where your module will do what it needs to do) |
| 284 |
|
| 285 |
org_id = meraki.params['org_id'] |
| 286 |
if not org_id: |
| 287 |
org_id = meraki.get_org_id(meraki.params['org_name']) |
| 288 |
net_id = meraki.params['net_id'] |
| 289 |
if net_id is None and meraki.params['net_name']: |
| 290 |
nets = meraki.get_nets(org_id=org_id) |
| 291 |
net_id = meraki.get_net_id(net_name=meraki.params['net_name'], data=nets) |
| 292 |
if meraki.params['state'] == 'query': |
| 293 |
path = meraki.construct_path('get_all', net_id=net_id) |
| 294 |
response = meraki.request(path, method='GET') |
| 295 |
meraki.result['data'] = response |
| 296 |
meraki.exit_json(**meraki.result) |
| 297 |
elif meraki.params['state'] == 'present': |
| 298 |
original = meraki.request(meraki.construct_path('get_all', net_id=net_id), method='GET') |
| 299 |
payload = construct_payload(meraki) |
| 300 |
if meraki.is_update_required(original, payload) is True: |
| 301 |
if meraki.check_mode is True: |
| 302 |
meraki.result['data'] = payload |
| 303 |
meraki.result['changed'] = True |
| 304 |
meraki.exit_json(**meraki.result) |
| 305 |
path = meraki.construct_path('update', net_id=net_id) |
| 306 |
response = meraki.request(path, method='PUT', payload=json.dumps(payload)) |
| 307 |
if 'md5_authentication_key' in response: |
| 308 |
response['md5_authentication_key']['passphrase'] = 'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER' |
| 309 |
meraki.result['data'] = response |
| 310 |
meraki.result['changed'] = True |
| 311 |
meraki.exit_json(**meraki.result) |
| 312 |
else: |
| 313 |
if 'md5_authentication_key' in original: |
| 314 |
original['md5_authentication_key']['passphrase'] = 'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER' |
| 315 |
meraki.result['data'] = original |
| 316 |
meraki.exit_json(**meraki.result) |
| 317 |
|
| 318 |
# in the event of a successful module execution, you will want to |
| 319 |
# simple AnsibleModule.exit_json(), passing the key/value results |
| 320 |
meraki.exit_json(**meraki.result) |
| 321 |
|
| 322 |
|
| 323 |
if __name__ == '__main__': |
| 324 |
main() |