| 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_device |
| 19 |
short_description: Manage devices in the Meraki cloud |
| 20 |
description: |
| 21 |
- Visibility into devices associated to a Meraki environment. |
| 22 |
notes: |
| 23 |
- This module does not support claiming of devices or licenses into a Meraki organization. |
| 24 |
- More information about the Meraki API can be found at U(https://dashboard.meraki.com/api_docs). |
| 25 |
- Some of the options are likely only used for developers within Meraki. |
| 26 |
deprecated: |
| 27 |
removed_in: '3.0.0' |
| 28 |
why: Updated modules released with increased functionality |
| 29 |
alternative: cisco.meraki.networks_devices_claim, cisco.meraki.networks_devices_remove and cisco.meraki.networks |
| 30 |
options: |
| 31 |
state: |
| 32 |
description: |
| 33 |
- Query an organization. |
| 34 |
choices: [absent, present, query] |
| 35 |
default: query |
| 36 |
type: str |
| 37 |
net_name: |
| 38 |
description: |
| 39 |
- Name of a network. |
| 40 |
aliases: [network] |
| 41 |
type: str |
| 42 |
net_id: |
| 43 |
description: |
| 44 |
- ID of a network. |
| 45 |
type: str |
| 46 |
serial: |
| 47 |
description: |
| 48 |
- Serial number of a device to query. |
| 49 |
type: str |
| 50 |
hostname: |
| 51 |
description: |
| 52 |
- Hostname of network device to search for. |
| 53 |
aliases: [name] |
| 54 |
type: str |
| 55 |
model: |
| 56 |
description: |
| 57 |
- Model of network device to search for. |
| 58 |
type: str |
| 59 |
tags: |
| 60 |
description: |
| 61 |
- Space delimited list of tags to assign to device. |
| 62 |
type: list |
| 63 |
elements: str |
| 64 |
lat: |
| 65 |
description: |
| 66 |
- Latitude of device's geographic location. |
| 67 |
- Use negative number for southern hemisphere. |
| 68 |
aliases: [latitude] |
| 69 |
type: float |
| 70 |
lng: |
| 71 |
description: |
| 72 |
- Longitude of device's geographic location. |
| 73 |
- Use negative number for western hemisphere. |
| 74 |
aliases: [longitude] |
| 75 |
type: float |
| 76 |
address: |
| 77 |
description: |
| 78 |
- Postal address of device's location. |
| 79 |
type: str |
| 80 |
move_map_marker: |
| 81 |
description: |
| 82 |
- Whether or not to set the latitude and longitude of a device based on the new address. |
| 83 |
- Only applies when C(lat) and C(lng) are not specified. |
| 84 |
type: bool |
| 85 |
lldp_cdp_timespan: |
| 86 |
description: |
| 87 |
- Timespan, in seconds, used to query LLDP and CDP information. |
| 88 |
- Must be less than 1 month. |
| 89 |
type: int |
| 90 |
note: |
| 91 |
description: |
| 92 |
- Informational notes about a device. |
| 93 |
- Limited to 255 characters. |
| 94 |
type: str |
| 95 |
query: |
| 96 |
description: |
| 97 |
- Specifies what information should be queried. |
| 98 |
type: str |
| 99 |
choices: [lldp_cdp, uplink] |
| 100 |
|
| 101 |
|
| 102 |
author: |
| 103 |
- Kevin Breit (@kbreit) |
| 104 |
extends_documentation_fragment: cisco.meraki.meraki |
| 105 |
''' |
| 106 |
|
| 107 |
EXAMPLES = r''' |
| 108 |
- name: Query all devices in an organization. |
| 109 |
meraki_device: |
| 110 |
auth_key: abc12345 |
| 111 |
org_name: YourOrg |
| 112 |
state: query |
| 113 |
delegate_to: localhost |
| 114 |
|
| 115 |
- name: Query all devices in a network. |
| 116 |
meraki_device: |
| 117 |
auth_key: abc12345 |
| 118 |
org_name: YourOrg |
| 119 |
net_name: YourNet |
| 120 |
state: query |
| 121 |
delegate_to: localhost |
| 122 |
|
| 123 |
- name: Query a device by serial number. |
| 124 |
meraki_device: |
| 125 |
auth_key: abc12345 |
| 126 |
org_name: YourOrg |
| 127 |
net_name: YourNet |
| 128 |
serial: ABC-123 |
| 129 |
state: query |
| 130 |
delegate_to: localhost |
| 131 |
|
| 132 |
- name: Lookup uplink information about a device. |
| 133 |
meraki_device: |
| 134 |
auth_key: abc12345 |
| 135 |
org_name: YourOrg |
| 136 |
net_name: YourNet |
| 137 |
serial_uplink: ABC-123 |
| 138 |
state: query |
| 139 |
delegate_to: localhost |
| 140 |
|
| 141 |
- name: Lookup LLDP and CDP information about devices connected to specified device. |
| 142 |
meraki_device: |
| 143 |
auth_key: abc12345 |
| 144 |
org_name: YourOrg |
| 145 |
net_name: YourNet |
| 146 |
serial_lldp_cdp: ABC-123 |
| 147 |
state: query |
| 148 |
delegate_to: localhost |
| 149 |
|
| 150 |
- name: Lookup a device by hostname. |
| 151 |
meraki_device: |
| 152 |
auth_key: abc12345 |
| 153 |
org_name: YourOrg |
| 154 |
net_name: YourNet |
| 155 |
hostname: main-switch |
| 156 |
state: query |
| 157 |
delegate_to: localhost |
| 158 |
|
| 159 |
- name: Query all devices of a specific model. |
| 160 |
meraki_device: |
| 161 |
auth_key: abc123 |
| 162 |
org_name: YourOrg |
| 163 |
net_name: YourNet |
| 164 |
model: MR26 |
| 165 |
state: query |
| 166 |
delegate_to: localhost |
| 167 |
|
| 168 |
- name: Update information about a device. |
| 169 |
meraki_device: |
| 170 |
auth_key: abc123 |
| 171 |
org_name: YourOrg |
| 172 |
net_name: YourNet |
| 173 |
state: present |
| 174 |
serial: '{{ serial }}' |
| 175 |
name: mr26 |
| 176 |
address: 1060 W. Addison St., Chicago, IL |
| 177 |
lat: 41.948038 |
| 178 |
lng: -87.65568 |
| 179 |
tags: recently-added |
| 180 |
delegate_to: localhost |
| 181 |
|
| 182 |
- name: Claim a device into a network. |
| 183 |
meraki_device: |
| 184 |
auth_key: abc123 |
| 185 |
org_name: YourOrg |
| 186 |
net_name: YourNet |
| 187 |
serial: ABC-123 |
| 188 |
state: present |
| 189 |
delegate_to: localhost |
| 190 |
|
| 191 |
- name: Remove a device from a network. |
| 192 |
meraki_device: |
| 193 |
auth_key: abc123 |
| 194 |
org_name: YourOrg |
| 195 |
net_name: YourNet |
| 196 |
serial: ABC-123 |
| 197 |
state: absent |
| 198 |
delegate_to: localhost |
| 199 |
''' |
| 200 |
|
| 201 |
RETURN = r''' |
| 202 |
response: |
| 203 |
description: Data returned from Meraki dashboard. |
| 204 |
type: dict |
| 205 |
returned: info |
| 206 |
''' |
| 207 |
|
| 208 |
from ansible.module_utils.basic import AnsibleModule, json |
| 209 |
from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec |
| 210 |
|
| 211 |
|
| 212 |
def is_device_valid(meraki, serial, data): |
| 213 |
""" Parse a list of devices for a serial and return True if it's in the list """ |
| 214 |
for device in data: |
| 215 |
if device['serial'] == serial: |
| 216 |
return True |
| 217 |
return False |
| 218 |
|
| 219 |
|
| 220 |
def get_org_devices(meraki, org_id): |
| 221 |
""" Get all devices in an organization """ |
| 222 |
path = meraki.construct_path('get_all_org', org_id=org_id) |
| 223 |
response = meraki.request(path, method='GET') |
| 224 |
if meraki.status != 200: |
| 225 |
meraki.fail_json(msg='Failed to query all devices belonging to the organization') |
| 226 |
return response |
| 227 |
|
| 228 |
|
| 229 |
def get_net_devices(meraki, net_id): |
| 230 |
""" Get all devices in a network """ |
| 231 |
path = meraki.construct_path('get_all', net_id=net_id) |
| 232 |
response = meraki.request(path, method='GET') |
| 233 |
if meraki.status != 200: |
| 234 |
meraki.fail_json(msg='Failed to query all devices belonging to the network') |
| 235 |
return response |
| 236 |
|
| 237 |
|
| 238 |
def construct_payload(params): |
| 239 |
""" Create payload based on inputs """ |
| 240 |
payload = {} |
| 241 |
if params['hostname'] is not None: |
| 242 |
payload['name'] = params['hostname'] |
| 243 |
if params['tags'] is not None: |
| 244 |
payload['tags'] = params['tags'] |
| 245 |
if params['lat'] is not None: |
| 246 |
payload['lat'] = params['lat'] |
| 247 |
if params['lng'] is not None: |
| 248 |
payload['lng'] = params['lng'] |
| 249 |
if params['address'] is not None: |
| 250 |
payload['address'] = params['address'] |
| 251 |
if params['move_map_marker'] is not None: |
| 252 |
payload['moveMapMarker'] = params['move_map_marker'] |
| 253 |
if params['note'] is not None: |
| 254 |
payload['notes'] = params['note'] |
| 255 |
return payload |
| 256 |
|
| 257 |
|
| 258 |
def main(): |
| 259 |
|
| 260 |
# define the available arguments/parameters that a user can pass to |
| 261 |
# the module |
| 262 |
argument_spec = meraki_argument_spec() |
| 263 |
argument_spec.update(state=dict(type='str', choices=['absent', 'present', 'query'], default='query'), |
| 264 |
net_name=dict(type='str', aliases=['network']), |
| 265 |
net_id=dict(type='str'), |
| 266 |
serial=dict(type='str'), |
| 267 |
lldp_cdp_timespan=dict(type='int'), |
| 268 |
hostname=dict(type='str', aliases=['name']), |
| 269 |
model=dict(type='str'), |
| 270 |
tags=dict(type='list', elements='str', default=None), |
| 271 |
lat=dict(type='float', aliases=['latitude'], default=None), |
| 272 |
lng=dict(type='float', aliases=['longitude'], default=None), |
| 273 |
address=dict(type='str', default=None), |
| 274 |
move_map_marker=dict(type='bool', default=None), |
| 275 |
note=dict(type='str', default=None), |
| 276 |
query=dict(type='str', default=None, choices=['lldp_cdp', 'uplink']) |
| 277 |
) |
| 278 |
|
| 279 |
# the AnsibleModule object will be our abstraction working with Ansible |
| 280 |
# this includes instantiation, a couple of common attr would be the |
| 281 |
# args/params passed to the execution, as well as if the module |
| 282 |
# supports check mode |
| 283 |
module = AnsibleModule(argument_spec=argument_spec, |
| 284 |
supports_check_mode=False, |
| 285 |
) |
| 286 |
meraki = MerakiModule(module, function='device') |
| 287 |
|
| 288 |
if meraki.params['query'] is not None \ |
| 289 |
and meraki.params['query'] == 'lldp_cdp' \ |
| 290 |
and not meraki.params['lldp_cdp_timespan']: |
| 291 |
meraki.fail_json(msg='lldp_cdp_timespan is required when querying LLDP and CDP information') |
| 292 |
if meraki.params['net_name'] and meraki.params['net_id']: |
| 293 |
meraki.fail_json(msg='net_name and net_id are mutually exclusive') |
| 294 |
|
| 295 |
meraki.params['follow_redirects'] = 'all' |
| 296 |
|
| 297 |
query_urls = {'device': '/networks/{net_id}/devices'} |
| 298 |
query_org_urls = {'device': '/organizations/{org_id}/devices'} |
| 299 |
query_device_urls = {'device': '/devices/{serial}'} |
| 300 |
query_device_lldp_urls = {'device': '/devices/{serial}/lldpCdp'} |
| 301 |
claim_device_urls = {'device': '/networks/{net_id}/devices/claim'} |
| 302 |
bind_org_urls = {'device': '/organizations/{org_id}/claim'} |
| 303 |
update_device_urls = {'device': '/devices/{serial}'} |
| 304 |
delete_device_urls = {'device': '/networks/{net_id}/devices/remove'} |
| 305 |
|
| 306 |
meraki.url_catalog['get_all'].update(query_urls) |
| 307 |
meraki.url_catalog['get_all_org'] = query_org_urls |
| 308 |
meraki.url_catalog['get_device'] = query_device_urls |
| 309 |
meraki.url_catalog['get_device_uplink'] = query_device_urls |
| 310 |
meraki.url_catalog['get_device_lldp'] = query_device_lldp_urls |
| 311 |
meraki.url_catalog['create'] = claim_device_urls |
| 312 |
meraki.url_catalog['bind_org'] = bind_org_urls |
| 313 |
meraki.url_catalog['update'] = update_device_urls |
| 314 |
meraki.url_catalog['delete'] = delete_device_urls |
| 315 |
|
| 316 |
payload = None |
| 317 |
|
| 318 |
# execute checks for argument completeness |
| 319 |
|
| 320 |
# manipulate or modify the state as needed (this is going to be the |
| 321 |
# part where your module will do what it needs to do) |
| 322 |
org_id = meraki.params['org_id'] |
| 323 |
if org_id is None: |
| 324 |
org_id = meraki.get_org_id(meraki.params['org_name']) |
| 325 |
nets = meraki.get_nets(org_id=org_id) |
| 326 |
net_id = None |
| 327 |
if meraki.params['net_id'] or meraki.params['net_name']: |
| 328 |
net_id = meraki.params['net_id'] |
| 329 |
if net_id is None: |
| 330 |
net_id = meraki.get_net_id(net_name=meraki.params['net_name'], data=nets) |
| 331 |
|
| 332 |
if meraki.params['state'] == 'query': |
| 333 |
if meraki.params['net_name'] or meraki.params['net_id']: |
| 334 |
device = [] |
| 335 |
if meraki.params['serial']: |
| 336 |
path = meraki.construct_path('get_device', net_id=net_id, custom={'serial': meraki.params['serial']}) |
| 337 |
request = meraki.request(path, method='GET') |
| 338 |
device.append(request) |
| 339 |
meraki.result['data'] = device |
| 340 |
if meraki.params['query'] == 'uplink': |
| 341 |
path = meraki.construct_path('get_device_uplink', net_id=net_id, custom={'serial': meraki.params['serial']}) |
| 342 |
meraki.result['data'] = (meraki.request(path, method='GET')) |
| 343 |
elif meraki.params['query'] == 'lldp_cdp': |
| 344 |
if meraki.params['lldp_cdp_timespan'] > 2592000: |
| 345 |
meraki.fail_json(msg='LLDP/CDP timespan must be less than a month (2592000 seconds)') |
| 346 |
path = meraki.construct_path('get_device_lldp', net_id=net_id, custom={'serial': meraki.params['serial']}) |
| 347 |
path = path + '?timespan=' + str(meraki.params['lldp_cdp_timespan']) |
| 348 |
device.append(meraki.request(path, method='GET')) |
| 349 |
meraki.result['data'] = device |
| 350 |
elif meraki.params['hostname']: |
| 351 |
path = meraki.construct_path('get_all', net_id=net_id) |
| 352 |
devices = meraki.request(path, method='GET') |
| 353 |
for unit in devices: |
| 354 |
try: |
| 355 |
if unit['name'] == meraki.params['hostname']: |
| 356 |
device.append(unit) |
| 357 |
meraki.result['data'] = device |
| 358 |
except KeyError: |
| 359 |
pass |
| 360 |
elif meraki.params['model']: |
| 361 |
path = meraki.construct_path('get_all', net_id=net_id) |
| 362 |
devices = meraki.request(path, method='GET') |
| 363 |
device_match = [] |
| 364 |
for device in devices: |
| 365 |
if device['model'] == meraki.params['model']: |
| 366 |
device_match.append(device) |
| 367 |
meraki.result['data'] = device_match |
| 368 |
else: |
| 369 |
path = meraki.construct_path('get_all', net_id=net_id) |
| 370 |
request = meraki.request(path, method='GET') |
| 371 |
meraki.result['data'] = request |
| 372 |
else: |
| 373 |
path = meraki.construct_path('get_all_org', org_id=org_id, params={'perPage': '1000'}) |
| 374 |
devices = meraki.request(path, method='GET', pagination_items=1000) |
| 375 |
if meraki.params['serial']: |
| 376 |
for device in devices: |
| 377 |
if device['serial'] == meraki.params['serial']: |
| 378 |
meraki.result['data'] = device |
| 379 |
else: |
| 380 |
meraki.result['data'] = devices |
| 381 |
elif meraki.params['state'] == 'present': |
| 382 |
device = [] |
| 383 |
if net_id is None: # Claim a device to an organization |
| 384 |
device_list = get_org_devices(meraki, org_id) |
| 385 |
if is_device_valid(meraki, meraki.params['serial'], device_list) is False: |
| 386 |
payload = {'serial': meraki.params['serial']} |
| 387 |
path = meraki.construct_path('bind_org', org_id=org_id) |
| 388 |
created_device = [] |
| 389 |
created_device.append(meraki.request(path, method='POST', payload=json.dumps(payload))) |
| 390 |
meraki.result['data'] = created_device |
| 391 |
meraki.result['changed'] = True |
| 392 |
else: # A device is assumed to be in an organization |
| 393 |
device_list = get_net_devices(meraki, net_id) |
| 394 |
if is_device_valid(meraki, meraki.params['serial'], device_list) is True: # Device is in network, update |
| 395 |
query_path = meraki.construct_path('get_all', net_id=net_id) |
| 396 |
if is_device_valid(meraki, meraki.params['serial'], device_list): |
| 397 |
payload = construct_payload(meraki.params) |
| 398 |
query_path = meraki.construct_path('get_device', net_id=net_id, custom={'serial': meraki.params['serial']}) |
| 399 |
device_data = meraki.request(query_path, method='GET') |
| 400 |
ignore_keys = ['lanIp', 'serial', 'mac', 'model', 'networkId', 'moveMapMarker', 'wan1Ip', 'wan2Ip'] |
| 401 |
if meraki.is_update_required(device_data, payload, optional_ignore=ignore_keys): |
| 402 |
path = meraki.construct_path('update', custom={'serial': meraki.params['serial']}) |
| 403 |
updated_device = [] |
| 404 |
updated_device.append(meraki.request(path, method='PUT', payload=json.dumps(payload))) |
| 405 |
meraki.result['data'] = updated_device |
| 406 |
meraki.result['changed'] = True |
| 407 |
else: |
| 408 |
meraki.result['data'] = device_data |
| 409 |
else: # Claim device into network |
| 410 |
query_path = meraki.construct_path('get_all', net_id=net_id) |
| 411 |
device_list = meraki.request(query_path, method='GET') |
| 412 |
if is_device_valid(meraki, meraki.params['serial'], device_list) is False: |
| 413 |
if net_id: |
| 414 |
payload = {'serials': [meraki.params['serial']]} |
| 415 |
path = meraki.construct_path('create', net_id=net_id) |
| 416 |
created_device = [] |
| 417 |
created_device.append(meraki.request(path, method='POST', payload=json.dumps(payload))) |
| 418 |
meraki.result['data'] = created_device |
| 419 |
meraki.result['changed'] = True |
| 420 |
elif meraki.params['state'] == 'absent': |
| 421 |
device = [] |
| 422 |
query_path = meraki.construct_path('get_all', net_id=net_id) |
| 423 |
device_list = meraki.request(query_path, method='GET') |
| 424 |
if is_device_valid(meraki, meraki.params['serial'], device_list) is True: |
| 425 |
payload = {'serial': meraki.params['serial']} |
| 426 |
path = meraki.construct_path('delete', net_id=net_id) |
| 427 |
request = meraki.request(path, method='POST', payload=json.dumps(payload)) |
| 428 |
meraki.result['changed'] = True |
| 429 |
|
| 430 |
# in the event of a successful module execution, you will want to |
| 431 |
# simple AnsibleModule.exit_json(), passing the key/value results |
| 432 |
meraki.exit_json(**meraki.result) |
| 433 |
|
| 434 |
|
| 435 |
if __name__ == '__main__': |
| 436 |
main() |