| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2019, 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_management_interface |
| 19 |
short_description: Configure Meraki management interfaces |
| 20 |
version_added: "1.1.0" |
| 21 |
description: |
| 22 |
- Allows for configuration of management interfaces on Meraki MX, MS, and MR devices. |
| 23 |
notes: |
| 24 |
- C(WAN2) parameter is only valid for MX appliances. |
| 25 |
- C(wan_enabled) should not be provided for non-MX devies. |
| 26 |
deprecated: |
| 27 |
removed_in: '3.0.0' |
| 28 |
why: Updated modules released with increased functionality |
| 29 |
alternative: cisco.meraki.devices_management_interface |
| 30 |
options: |
| 31 |
state: |
| 32 |
description: |
| 33 |
- Specifies whether configuration template information should be queried, modified, or deleted. |
| 34 |
choices: ['absent', 'query', 'present'] |
| 35 |
default: query |
| 36 |
type: str |
| 37 |
org_name: |
| 38 |
description: |
| 39 |
- Name of organization containing the configuration template. |
| 40 |
type: str |
| 41 |
org_id: |
| 42 |
description: |
| 43 |
- ID of organization associated to a configuration template. |
| 44 |
type: str |
| 45 |
net_name: |
| 46 |
description: |
| 47 |
- Name of the network to bind or unbind configuration template to. |
| 48 |
type: str |
| 49 |
net_id: |
| 50 |
description: |
| 51 |
- ID of the network to bind or unbind configuration template to. |
| 52 |
type: str |
| 53 |
serial: |
| 54 |
description: |
| 55 |
- serial number of the device to configure. |
| 56 |
type: str |
| 57 |
required: true |
| 58 |
wan1: |
| 59 |
description: |
| 60 |
- Management interface details for management interface. |
| 61 |
aliases: [mgmt1] |
| 62 |
type: dict |
| 63 |
suboptions: |
| 64 |
wan_enabled: |
| 65 |
description: |
| 66 |
- States whether the management interface is enabled. |
| 67 |
- Only valid for MX devices. |
| 68 |
type: str |
| 69 |
choices: [disabled, enabled, not configured] |
| 70 |
using_static_ip: |
| 71 |
description: |
| 72 |
- Configures the interface to use static IP or DHCP. |
| 73 |
type: bool |
| 74 |
static_ip: |
| 75 |
description: |
| 76 |
- IP address assigned to Management interface. |
| 77 |
- Valid only if C(using_static_ip) is C(True). |
| 78 |
type: str |
| 79 |
static_gateway_ip: |
| 80 |
description: |
| 81 |
- IP address for default gateway. |
| 82 |
- Valid only if C(using_static_ip) is C(True). |
| 83 |
type: str |
| 84 |
static_subnet_mask: |
| 85 |
description: |
| 86 |
- Netmask for static IP address. |
| 87 |
- Valid only if C(using_static_ip) is C(True). |
| 88 |
type: str |
| 89 |
static_dns: |
| 90 |
description: |
| 91 |
- DNS servers to use. |
| 92 |
- Allows for a maximum of 2 addresses. |
| 93 |
type: list |
| 94 |
elements: str |
| 95 |
vlan: |
| 96 |
description: |
| 97 |
- VLAN number to use for the management network. |
| 98 |
type: int |
| 99 |
wan2: |
| 100 |
description: |
| 101 |
- Management interface details for management interface. |
| 102 |
type: dict |
| 103 |
aliases: [mgmt2] |
| 104 |
suboptions: |
| 105 |
wan_enabled: |
| 106 |
description: |
| 107 |
- States whether the management interface is enabled. |
| 108 |
- Only valid for MX devices. |
| 109 |
type: str |
| 110 |
choices: [disabled, enabled, not configured] |
| 111 |
using_static_ip: |
| 112 |
description: |
| 113 |
- Configures the interface to use static IP or DHCP. |
| 114 |
type: bool |
| 115 |
static_ip: |
| 116 |
description: |
| 117 |
- IP address assigned to Management interface. |
| 118 |
- Valid only if C(using_static_ip) is C(True). |
| 119 |
type: str |
| 120 |
static_gateway_ip: |
| 121 |
description: |
| 122 |
- IP address for default gateway. |
| 123 |
- Valid only if C(using_static_ip) is C(True). |
| 124 |
type: str |
| 125 |
static_subnet_mask: |
| 126 |
description: |
| 127 |
- Netmask for static IP address. |
| 128 |
- Valid only if C(using_static_ip) is C(True). |
| 129 |
type: str |
| 130 |
static_dns: |
| 131 |
description: |
| 132 |
- DNS servers to use. |
| 133 |
- Allows for a maximum of 2 addresses. |
| 134 |
type: list |
| 135 |
elements: str |
| 136 |
vlan: |
| 137 |
description: |
| 138 |
- VLAN number to use for the management network. |
| 139 |
type: int |
| 140 |
|
| 141 |
author: |
| 142 |
- Kevin Breit (@kbreit) |
| 143 |
extends_documentation_fragment: cisco.meraki.meraki |
| 144 |
''' |
| 145 |
|
| 146 |
EXAMPLES = r''' |
| 147 |
- name: Set WAN2 as static IP |
| 148 |
meraki_management_interface: |
| 149 |
auth_key: abc123 |
| 150 |
state: present |
| 151 |
org_name: YourOrg |
| 152 |
net_id: YourNetId |
| 153 |
serial: AAAA-BBBB-CCCC |
| 154 |
wan2: |
| 155 |
wan_enabled: enabled |
| 156 |
using_static_ip: true |
| 157 |
static_ip: 192.168.16.195 |
| 158 |
static_gateway_ip: 192.168.16.1 |
| 159 |
static_subnet_mask: 255.255.255.0 |
| 160 |
static_dns: |
| 161 |
- 1.1.1.1 |
| 162 |
vlan: 1 |
| 163 |
delegate_to: localhost |
| 164 |
|
| 165 |
- name: Query management information |
| 166 |
meraki_management_interface: |
| 167 |
auth_key: abc123 |
| 168 |
state: query |
| 169 |
org_name: YourOrg |
| 170 |
net_id: YourNetId |
| 171 |
serial: AAAA-BBBB-CCCC |
| 172 |
delegate_to: localhost |
| 173 |
''' |
| 174 |
|
| 175 |
RETURN = r''' |
| 176 |
data: |
| 177 |
description: Information about queried object. |
| 178 |
returned: success |
| 179 |
type: complex |
| 180 |
contains: |
| 181 |
wan1: |
| 182 |
description: Management configuration for WAN1 interface |
| 183 |
returned: success |
| 184 |
type: complex |
| 185 |
contains: |
| 186 |
wan_enabled: |
| 187 |
description: Enabled state of interface |
| 188 |
returned: success |
| 189 |
type: str |
| 190 |
sample: enabled |
| 191 |
using_static_ip: |
| 192 |
description: Boolean value of whether static IP assignment is used on interface |
| 193 |
returned: success |
| 194 |
type: bool |
| 195 |
sample: true |
| 196 |
static_ip: |
| 197 |
description: Assigned static IP |
| 198 |
returned: only if static IP assignment is used |
| 199 |
type: str |
| 200 |
sample: 192.0.1.2 |
| 201 |
static_gateway_ip: |
| 202 |
description: Assigned static gateway IP |
| 203 |
returned: only if static IP assignment is used |
| 204 |
type: str |
| 205 |
sample: 192.0.1.1 |
| 206 |
static_subnet_mask: |
| 207 |
description: Assigned netmask for static IP |
| 208 |
returned: only if static IP assignment is used |
| 209 |
type: str |
| 210 |
sample: 255.255.255.0 |
| 211 |
static_dns: |
| 212 |
description: List of DNS IP addresses |
| 213 |
returned: only if static IP assignment is used |
| 214 |
type: list |
| 215 |
sample: ["1.1.1.1"] |
| 216 |
vlan: |
| 217 |
description: VLAN tag id of management VLAN |
| 218 |
returned: success |
| 219 |
type: int |
| 220 |
sample: 2 |
| 221 |
wan2: |
| 222 |
description: Management configuration for WAN1 interface |
| 223 |
returned: success |
| 224 |
type: complex |
| 225 |
contains: |
| 226 |
wan_enabled: |
| 227 |
description: Enabled state of interface |
| 228 |
returned: success |
| 229 |
type: str |
| 230 |
sample: enabled |
| 231 |
using_static_ip: |
| 232 |
description: Boolean value of whether static IP assignment is used on interface |
| 233 |
returned: success |
| 234 |
type: bool |
| 235 |
sample: true |
| 236 |
static_ip: |
| 237 |
description: Assigned static IP |
| 238 |
returned: only if static IP assignment is used |
| 239 |
type: str |
| 240 |
sample: 192.0.1.2 |
| 241 |
static_gateway_ip: |
| 242 |
description: Assigned static gateway IP |
| 243 |
returned: only if static IP assignment is used |
| 244 |
type: str |
| 245 |
sample: 192.0.1.1 |
| 246 |
static_subnet_mask: |
| 247 |
description: Assigned netmask for static IP |
| 248 |
returned: only if static IP assignment is used |
| 249 |
type: str |
| 250 |
sample: 255.255.255.0 |
| 251 |
static_dns: |
| 252 |
description: List of DNS IP addresses |
| 253 |
returned: only if static IP assignment is used |
| 254 |
type: list |
| 255 |
sample: ["1.1.1.1"] |
| 256 |
vlan: |
| 257 |
description: VLAN tag id of management VLAN |
| 258 |
returned: success |
| 259 |
type: int |
| 260 |
sample: 2 |
| 261 |
''' |
| 262 |
|
| 263 |
from ansible.module_utils.basic import AnsibleModule, json |
| 264 |
from ansible.module_utils.common.dict_transformations import recursive_diff |
| 265 |
from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec |
| 266 |
|
| 267 |
|
| 268 |
def main(): |
| 269 |
# define the available arguments/parameters that a user can pass to |
| 270 |
# the module |
| 271 |
|
| 272 |
int_arg_spec = dict(wan_enabled=dict(type='str', choices=['enabled', 'disabled', 'not configured']), |
| 273 |
using_static_ip=dict(type='bool'), |
| 274 |
static_ip=dict(type='str'), |
| 275 |
static_gateway_ip=dict(type='str'), |
| 276 |
static_subnet_mask=dict(type='str'), |
| 277 |
static_dns=dict(type='list', elements='str'), |
| 278 |
vlan=dict(type='int'), |
| 279 |
) |
| 280 |
|
| 281 |
argument_spec = meraki_argument_spec() |
| 282 |
argument_spec.update(state=dict(type='str', choices=['absent', 'query', 'present'], default='query'), |
| 283 |
net_name=dict(type='str'), |
| 284 |
net_id=dict(type='str'), |
| 285 |
serial=dict(type='str', required=True), |
| 286 |
wan1=dict(type='dict', default=None, options=int_arg_spec, aliases=['mgmt1']), |
| 287 |
wan2=dict(type='dict', default=None, options=int_arg_spec, aliases=['mgmt2']), |
| 288 |
) |
| 289 |
|
| 290 |
# the AnsibleModule object will be our abstraction working with Ansible |
| 291 |
# this includes instantiation, a couple of common attr would be the |
| 292 |
# args/params passed to the execution, as well as if the module |
| 293 |
# supports check mode |
| 294 |
module = AnsibleModule(argument_spec=argument_spec, |
| 295 |
supports_check_mode=True, |
| 296 |
) |
| 297 |
meraki = MerakiModule(module, function='management_interface') |
| 298 |
meraki.params['follow_redirects'] = 'all' |
| 299 |
|
| 300 |
query_urls = {'management_interface': '/devices/{serial}/managementInterface'} |
| 301 |
|
| 302 |
meraki.url_catalog['get_one'].update(query_urls) |
| 303 |
|
| 304 |
if meraki.params['net_id'] and meraki.params['net_name']: |
| 305 |
meraki.fail_json('net_id and net_name are mutually exclusive.') |
| 306 |
if meraki.params['state'] == 'present': |
| 307 |
interfaces = ('wan1', 'wan2') |
| 308 |
for interface in interfaces: |
| 309 |
if meraki.params[interface] is not None: |
| 310 |
if meraki.params[interface]['using_static_ip'] is True: |
| 311 |
if len(meraki.params[interface]['static_dns']) > 2: |
| 312 |
meraki.fail_json("Maximum number of static DNS addresses is 2.") |
| 313 |
|
| 314 |
payload = dict() |
| 315 |
|
| 316 |
if meraki.params['state'] == 'present': |
| 317 |
interfaces = ('wan1', 'wan2') |
| 318 |
for interface in interfaces: |
| 319 |
if meraki.params[interface] is not None: |
| 320 |
wan_int = dict() |
| 321 |
if meraki.params[interface]['wan_enabled'] is not None: |
| 322 |
wan_int['wanEnabled'] = meraki.params[interface]['wan_enabled'] |
| 323 |
if meraki.params[interface]['using_static_ip'] is not None: |
| 324 |
wan_int['usingStaticIp'] = meraki.params[interface]['using_static_ip'] |
| 325 |
if meraki.params[interface]['vlan'] is not None: |
| 326 |
wan_int['vlan'] = meraki.params[interface]['vlan'] |
| 327 |
if meraki.params[interface]['using_static_ip'] is True: |
| 328 |
wan_int['staticIp'] = meraki.params[interface]['static_ip'] |
| 329 |
wan_int['staticGatewayIp'] = meraki.params[interface]['static_gateway_ip'] |
| 330 |
wan_int['staticSubnetMask'] = meraki.params[interface]['static_subnet_mask'] |
| 331 |
wan_int['staticDns'] = meraki.params[interface]['static_dns'] |
| 332 |
payload[interface] = wan_int |
| 333 |
|
| 334 |
# manipulate or modify the state as needed (this is going to be the |
| 335 |
# part where your module will do what it needs to do) |
| 336 |
org_id = meraki.params['org_id'] |
| 337 |
if meraki.params['org_name']: |
| 338 |
org_id = meraki.get_org_id(meraki.params['org_name']) |
| 339 |
net_id = meraki.params['net_id'] |
| 340 |
if net_id is None: |
| 341 |
nets = meraki.get_nets(org_id=org_id) |
| 342 |
net_id = meraki.get_net_id(net_name=meraki.params['net_name'], data=nets) |
| 343 |
|
| 344 |
if meraki.params['state'] == 'query': |
| 345 |
path = meraki.construct_path('get_one', net_id=net_id, custom={'serial': meraki.params['serial']}) |
| 346 |
response = meraki.request(path, method='GET') |
| 347 |
if meraki.status == 200: |
| 348 |
meraki.result['data'] = response |
| 349 |
elif meraki.params['state'] == 'present': |
| 350 |
path = meraki.construct_path('get_one', custom={'serial': meraki.params['serial']}) |
| 351 |
original = meraki.request(path, method='GET') |
| 352 |
update_required = False |
| 353 |
if 'wan1' in original: |
| 354 |
if 'wanEnabled' in original['wan1']: |
| 355 |
update_required = meraki.is_update_required(original, payload) |
| 356 |
else: |
| 357 |
update_required = meraki.is_update_required(original, payload, optional_ignore=['wanEnabled']) |
| 358 |
if 'wan2' in original and update_required is False: |
| 359 |
if 'wanEnabled' in original['wan2']: |
| 360 |
update_required = meraki.is_update_required(original, payload) |
| 361 |
else: |
| 362 |
update_required = meraki.is_update_required(original, payload, optional_ignore=['wanEnabled']) |
| 363 |
if update_required is True: |
| 364 |
if meraki.check_mode is True: |
| 365 |
diff = recursive_diff(original, payload) |
| 366 |
original.update(payload) |
| 367 |
meraki.result['diff'] = {'before': diff[0], |
| 368 |
'after': diff[1]} |
| 369 |
meraki.result['data'] = original |
| 370 |
meraki.result['changed'] = True |
| 371 |
meraki.exit_json(**meraki.result) |
| 372 |
response = meraki.request(path, method='PUT', payload=json.dumps(payload)) |
| 373 |
if meraki.status == 200: |
| 374 |
diff = recursive_diff(original, response) |
| 375 |
meraki.result['diff'] = {'before': diff[0], |
| 376 |
'after': diff[1]} |
| 377 |
meraki.result['data'] = response |
| 378 |
meraki.result['changed'] = True |
| 379 |
else: |
| 380 |
meraki.result['data'] = original |
| 381 |
|
| 382 |
# in the event of a successful module execution, you will want to |
| 383 |
# simple AnsibleModule.exit_json(), passing the key/value results |
| 384 |
meraki.exit_json(**meraki.result) |
| 385 |
|
| 386 |
|
| 387 |
if __name__ == '__main__': |
| 388 |
main() |