| 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_ms_access_list |
| 19 |
short_description: Manage access lists for Meraki switches in the Meraki cloud |
| 20 |
version_added: "0.1.0" |
| 21 |
description: |
| 22 |
- Configure and query information about access lists on Meraki switches within the Meraki cloud. |
| 23 |
notes: |
| 24 |
- Some of the options are likely only used for developers within Meraki. |
| 25 |
deprecated: |
| 26 |
removed_in: '3.0.0' |
| 27 |
why: Updated modules released with increased functionality |
| 28 |
alternative: cisco.meraki.networks_switch_access_control_lists |
| 29 |
options: |
| 30 |
state: |
| 31 |
description: |
| 32 |
- Specifies whether object should be queried, created/modified, or removed. |
| 33 |
choices: [absent, present, query] |
| 34 |
default: query |
| 35 |
type: str |
| 36 |
net_name: |
| 37 |
description: |
| 38 |
- Name of network which configuration is applied to. |
| 39 |
aliases: [network] |
| 40 |
type: str |
| 41 |
net_id: |
| 42 |
description: |
| 43 |
- ID of network which configuration is applied to. |
| 44 |
type: str |
| 45 |
rules: |
| 46 |
description: |
| 47 |
- List of access control rules. |
| 48 |
type: list |
| 49 |
elements: dict |
| 50 |
suboptions: |
| 51 |
comment: |
| 52 |
description: |
| 53 |
- Description of the rule. |
| 54 |
type: str |
| 55 |
policy: |
| 56 |
description: |
| 57 |
- Action to take on matching traffic. |
| 58 |
choices: [allow, deny] |
| 59 |
type: str |
| 60 |
ip_version: |
| 61 |
description: |
| 62 |
- Type of IP packets to match. |
| 63 |
choices: [any, ipv4, ipv6] |
| 64 |
type: str |
| 65 |
protocol: |
| 66 |
description: |
| 67 |
- Type of protocol to match. |
| 68 |
choices: [any, tcp, udp] |
| 69 |
type: str |
| 70 |
src_cidr: |
| 71 |
description: |
| 72 |
- CIDR notation of source IP address to match. |
| 73 |
type: str |
| 74 |
src_port: |
| 75 |
description: |
| 76 |
- Port number of source port to match. |
| 77 |
- May be a port number or 'any'. |
| 78 |
type: str |
| 79 |
dst_cidr: |
| 80 |
description: |
| 81 |
- CIDR notation of source IP address to match. |
| 82 |
type: str |
| 83 |
dst_port: |
| 84 |
description: |
| 85 |
- Port number of destination port to match. |
| 86 |
- May be a port number or 'any'. |
| 87 |
type: str |
| 88 |
vlan: |
| 89 |
description: |
| 90 |
- Incoming traffic VLAN. |
| 91 |
- May be any port between 1-4095 or 'any'. |
| 92 |
type: str |
| 93 |
author: |
| 94 |
Kevin Breit (@kbreit) |
| 95 |
extends_documentation_fragment: cisco.meraki.meraki |
| 96 |
''' |
| 97 |
|
| 98 |
EXAMPLES = r''' |
| 99 |
- name: Set access list |
| 100 |
meraki_switch_access_list: |
| 101 |
auth_key: abc123 |
| 102 |
state: present |
| 103 |
org_name: YourOrg |
| 104 |
net_name: YourNet |
| 105 |
rules: |
| 106 |
- comment: Fake rule |
| 107 |
policy: allow |
| 108 |
ip_version: ipv4 |
| 109 |
protocol: udp |
| 110 |
src_cidr: 192.0.1.0/24 |
| 111 |
src_port: "4242" |
| 112 |
dst_cidr: 1.2.3.4/32 |
| 113 |
dst_port: "80" |
| 114 |
vlan: "100" |
| 115 |
delegate_to: localhost |
| 116 |
|
| 117 |
- name: Query access lists |
| 118 |
meraki_switch_access_list: |
| 119 |
auth_key: abc123 |
| 120 |
state: query |
| 121 |
org_name: YourOrg |
| 122 |
net_name: YourNet |
| 123 |
delegate_to: localhost |
| 124 |
''' |
| 125 |
|
| 126 |
RETURN = r''' |
| 127 |
data: |
| 128 |
description: List of administrators. |
| 129 |
returned: success |
| 130 |
type: complex |
| 131 |
contains: |
| 132 |
rules: |
| 133 |
description: |
| 134 |
- List of access control rules. |
| 135 |
type: list |
| 136 |
contains: |
| 137 |
comment: |
| 138 |
description: |
| 139 |
- Description of the rule. |
| 140 |
type: str |
| 141 |
sample: User rule |
| 142 |
returned: success |
| 143 |
policy: |
| 144 |
description: |
| 145 |
- Action to take on matching traffic. |
| 146 |
type: str |
| 147 |
sample: allow |
| 148 |
returned: success |
| 149 |
ip_version: |
| 150 |
description: |
| 151 |
- Type of IP packets to match. |
| 152 |
type: str |
| 153 |
sample: ipv4 |
| 154 |
returned: success |
| 155 |
protocol: |
| 156 |
description: |
| 157 |
- Type of protocol to match. |
| 158 |
type: str |
| 159 |
sample: udp |
| 160 |
returned: success |
| 161 |
src_cidr: |
| 162 |
description: |
| 163 |
- CIDR notation of source IP address to match. |
| 164 |
type: str |
| 165 |
sample: 192.0.1.0/24 |
| 166 |
returned: success |
| 167 |
src_port: |
| 168 |
description: |
| 169 |
- Port number of source port to match. |
| 170 |
type: str |
| 171 |
sample: 1234 |
| 172 |
returned: success |
| 173 |
dst_cidr: |
| 174 |
description: |
| 175 |
- CIDR notation of source IP address to match. |
| 176 |
type: str |
| 177 |
sample: 1.2.3.4/32 |
| 178 |
returned: success |
| 179 |
dst_port: |
| 180 |
description: |
| 181 |
- Port number of destination port to match. |
| 182 |
type: str |
| 183 |
sample: 80 |
| 184 |
returned: success |
| 185 |
vlan: |
| 186 |
description: |
| 187 |
- Incoming traffic VLAN. |
| 188 |
type: str |
| 189 |
sample: 100 |
| 190 |
returned: success |
| 191 |
''' |
| 192 |
|
| 193 |
from ansible.module_utils.basic import AnsibleModule, json |
| 194 |
from ansible.module_utils.common.dict_transformations import recursive_diff |
| 195 |
from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec |
| 196 |
from copy import deepcopy |
| 197 |
|
| 198 |
|
| 199 |
def construct_payload(params): |
| 200 |
payload = {'rules': []} |
| 201 |
for rule in params['rules']: |
| 202 |
new_rule = dict() |
| 203 |
if 'comment' in rule: |
| 204 |
new_rule['comment'] = rule['comment'] |
| 205 |
if 'policy' in rule: |
| 206 |
new_rule['policy'] = rule['policy'] |
| 207 |
if 'ip_version' in rule: |
| 208 |
new_rule['ipVersion'] = rule['ip_version'] |
| 209 |
if 'protocol' in rule: |
| 210 |
new_rule['protocol'] = rule['protocol'] |
| 211 |
if 'src_cidr' in rule: |
| 212 |
new_rule['srcCidr'] = rule['src_cidr'] |
| 213 |
if 'src_port' in rule: |
| 214 |
try: # Need to convert to int for comparison later |
| 215 |
new_rule['srcPort'] = int(rule['src_port']) |
| 216 |
except ValueError: |
| 217 |
pass |
| 218 |
if 'dst_cidr' in rule: |
| 219 |
new_rule['dstCidr'] = rule['dst_cidr'] |
| 220 |
if 'dst_port' in rule: |
| 221 |
try: # Need to convert to int for comparison later |
| 222 |
new_rule['dstPort'] = int(rule['dst_port']) |
| 223 |
except ValueError: |
| 224 |
pass |
| 225 |
if 'vlan' in rule: |
| 226 |
try: # Need to convert to int for comparison later |
| 227 |
new_rule['vlan'] = int(rule['vlan']) |
| 228 |
except ValueError: |
| 229 |
pass |
| 230 |
payload['rules'].append(new_rule) |
| 231 |
return payload |
| 232 |
|
| 233 |
|
| 234 |
def main(): |
| 235 |
# define the available arguments/parameters that a user can pass to |
| 236 |
# the module |
| 237 |
|
| 238 |
rules_arg_spec = dict(comment=dict(type='str'), |
| 239 |
policy=dict(type='str', choices=['allow', 'deny']), |
| 240 |
ip_version=dict(type='str', choices=['ipv4', 'ipv6', 'any']), |
| 241 |
protocol=dict(type='str', choices=['tcp', 'udp', 'any']), |
| 242 |
src_cidr=dict(type='str'), |
| 243 |
src_port=dict(type='str'), |
| 244 |
dst_cidr=dict(type='str'), |
| 245 |
dst_port=dict(type='str'), |
| 246 |
vlan=dict(type='str'), |
| 247 |
) |
| 248 |
|
| 249 |
argument_spec = meraki_argument_spec() |
| 250 |
argument_spec.update(state=dict(type='str', choices=['absent', 'present', 'query'], default='query'), |
| 251 |
net_name=dict(type='str', aliases=['network']), |
| 252 |
net_id=dict(type='str'), |
| 253 |
rules=dict(type='list', elements='dict', options=rules_arg_spec), |
| 254 |
) |
| 255 |
|
| 256 |
# the AnsibleModule object will be our abstraction working with Ansible |
| 257 |
# this includes instantiation, a couple of common attr would be the |
| 258 |
# args/params passed to the execution, as well as if the module |
| 259 |
# supports check mode |
| 260 |
module = AnsibleModule(argument_spec=argument_spec, |
| 261 |
supports_check_mode=True, |
| 262 |
) |
| 263 |
meraki = MerakiModule(module, function='switch_access_list') |
| 264 |
|
| 265 |
meraki.params['follow_redirects'] = 'all' |
| 266 |
|
| 267 |
query_url = {'switch_access_list': '/networks/{net_id}/switch/accessControlLists'} |
| 268 |
update_url = {'switch_access_list': '/networks/{net_id}/switch/accessControlLists'} |
| 269 |
|
| 270 |
meraki.url_catalog['get_all'].update(query_url) |
| 271 |
meraki.url_catalog['update'] = update_url |
| 272 |
|
| 273 |
org_id = meraki.params['org_id'] |
| 274 |
if org_id is None: |
| 275 |
org_id = meraki.get_org_id(meraki.params['org_name']) |
| 276 |
net_id = meraki.params['net_id'] |
| 277 |
if net_id is None: |
| 278 |
nets = meraki.get_nets(org_id=org_id) |
| 279 |
net_id = meraki.get_net_id(net_name=meraki.params['net_name'], data=nets) |
| 280 |
|
| 281 |
if meraki.params['state'] == 'query': |
| 282 |
path = meraki.construct_path('get_all', net_id=net_id) |
| 283 |
result = meraki.request(path, method='GET') |
| 284 |
if meraki.status == 200: |
| 285 |
meraki.result['data'] = result |
| 286 |
elif meraki.params['state'] == 'present': |
| 287 |
path = meraki.construct_path('get_all', net_id=net_id) |
| 288 |
original = meraki.request(path, method='GET') |
| 289 |
payload = construct_payload(meraki.params) |
| 290 |
comparable = deepcopy(original) |
| 291 |
if len(comparable['rules']) > 1: |
| 292 |
del comparable['rules'][len(comparable['rules']) - 1] # Delete the default rule for comparison |
| 293 |
else: |
| 294 |
del comparable['rules'][0] |
| 295 |
if meraki.is_update_required(comparable, payload): |
| 296 |
if meraki.check_mode is True: |
| 297 |
default_rule = original['rules'][len(original['rules']) - 1] |
| 298 |
payload['rules'].append(default_rule) |
| 299 |
new_rules = {'rules': payload['rules']} |
| 300 |
meraki.result['data'] = new_rules |
| 301 |
meraki.result['changed'] = True |
| 302 |
diff = recursive_diff(original, new_rules) |
| 303 |
meraki.result['diff'] = {'before': diff[0], |
| 304 |
'after': diff[1]} |
| 305 |
meraki.exit_json(**meraki.result) |
| 306 |
path = meraki.construct_path('update', net_id=net_id) |
| 307 |
response = meraki.request(path, method='PUT', payload=json.dumps(payload)) |
| 308 |
if meraki.status == 200: |
| 309 |
diff = recursive_diff(original, payload) |
| 310 |
meraki.result['data'] = response |
| 311 |
meraki.result['changed'] = True |
| 312 |
meraki.result['diff'] = {'before': diff[0], |
| 313 |
'after': diff[1]} |
| 314 |
else: |
| 315 |
meraki.result['data'] = original |
| 316 |
|
| 317 |
# in the event of a successful module execution, you will want to |
| 318 |
# simple AnsibleModule.exit_json(), passing the key/value results |
| 319 |
meraki.exit_json(**meraki.result) |
| 320 |
|
| 321 |
|
| 322 |
if __name__ == '__main__': |
| 323 |
main() |