| 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_link_aggregation |
| 19 |
short_description: Manage link aggregations on MS switches |
| 20 |
version_added: "1.2.0" |
| 21 |
description: |
| 22 |
- Allows for management of MS switch link aggregations in a Meraki environment. |
| 23 |
notes: |
| 24 |
- Switch profile ports are not supported in this module. |
| 25 |
deprecated: |
| 26 |
removed_in: '3.0.0' |
| 27 |
why: Updated modules released with increased functionality |
| 28 |
alternative: cisco.meraki.networks_switch_link_aggregations |
| 29 |
options: |
| 30 |
state: |
| 31 |
description: |
| 32 |
- Specifies whether SNMP information should be queried or modified. |
| 33 |
type: str |
| 34 |
choices: [ absent, query, present ] |
| 35 |
default: present |
| 36 |
net_name: |
| 37 |
description: |
| 38 |
- Name of network. |
| 39 |
type: str |
| 40 |
net_id: |
| 41 |
description: |
| 42 |
- ID of network. |
| 43 |
type: str |
| 44 |
lag_id: |
| 45 |
description: |
| 46 |
- ID of lag to query or modify. |
| 47 |
type: str |
| 48 |
switch_ports: |
| 49 |
description: |
| 50 |
- List of switchports to include in link aggregation. |
| 51 |
type: list |
| 52 |
elements: dict |
| 53 |
suboptions: |
| 54 |
serial: |
| 55 |
description: |
| 56 |
- Serial number of switch to own link aggregation. |
| 57 |
type: str |
| 58 |
port_id: |
| 59 |
description: |
| 60 |
- Port number which should be included in link aggregation. |
| 61 |
type: str |
| 62 |
author: |
| 63 |
- Kevin Breit (@kbreit) |
| 64 |
extends_documentation_fragment: cisco.meraki.meraki |
| 65 |
''' |
| 66 |
|
| 67 |
EXAMPLES = r''' |
| 68 |
- name: Create LAG |
| 69 |
meraki_ms_link_aggregation: |
| 70 |
auth_key: '{{ auth_key }}' |
| 71 |
state: present |
| 72 |
org_name: '{{ test_org_name }}' |
| 73 |
net_name: '{{ test_switch_net_name }}' |
| 74 |
switch_ports: |
| 75 |
- serial: '{{ serial_switch }}' |
| 76 |
port_id: "1" |
| 77 |
- serial: '{{ serial_switch }}' |
| 78 |
port_id: "2" |
| 79 |
delegate_to: localhost |
| 80 |
|
| 81 |
- name: Update LAG |
| 82 |
meraki_ms_link_aggregation: |
| 83 |
auth_key: '{{ auth_key }}' |
| 84 |
state: present |
| 85 |
org_name: '{{ test_org_name }}' |
| 86 |
net_name: '{{ test_switch_net_name }}' |
| 87 |
lag_id: '{{ lag_id }}' |
| 88 |
switch_ports: |
| 89 |
- serial: '{{ serial_switch }}' |
| 90 |
port_id: "1" |
| 91 |
- serial: '{{ serial_switch }}' |
| 92 |
port_id: "2" |
| 93 |
- serial: '{{ serial_switch }}' |
| 94 |
port_id: "3" |
| 95 |
- serial: '{{ serial_switch }}' |
| 96 |
port_id: "4" |
| 97 |
delegate_to: localhost |
| 98 |
''' |
| 99 |
|
| 100 |
RETURN = r''' |
| 101 |
data: |
| 102 |
description: List of aggregated links. |
| 103 |
returned: success |
| 104 |
type: complex |
| 105 |
contains: |
| 106 |
id: |
| 107 |
description: |
| 108 |
- ID of link aggregation. |
| 109 |
returned: success |
| 110 |
type: str |
| 111 |
sample: "MTK3M4A2ZDdfM3==" |
| 112 |
switch_ports: |
| 113 |
description: |
| 114 |
- List of switch ports to be included in link aggregation. |
| 115 |
returned: success |
| 116 |
type: complex |
| 117 |
contains: |
| 118 |
port_id: |
| 119 |
description: |
| 120 |
- Port number. |
| 121 |
type: str |
| 122 |
returned: success |
| 123 |
sample: "1" |
| 124 |
serial: |
| 125 |
description: |
| 126 |
- Serial number of switch on which port resides. |
| 127 |
type: str |
| 128 |
returned: success |
| 129 |
sample: "ABCD-1234-WXYZ" |
| 130 |
''' |
| 131 |
|
| 132 |
from ansible.module_utils.basic import AnsibleModule, json |
| 133 |
from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec |
| 134 |
|
| 135 |
|
| 136 |
def get_lags(meraki, net_id): |
| 137 |
path = meraki.construct_path('get_all', net_id=net_id) |
| 138 |
return meraki.request(path, method='GET') |
| 139 |
|
| 140 |
|
| 141 |
def is_lag_valid(lags, lag_id): |
| 142 |
for lag in lags: |
| 143 |
if lag['id'] == lag_id: |
| 144 |
return lag |
| 145 |
return False |
| 146 |
|
| 147 |
|
| 148 |
def construct_payload(meraki): |
| 149 |
payload = dict() |
| 150 |
if meraki.params['switch_ports'] is not None: |
| 151 |
payload['switchPorts'] = [] |
| 152 |
for port in meraki.params['switch_ports']: |
| 153 |
port_config = {'serial': port['serial'], |
| 154 |
'portId': port['port_id'], |
| 155 |
} |
| 156 |
payload['switchPorts'].append(port_config) |
| 157 |
# if meraki.params['switch_profile_ports'] is not None: |
| 158 |
# payload['switchProfilePorts'] = [] |
| 159 |
# for port in meraki.params['switch_profile_ports']: |
| 160 |
# port_config = {'profile': port['profile'], |
| 161 |
# 'portId': port['port_id'], |
| 162 |
# } |
| 163 |
# payload['switchProfilePorts'].append(port_config) |
| 164 |
return payload |
| 165 |
|
| 166 |
|
| 167 |
def main(): |
| 168 |
|
| 169 |
# define the available arguments/parameters that a user can pass to |
| 170 |
# the module |
| 171 |
|
| 172 |
switch_ports_args = dict(serial=dict(type='str'), |
| 173 |
port_id=dict(type='str'), |
| 174 |
) |
| 175 |
|
| 176 |
# switch_profile_ports_args = dict(profile=dict(type='str'), |
| 177 |
# port_id=dict(type='str'), |
| 178 |
# ) |
| 179 |
|
| 180 |
argument_spec = meraki_argument_spec() |
| 181 |
argument_spec.update(state=dict(type='str', choices=['absent', 'present', 'query'], default='present'), |
| 182 |
org_name=dict(type='str', aliases=['organization']), |
| 183 |
org_id=dict(type='str'), |
| 184 |
net_name=dict(type='str'), |
| 185 |
net_id=dict(type='str'), |
| 186 |
lag_id=dict(type='str'), |
| 187 |
switch_ports=dict(type='list', default=None, elements='dict', options=switch_ports_args), |
| 188 |
# switch_profile_ports=dict(type='list', default=None, elements='dict', options=switch_profile_ports_args), |
| 189 |
) |
| 190 |
|
| 191 |
# the AnsibleModule object will be our abstraction working with Ansible |
| 192 |
# this includes instantiation, a couple of common attr would be the |
| 193 |
# args/params passed to the execution, as well as if the module |
| 194 |
# supports check mode |
| 195 |
module = AnsibleModule(argument_spec=argument_spec, |
| 196 |
supports_check_mode=True, |
| 197 |
) |
| 198 |
meraki = MerakiModule(module, function='ms_link_aggregation') |
| 199 |
meraki.params['follow_redirects'] = 'all' |
| 200 |
|
| 201 |
query_urls = {'ms_link_aggregation': '/networks/{net_id}/switch/linkAggregations'} |
| 202 |
create_url = {'ms_link_aggregation': '/networks/{net_id}/switch/linkAggregations'} |
| 203 |
update_url = {'ms_link_aggregation': '/networks/{net_id}/switch/linkAggregations/{lag_id}'} |
| 204 |
delete_url = {'ms_link_aggregation': '/networks/{net_id}/switch/linkAggregations/{lag_id}'} |
| 205 |
|
| 206 |
meraki.url_catalog['get_all'].update(query_urls) |
| 207 |
meraki.url_catalog['create'] = create_url |
| 208 |
meraki.url_catalog['update'] = update_url |
| 209 |
meraki.url_catalog['delete'] = delete_url |
| 210 |
|
| 211 |
payload = None |
| 212 |
|
| 213 |
# execute checks for argument completeness |
| 214 |
# manipulate or modify the state as needed (this is going to be the |
| 215 |
# part where your module will do what it needs to do) |
| 216 |
org_id = meraki.params['org_id'] |
| 217 |
if org_id is None: |
| 218 |
org_id = meraki.get_org_id(meraki.params['org_name']) |
| 219 |
net_id = meraki.params['net_id'] |
| 220 |
if net_id is None: |
| 221 |
nets = meraki.get_nets(org_id=org_id) |
| 222 |
net_id = meraki.get_net_id(org_id, meraki.params['net_name'], data=nets) |
| 223 |
|
| 224 |
if meraki.params['state'] == 'query': |
| 225 |
path = meraki.construct_path('get_all', net_id=net_id) |
| 226 |
response = meraki.request(path, method='GET') |
| 227 |
meraki.result['data'] = response |
| 228 |
meraki.exit_json(**meraki.result) |
| 229 |
elif meraki.params['state'] == 'present': |
| 230 |
if meraki.params['lag_id'] is not None: # Need to update |
| 231 |
lag = is_lag_valid(get_lags(meraki, net_id), meraki.params['lag_id']) |
| 232 |
if lag is not False: # Lag ID is valid |
| 233 |
payload = construct_payload(meraki) |
| 234 |
if meraki.is_update_required(lag, payload) is True: |
| 235 |
path = meraki.construct_path('update', net_id=net_id, custom={'lag_id': meraki.params['lag_id']}) |
| 236 |
response = meraki.request(path, method='PUT', payload=json.dumps(payload)) |
| 237 |
meraki.result['changed'] = True |
| 238 |
meraki.result['data'] = response |
| 239 |
else: |
| 240 |
meraki.result['data'] = lag |
| 241 |
else: |
| 242 |
meraki.fail_json("Provided lag_id is not valid.") |
| 243 |
else: |
| 244 |
path = meraki.construct_path('create', net_id=net_id) |
| 245 |
payload = construct_payload(meraki) |
| 246 |
response = meraki.request(path, method='POST', payload=json.dumps(payload)) |
| 247 |
meraki.result['changed'] = True |
| 248 |
meraki.result['data'] = response |
| 249 |
meraki.exit_json(**meraki.result) |
| 250 |
elif meraki.params['state'] == 'absent': |
| 251 |
path = meraki.construct_path('delete', net_id=net_id, custom={'lag_id': meraki.params['lag_id']}) |
| 252 |
response = meraki.request(path, method='DELETE') |
| 253 |
meraki.result['data'] = {} |
| 254 |
meraki.result['changed'] = True |
| 255 |
|
| 256 |
# in the event of a successful module execution, you will want to |
| 257 |
# simple AnsibleModule.exit_json(), passing the key/value results |
| 258 |
meraki.exit_json(**meraki.result) |
| 259 |
|
| 260 |
|
| 261 |
if __name__ == '__main__': |
| 262 |
main() |