summaryrefslogtreecommitdiffstats
path: root/ansible_collections/cisco/meraki/plugins/modules/meraki_management_interface.py
blob: 17b164208b752d7287ec5ac00b57eeb07cabd012 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright: (c) 2019, Kevin Breit (@kbreit) <kevin.breit@kevinbreit.net>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

ANSIBLE_METADATA = {
    'metadata_version': '1.1',
    "status": ['deprecated'],
    'supported_by': 'community'
}

DOCUMENTATION = r'''
---
module: meraki_management_interface
short_description: Configure Meraki management interfaces
version_added: "1.1.0"
description:
- Allows for configuration of management interfaces on Meraki MX, MS, and MR devices.
notes:
- C(WAN2) parameter is only valid for MX appliances.
- C(wan_enabled) should not be provided for non-MX devies.
deprecated:
  removed_in: '3.0.0'
  why: Updated modules released with increased functionality
  alternative: cisco.meraki.devices_management_interface
options:
    state:
        description:
        - Specifies whether configuration template information should be queried, modified, or deleted.
        choices: ['absent', 'query', 'present']
        default: query
        type: str
    org_name:
        description:
        - Name of organization containing the configuration template.
        type: str
    org_id:
        description:
        - ID of organization associated to a configuration template.
        type: str
    net_name:
        description:
        - Name of the network to bind or unbind configuration template to.
        type: str
    net_id:
        description:
        - ID of the network to bind or unbind configuration template to.
        type: str
    serial:
        description:
        - serial number of the device to configure.
        type: str
        required: true
    wan1:
        description:
        - Management interface details for management interface.
        aliases: [mgmt1]
        type: dict
        suboptions:
            wan_enabled:
                description:
                - States whether the management interface is enabled.
                - Only valid for MX devices.
                type: str
                choices: [disabled, enabled, not configured]
            using_static_ip:
                description:
                - Configures the interface to use static IP or DHCP.
                type: bool
            static_ip:
                description:
                - IP address assigned to Management interface.
                - Valid only if C(using_static_ip) is C(True).
                type: str
            static_gateway_ip:
                description:
                - IP address for default gateway.
                - Valid only if C(using_static_ip) is C(True).
                type: str
            static_subnet_mask:
                description:
                - Netmask for static IP address.
                - Valid only if C(using_static_ip) is C(True).
                type: str
            static_dns:
                description:
                - DNS servers to use.
                - Allows for a maximum of 2 addresses.
                type: list
                elements: str
            vlan:
                description:
                - VLAN number to use for the management network.
                type: int
    wan2:
        description:
        - Management interface details for management interface.
        type: dict
        aliases: [mgmt2]
        suboptions:
            wan_enabled:
                description:
                - States whether the management interface is enabled.
                - Only valid for MX devices.
                type: str
                choices: [disabled, enabled, not configured]
            using_static_ip:
                description:
                - Configures the interface to use static IP or DHCP.
                type: bool
            static_ip:
                description:
                - IP address assigned to Management interface.
                - Valid only if C(using_static_ip) is C(True).
                type: str
            static_gateway_ip:
                description:
                - IP address for default gateway.
                - Valid only if C(using_static_ip) is C(True).
                type: str
            static_subnet_mask:
                description:
                - Netmask for static IP address.
                - Valid only if C(using_static_ip) is C(True).
                type: str
            static_dns:
                description:
                - DNS servers to use.
                - Allows for a maximum of 2 addresses.
                type: list
                elements: str
            vlan:
                description:
                - VLAN number to use for the management network.
                type: int

author:
- Kevin Breit (@kbreit)
extends_documentation_fragment: cisco.meraki.meraki
'''

EXAMPLES = r'''
- name: Set WAN2 as static IP
  meraki_management_interface:
    auth_key: abc123
    state: present
    org_name: YourOrg
    net_id: YourNetId
    serial: AAAA-BBBB-CCCC
    wan2:
      wan_enabled: enabled
      using_static_ip: yes
      static_ip: 192.168.16.195
      static_gateway_ip: 192.168.16.1
      static_subnet_mask: 255.255.255.0
      static_dns:
        - 1.1.1.1
      vlan: 1
  delegate_to: localhost

- name: Query management information
  meraki_management_interface:
    auth_key: abc123
    state: query
    org_name: YourOrg
    net_id: YourNetId
    serial: AAAA-BBBB-CCCC
  delegate_to: localhost
'''

RETURN = r'''
data:
    description: Information about queried object.
    returned: success
    type: complex
    contains:
        wan1:
          description: Management configuration for WAN1 interface
          returned: success
          type: complex
          contains:
            wan_enabled:
                description: Enabled state of interface
                returned: success
                type: str
                sample: enabled
            using_static_ip:
                description: Boolean value of whether static IP assignment is used on interface
                returned: success
                type: bool
                sample: True
            static_ip:
                description: Assigned static IP
                returned: only if static IP assignment is used
                type: str
                sample: 192.0.1.2
            static_gateway_ip:
                description: Assigned static gateway IP
                returned: only if static IP assignment is used
                type: str
                sample: 192.0.1.1
            static_subnet_mask:
                description: Assigned netmask for static IP
                returned: only if static IP assignment is used
                type: str
                sample: 255.255.255.0
            static_dns:
                description: List of DNS IP addresses
                returned: only if static IP assignment is used
                type: list
                sample: ["1.1.1.1"]
            vlan:
                description: VLAN tag id of management VLAN
                returned: success
                type: int
                sample: 2
        wan2:
          description: Management configuration for WAN1 interface
          returned: success
          type: complex
          contains:
            wan_enabled:
                description: Enabled state of interface
                returned: success
                type: str
                sample: enabled
            using_static_ip:
                description: Boolean value of whether static IP assignment is used on interface
                returned: success
                type: bool
                sample: True
            static_ip:
                description: Assigned static IP
                returned: only if static IP assignment is used
                type: str
                sample: 192.0.1.2
            static_gateway_ip:
                description: Assigned static gateway IP
                returned: only if static IP assignment is used
                type: str
                sample: 192.0.1.1
            static_subnet_mask:
                description: Assigned netmask for static IP
                returned: only if static IP assignment is used
                type: str
                sample: 255.255.255.0
            static_dns:
                description: List of DNS IP addresses
                returned: only if static IP assignment is used
                type: list
                sample: ["1.1.1.1"]
            vlan:
                description: VLAN tag id of management VLAN
                returned: success
                type: int
                sample: 2
'''

from ansible.module_utils.basic import AnsibleModule, json
from ansible.module_utils.common.dict_transformations import recursive_diff
from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec


def main():
    # define the available arguments/parameters that a user can pass to
    # the module

    int_arg_spec = dict(wan_enabled=dict(type='str', choices=['enabled', 'disabled', 'not configured']),
                        using_static_ip=dict(type='bool'),
                        static_ip=dict(type='str'),
                        static_gateway_ip=dict(type='str'),
                        static_subnet_mask=dict(type='str'),
                        static_dns=dict(type='list', elements='str'),
                        vlan=dict(type='int'),
                        )

    argument_spec = meraki_argument_spec()
    argument_spec.update(state=dict(type='str', choices=['absent', 'query', 'present'], default='query'),
                         net_name=dict(type='str'),
                         net_id=dict(type='str'),
                         serial=dict(type='str', required=True),
                         wan1=dict(type='dict', default=None, options=int_arg_spec, aliases=['mgmt1']),
                         wan2=dict(type='dict', default=None, options=int_arg_spec, aliases=['mgmt2']),
                         )

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True,
                           )
    meraki = MerakiModule(module, function='management_interface')
    meraki.params['follow_redirects'] = 'all'

    query_urls = {'management_interface': '/devices/{serial}/managementInterface'}

    meraki.url_catalog['get_one'].update(query_urls)

    if meraki.params['net_id'] and meraki.params['net_name']:
        meraki.fail_json('net_id and net_name are mutually exclusive.')
    if meraki.params['state'] == 'present':
        interfaces = ('wan1', 'wan2')
        for interface in interfaces:
            if meraki.params[interface] is not None:
                if meraki.params[interface]['using_static_ip'] is True:
                    if len(meraki.params[interface]['static_dns']) > 2:
                        meraki.fail_json("Maximum number of static DNS addresses is 2.")

    payload = dict()

    if meraki.params['state'] == 'present':
        interfaces = ('wan1', 'wan2')
        for interface in interfaces:
            if meraki.params[interface] is not None:
                wan_int = dict()
                if meraki.params[interface]['wan_enabled'] is not None:
                    wan_int['wanEnabled'] = meraki.params[interface]['wan_enabled']
                if meraki.params[interface]['using_static_ip'] is not None:
                    wan_int['usingStaticIp'] = meraki.params[interface]['using_static_ip']
                if meraki.params[interface]['vlan'] is not None:
                    wan_int['vlan'] = meraki.params[interface]['vlan']
                if meraki.params[interface]['using_static_ip'] is True:
                    wan_int['staticIp'] = meraki.params[interface]['static_ip']
                    wan_int['staticGatewayIp'] = meraki.params[interface]['static_gateway_ip']
                    wan_int['staticSubnetMask'] = meraki.params[interface]['static_subnet_mask']
                    wan_int['staticDns'] = meraki.params[interface]['static_dns']
                payload[interface] = wan_int

    # manipulate or modify the state as needed (this is going to be the
    # part where your module will do what it needs to do)
    org_id = meraki.params['org_id']
    if meraki.params['org_name']:
        org_id = meraki.get_org_id(meraki.params['org_name'])
    net_id = meraki.params['net_id']
    if net_id is None:
        nets = meraki.get_nets(org_id=org_id)
        net_id = meraki.get_net_id(net_name=meraki.params['net_name'], data=nets)

    if meraki.params['state'] == 'query':
        path = meraki.construct_path('get_one', net_id=net_id, custom={'serial': meraki.params['serial']})
        response = meraki.request(path, method='GET')
        if meraki.status == 200:
            meraki.result['data'] = response
    elif meraki.params['state'] == 'present':
        path = meraki.construct_path('get_one', custom={'serial': meraki.params['serial']})
        original = meraki.request(path, method='GET')
        update_required = False
        if 'wan1' in original:
            if 'wanEnabled' in original['wan1']:
                update_required = meraki.is_update_required(original, payload)
            else:
                update_required = meraki.is_update_required(original, payload, optional_ignore=['wanEnabled'])
        if 'wan2' in original and update_required is False:
            if 'wanEnabled' in original['wan2']:
                update_required = meraki.is_update_required(original, payload)
            else:
                update_required = meraki.is_update_required(original, payload, optional_ignore=['wanEnabled'])
        if update_required is True:
            if meraki.check_mode is True:
                diff = recursive_diff(original, payload)
                original.update(payload)
                meraki.result['diff'] = {'before': diff[0],
                                         'after': diff[1]}
                meraki.result['data'] = original
                meraki.result['changed'] = True
                meraki.exit_json(**meraki.result)
            response = meraki.request(path, method='PUT', payload=json.dumps(payload))
            if meraki.status == 200:
                diff = recursive_diff(original, response)
                meraki.result['diff'] = {'before': diff[0],
                                         'after': diff[1]}
                meraki.result['data'] = response
                meraki.result['changed'] = True
        else:
            meraki.result['data'] = original

    # in the event of a successful module execution, you will want to
    # simple AnsibleModule.exit_json(), passing the key/value results
    meraki.exit_json(**meraki.result)


if __name__ == '__main__':
    main()