| 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_l3_interface |
| 19 |
short_description: Manage routed interfaces on MS switches |
| 20 |
description: |
| 21 |
- Allows for creation, management, and visibility into routed interfaces on Meraki MS switches. |
| 22 |
notes: |
| 23 |
- Once a layer 3 interface is created, the API does not allow updating the interface and specifying C(default_gateway). |
| 24 |
deprecated: |
| 25 |
removed_in: '3.0.0' |
| 26 |
why: Updated modules released with increased functionality |
| 27 |
alternative: cisco.meraki.devices_switch_routing_interfaces |
| 28 |
options: |
| 29 |
state: |
| 30 |
description: |
| 31 |
- Create or modify an organization. |
| 32 |
type: str |
| 33 |
choices: [ present, query, absent ] |
| 34 |
default: present |
| 35 |
serial: |
| 36 |
description: |
| 37 |
- Serial number of MS switch hosting the layer 3 interface. |
| 38 |
type: str |
| 39 |
vlan_id: |
| 40 |
description: |
| 41 |
- The VLAN this routed interface is on. |
| 42 |
- VLAN must be between 1 and 4094. |
| 43 |
type: int |
| 44 |
default_gateway: |
| 45 |
description: |
| 46 |
- The next hop for any traffic that isn't going to a directly connected subnet or over a static route. |
| 47 |
- This IP address must exist in a subnet with a routed interface. |
| 48 |
type: str |
| 49 |
interface_ip: |
| 50 |
description: |
| 51 |
- The IP address this switch will use for layer 3 routing on this VLAN or subnet. |
| 52 |
- This cannot be the same as the switch's management IP. |
| 53 |
type: str |
| 54 |
interface_id: |
| 55 |
description: |
| 56 |
- Uniqiue identification number for layer 3 interface. |
| 57 |
type: str |
| 58 |
multicast_routing: |
| 59 |
description: |
| 60 |
- Enable multicast support if multicast routing between VLANs is required. |
| 61 |
type: str |
| 62 |
choices: [disabled, enabled, IGMP snooping querier] |
| 63 |
name: |
| 64 |
description: |
| 65 |
- A friendly name or description for the interface or VLAN. |
| 66 |
type: str |
| 67 |
subnet: |
| 68 |
description: |
| 69 |
- The network that this routed interface is on, in CIDR notation. |
| 70 |
type: str |
| 71 |
ospf_settings: |
| 72 |
description: |
| 73 |
- The OSPF routing settings of the interface. |
| 74 |
type: dict |
| 75 |
suboptions: |
| 76 |
cost: |
| 77 |
description: |
| 78 |
- The path cost for this interface. |
| 79 |
type: int |
| 80 |
area: |
| 81 |
description: |
| 82 |
- The OSPF area to which this interface should belong. |
| 83 |
- Can be either 'disabled' or the identifier of an existing OSPF area. |
| 84 |
type: str |
| 85 |
is_passive_enabled: |
| 86 |
description: |
| 87 |
- When enabled, OSPF will not run on the interface, but the subnet will still be advertised. |
| 88 |
type: bool |
| 89 |
author: |
| 90 |
- Kevin Breit (@kbreit) |
| 91 |
extends_documentation_fragment: cisco.meraki.meraki |
| 92 |
''' |
| 93 |
|
| 94 |
EXAMPLES = r''' |
| 95 |
- name: Query all l3 interfaces |
| 96 |
meraki_ms_l3_interface: |
| 97 |
auth_key: abc123 |
| 98 |
state: query |
| 99 |
serial: aaa-bbb-ccc |
| 100 |
|
| 101 |
- name: Query one l3 interface |
| 102 |
meraki_ms_l3_interface: |
| 103 |
auth_key: abc123 |
| 104 |
state: query |
| 105 |
serial: aaa-bbb-ccc |
| 106 |
name: Test L3 interface |
| 107 |
|
| 108 |
- name: Create l3 interface |
| 109 |
meraki_ms_l3_interface: |
| 110 |
auth_key: abc123 |
| 111 |
state: present |
| 112 |
serial: aaa-bbb-ccc |
| 113 |
name: "Test L3 interface 2" |
| 114 |
subnet: "192.168.3.0/24" |
| 115 |
interface_ip: "192.168.3.2" |
| 116 |
multicast_routing: disabled |
| 117 |
vlan_id: 11 |
| 118 |
ospf_settings: |
| 119 |
area: 0 |
| 120 |
cost: 1 |
| 121 |
is_passive_enabled: true |
| 122 |
|
| 123 |
- name: Update l3 interface |
| 124 |
meraki_ms_l3_interface: |
| 125 |
auth_key: abc123 |
| 126 |
state: present |
| 127 |
serial: aaa-bbb-ccc |
| 128 |
name: "Test L3 interface 2" |
| 129 |
subnet: "192.168.3.0/24" |
| 130 |
interface_ip: "192.168.3.2" |
| 131 |
multicast_routing: disabled |
| 132 |
vlan_id: 11 |
| 133 |
ospf_settings: |
| 134 |
area: 0 |
| 135 |
cost: 2 |
| 136 |
is_passive_enabled: true |
| 137 |
|
| 138 |
- name: Delete l3 interface |
| 139 |
meraki_ms_l3_interface: |
| 140 |
auth_key: abc123 |
| 141 |
state: absent |
| 142 |
serial: aaa-bbb-ccc |
| 143 |
interface_id: abc123344566 |
| 144 |
''' |
| 145 |
|
| 146 |
RETURN = r''' |
| 147 |
data: |
| 148 |
description: Information about the layer 3 interfaces. |
| 149 |
returned: success |
| 150 |
type: complex |
| 151 |
contains: |
| 152 |
vlan_id: |
| 153 |
description: The VLAN this routed interface is on. |
| 154 |
returned: success |
| 155 |
type: int |
| 156 |
sample: 10 |
| 157 |
default_gateway: |
| 158 |
description: The next hop for any traffic that isn't going to a directly connected subnet or over a static route. |
| 159 |
returned: success |
| 160 |
type: str |
| 161 |
sample: 192.168.2.1 |
| 162 |
interface_ip: |
| 163 |
description: The IP address this switch will use for layer 3 routing on this VLAN or subnet. |
| 164 |
returned: success |
| 165 |
type: str |
| 166 |
sample: 192.168.2.2 |
| 167 |
interface_id: |
| 168 |
description: Uniqiue identification number for layer 3 interface. |
| 169 |
returned: success |
| 170 |
type: str |
| 171 |
sample: 62487444811111120 |
| 172 |
multicast_routing: |
| 173 |
description: Enable multicast support if multicast routing between VLANs is required. |
| 174 |
returned: success |
| 175 |
type: str |
| 176 |
sample: disabled |
| 177 |
name: |
| 178 |
description: A friendly name or description for the interface or VLAN. |
| 179 |
returned: success |
| 180 |
type: str |
| 181 |
sample: L3 interface |
| 182 |
subnet: |
| 183 |
description: The network that this routed interface is on, in CIDR notation. |
| 184 |
returned: success |
| 185 |
type: str |
| 186 |
sample: 192.168.2.0/24 |
| 187 |
ospf_settings: |
| 188 |
description: The OSPF routing settings of the interface. |
| 189 |
returned: success |
| 190 |
type: complex |
| 191 |
contains: |
| 192 |
cost: |
| 193 |
description: The path cost for this interface. |
| 194 |
returned: success |
| 195 |
type: int |
| 196 |
sample: 1 |
| 197 |
area: |
| 198 |
description: The OSPF area to which this interface should belong. |
| 199 |
returned: success |
| 200 |
type: str |
| 201 |
sample: 0 |
| 202 |
is_passive_enabled: |
| 203 |
description: When enabled, OSPF will not run on the interface, but the subnet will still be advertised. |
| 204 |
returned: success |
| 205 |
type: bool |
| 206 |
sample: true |
| 207 |
''' |
| 208 |
|
| 209 |
from ansible.module_utils.basic import AnsibleModule, json |
| 210 |
from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec |
| 211 |
|
| 212 |
|
| 213 |
def construct_payload(meraki): |
| 214 |
payload = {} |
| 215 |
if meraki.params['name'] is not None: |
| 216 |
payload['name'] = meraki.params['name'] |
| 217 |
if meraki.params['subnet'] is not None: |
| 218 |
payload['subnet'] = meraki.params['subnet'] |
| 219 |
if meraki.params['interface_ip'] is not None: |
| 220 |
payload['interfaceIp'] = meraki.params['interface_ip'] |
| 221 |
if meraki.params['multicast_routing'] is not None: |
| 222 |
payload['multicastRouting'] = meraki.params['multicast_routing'] |
| 223 |
if meraki.params['vlan_id'] is not None: |
| 224 |
payload['vlanId'] = meraki.params['vlan_id'] |
| 225 |
if meraki.params['default_gateway'] is not None: |
| 226 |
payload['defaultGateway'] = meraki.params['default_gateway'] |
| 227 |
if meraki.params['ospf_settings'] is not None: |
| 228 |
payload['ospfSettings'] = {} |
| 229 |
if meraki.params['ospf_settings']['area'] is not None: |
| 230 |
payload['ospfSettings']['area'] = meraki.params['ospf_settings']['area'] |
| 231 |
if meraki.params['ospf_settings']['cost'] is not None: |
| 232 |
payload['ospfSettings']['cost'] = meraki.params['ospf_settings']['cost'] |
| 233 |
if meraki.params['ospf_settings']['is_passive_enabled'] is not None: |
| 234 |
payload['ospfSettings']['isPassiveEnabled'] = meraki.params['ospf_settings']['is_passive_enabled'] |
| 235 |
return payload |
| 236 |
|
| 237 |
|
| 238 |
def get_interface_id(meraki, data, name): |
| 239 |
# meraki.fail_json(msg=data) |
| 240 |
for interface in data: |
| 241 |
if interface['name'] == name: |
| 242 |
return interface['interfaceId'] |
| 243 |
return None |
| 244 |
|
| 245 |
|
| 246 |
def get_interface(interfaces, interface_id): |
| 247 |
for interface in interfaces: |
| 248 |
if interface['interfaceId'] == interface_id: |
| 249 |
return interface |
| 250 |
return None |
| 251 |
|
| 252 |
|
| 253 |
def main(): |
| 254 |
# define the available arguments/parameters that a user can pass to |
| 255 |
# the module |
| 256 |
|
| 257 |
ospf_arg_spec = dict(area=dict(type='str'), |
| 258 |
cost=dict(type='int'), |
| 259 |
is_passive_enabled=dict(type='bool'), |
| 260 |
) |
| 261 |
|
| 262 |
argument_spec = meraki_argument_spec() |
| 263 |
argument_spec.update(state=dict(type='str', choices=['present', 'query', 'absent'], default='present'), |
| 264 |
serial=dict(type='str'), |
| 265 |
name=dict(type='str'), |
| 266 |
subnet=dict(type='str'), |
| 267 |
interface_id=dict(type='str'), |
| 268 |
interface_ip=dict(type='str'), |
| 269 |
multicast_routing=dict(type='str', choices=['disabled', 'enabled', 'IGMP snooping querier']), |
| 270 |
vlan_id=dict(type='int'), |
| 271 |
default_gateway=dict(type='str'), |
| 272 |
ospf_settings=dict(type='dict', default=None, options=ospf_arg_spec), |
| 273 |
) |
| 274 |
|
| 275 |
# the AnsibleModule object will be our abstraction working with Ansible |
| 276 |
# this includes instantiation, a couple of common attr would be the |
| 277 |
# args/params passed to the execution, as well as if the module |
| 278 |
# supports check mode |
| 279 |
module = AnsibleModule(argument_spec=argument_spec, |
| 280 |
supports_check_mode=True, |
| 281 |
) |
| 282 |
meraki = MerakiModule(module, function='ms_l3_interfaces') |
| 283 |
|
| 284 |
meraki.params['follow_redirects'] = 'all' |
| 285 |
|
| 286 |
query_urls = {'ms_l3_interfaces': '/devices/{serial}/switch/routing/interfaces'} |
| 287 |
query_one_urls = {'ms_l3_interfaces': '/devices/{serial}/switch/routing/interfaces'} |
| 288 |
create_urls = {'ms_l3_interfaces': '/devices/{serial}/switch/routing/interfaces'} |
| 289 |
update_urls = {'ms_l3_interfaces': '/devices/{serial}/switch/routing/interfaces/{interface_id}'} |
| 290 |
delete_urls = {'ms_l3_interfaces': '/devices/{serial}/switch/routing/interfaces/{interface_id}'} |
| 291 |
|
| 292 |
meraki.url_catalog['get_all'].update(query_urls) |
| 293 |
meraki.url_catalog['get_one'].update(query_one_urls) |
| 294 |
meraki.url_catalog['create'] = create_urls |
| 295 |
meraki.url_catalog['update'] = update_urls |
| 296 |
meraki.url_catalog['delete'] = delete_urls |
| 297 |
|
| 298 |
payload = None |
| 299 |
|
| 300 |
if meraki.params['vlan_id'] is not None: |
| 301 |
if meraki.params['vlan_id'] < 1 or meraki.params['vlan_id'] > 4094: |
| 302 |
meraki.fail_json(msg='vlan_id must be between 1 and 4094') |
| 303 |
|
| 304 |
interface_id = meraki.params['interface_id'] |
| 305 |
interfaces = None |
| 306 |
if interface_id is None: |
| 307 |
if meraki.params['name'] is not None: |
| 308 |
path = meraki.construct_path('get_all', custom={'serial': meraki.params['serial']}) |
| 309 |
interfaces = meraki.request(path, method='GET') |
| 310 |
interface_id = get_interface_id(meraki, interfaces, meraki.params['name']) |
| 311 |
|
| 312 |
# manipulate or modify the state as needed (this is going to be the |
| 313 |
# part where your module will do what it needs to do) |
| 314 |
|
| 315 |
if meraki.params['state'] == 'query': |
| 316 |
if interface_id is not None: # Query one interface |
| 317 |
path = meraki.construct_path('get_one', custom={'serial': meraki.params['serial'], |
| 318 |
'interface_id': interface_id}) |
| 319 |
response = meraki.request(path, method='GET') |
| 320 |
meraki.result['data'] = response |
| 321 |
meraki.exit_json(**meraki.result) |
| 322 |
else: # Query all interfaces |
| 323 |
path = meraki.construct_path('get_all', custom={'serial': meraki.params['serial']}) |
| 324 |
response = meraki.request(path, method='GET') |
| 325 |
meraki.result['data'] = response |
| 326 |
meraki.exit_json(**meraki.result) |
| 327 |
elif meraki.params['state'] == 'present': |
| 328 |
if interface_id is None: # Create a new interface |
| 329 |
payload = construct_payload(meraki) |
| 330 |
if meraki.check_mode is True: |
| 331 |
meraki.result['data'] = payload |
| 332 |
meraki.result['changed'] = True |
| 333 |
meraki.exit_json(**meraki.result) |
| 334 |
path = meraki.construct_path('create', custom={'serial': meraki.params['serial']}) |
| 335 |
response = meraki.request(path, method='POST', payload=json.dumps(payload)) |
| 336 |
meraki.result['data'] = response |
| 337 |
meraki.result['changed'] = True |
| 338 |
meraki.exit_json(**meraki.result) |
| 339 |
else: |
| 340 |
if interfaces is None: |
| 341 |
path = meraki.construct_path('get_all', custom={'serial': meraki.params['serial']}) |
| 342 |
interfaces = meraki.request(path, method='GET') |
| 343 |
payload = construct_payload(meraki) |
| 344 |
interface = get_interface(interfaces, interface_id) |
| 345 |
if meraki.is_update_required(interface, payload): |
| 346 |
if meraki.check_mode is True: |
| 347 |
interface.update(payload) |
| 348 |
meraki.result['data'] = interface |
| 349 |
meraki.result['changed'] = True |
| 350 |
meraki.exit_json(**meraki.result) |
| 351 |
path = meraki.construct_path('update', custom={'serial': meraki.params['serial'], |
| 352 |
'interface_id': interface_id}) |
| 353 |
response = meraki.request(path, method='PUT', payload=json.dumps(payload)) |
| 354 |
meraki.result['data'] = response |
| 355 |
meraki.result['changed'] = True |
| 356 |
meraki.exit_json(**meraki.result) |
| 357 |
else: |
| 358 |
meraki.result['data'] = interface |
| 359 |
meraki.exit_json(**meraki.result) |
| 360 |
elif meraki.params['state'] == 'absent': |
| 361 |
if meraki.check_mode is True: |
| 362 |
meraki.result['data'] = {} |
| 363 |
meraki.result['changed'] = True |
| 364 |
meraki.exit_json(**meraki.result) |
| 365 |
path = meraki.construct_path('delete', custom={'serial': meraki.params['serial'], |
| 366 |
'interface_id': meraki.params['interface_id']}) |
| 367 |
response = meraki.request(path, method='DELETE') |
| 368 |
meraki.result['data'] = response |
| 369 |
meraki.result['changed'] = True |
| 370 |
|
| 371 |
# in the event of a successful module execution, you will want to |
| 372 |
# simple AnsibleModule.exit_json(), passing the key/value results |
| 373 |
meraki.exit_json(**meraki.result) |
| 374 |
|
| 375 |
|
| 376 |
if __name__ == '__main__': |
| 377 |
main() |