Debian Codegraph @

ansible_collections/cisco/ios/plugins/modules/ios_acl_interfaces.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Copyright 2022 Red Hat
4 # GNU General Public License v3.0+
5 # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 """
8 The module file for ios_acl_interfaces
9 """
10
11 from __future__ import absolute_import, division, print_function
12
13
14 __metaclass__ = type
15
16 DOCUMENTATION = """
17 module: ios_acl_interfaces
18 short_description: Resource module to configure ACL interfaces.
19 description:
20 This module configures and manages the access-control (ACL) attributes
21 of interfaces on IOS platforms.
22 version_added: 1.0.0
23 author:
24 - Sumit Jaiswal (@justjais)
25 - Sagar Paul (@KB-perByte)
26 notes:
27 - Tested against Cisco IOSXE Version 17.3 on CML.
28 - This module works with connection C(network_cli).
29 See U(https://docs.ansible.com/ansible/latest/network/user_guide/platform_ios.html)
30 options:
31 config:
32 description: A dictionary of ACL interfaces options
33 type: list
34 elements: dict
35 suboptions:
36 name:
37 description:
38 Full name of the interface excluding any logical unit number,
39 i.e. GigabitEthernet0/1.
40 type: str
41 required: true
42 access_groups:
43 description: Specify access-group for IP access list (standard or extended).
44 type: list
45 elements: dict
46 suboptions:
47 afi:
48 description: Specifies the AFI for the ACLs to be configured on this interface.
49 type: str
50 required: true
51 choices:
52 - ipv4
53 - ipv6
54 acls:
55 description: Specifies the ACLs for the provided AFI.
56 type: list
57 elements: dict
58 suboptions:
59 name:
60 description: Specifies the name of the IPv4/IPv4 ACL for the interface.
61 type: str
62 required: true
63 direction:
64 description:
65 - Specifies the direction of packets that the ACL will be applied
66 on.
67 - With one direction already assigned, other acl direction cannot
68 be same.
69 type: str
70 required: true
71 choices:
72 - in
73 - out
74 running_config:
75 description:
76 - The module, by default, will connect to the remote device and retrieve the current
77 running-config to use as a base for comparing against the contents of source.
78 There are times when it is not desirable to have the task get the current running-config
79 for every task in a playbook. The I(running_config) argument allows the implementer
80 to pass in the configuration to use as the base config for comparison. This
81 value of this option should be the output received from device by executing
82 command.
83 type: str
84 state:
85 description:
86 - The state the configuration should be left in
87 - The states I(rendered), I(gathered) and I(parsed) does not perform any change
88 on the device.
89 - The state I(rendered) will transform the configuration in C(config) option to
90 platform specific CLI commands which will be returned in the I(rendered) key
91 within the result. For state I(rendered) active connection to remote host is
92 not required.
93 - The state I(gathered) will fetch the running configuration from device and transform
94 it into structured data in the format as per the resource module argspec and
95 the value is returned in the I(gathered) key within the result.
96 - The state I(parsed) reads the configuration from C(running_config) option and
97 transforms it into JSON format as per the resource module parameters and the
98 value is returned in the I(parsed) key within the result. The value of C(running_config)
99 option should be the same format as the output of command I(show running-config |
100 include ^interface|ip access-group|ipv6 traffic-filter) executed on device.
101 For state I(parsed) active
102 connection to remote host is not required.
103 type: str
104 choices:
105 - merged
106 - replaced
107 - overridden
108 - deleted
109 - gathered
110 - parsed
111 - rendered
112 default: merged
113 """
114
115 EXAMPLES = """
116 # Using Merged
117
118 # Before state:
119 # -------------
120 #
121 # vios#sh running-config | include interface|ip access-group|ipv6 traffic-filter
122 # interface Loopback888
123 # interface GigabitEthernet0/0
124 # interface GigabitEthernet0/1
125 # interface GigabitEthernet0/2
126 # ip access-group 123 out
127
128 - name: Merge module attributes of given access-groups
129 cisco.ios.ios_acl_interfaces:
130 config:
131 - name: GigabitEthernet0/1
132 access_groups:
133 - afi: ipv4
134 acls:
135 - name: 110
136 direction: in
137 - name: 123
138 direction: out
139 - afi: ipv6
140 acls:
141 - name: test_v6
142 direction: out
143 - name: temp_v6
144 direction: in
145 - name: GigabitEthernet0/2
146 access_groups:
147 - afi: ipv4
148 acls:
149 - name: 100
150 direction: in
151 state: merged
152
153 # Commands Fired:
154 # ---------------
155 #
156 # interface GigabitEthernet0/1
157 # ip access-group 110 in
158 # ip access-group 123 out
159 # ipv6 traffic-filter test_v6 out
160 # ipv6 traffic-filter temp_v6 in
161 # interface GigabitEthernet0/2
162 # ip access-group 100 in
163
164 # After state:
165 # -------------
166 #
167 # vios#sh running-config | include interface|ip access-group|ipv6 traffic-filter
168 # interface Loopback888
169 # interface GigabitEthernet0/0
170 # interface GigabitEthernet0/1
171 # ip access-group 110 in
172 # ip access-group 123 out
173 # ipv6 traffic-filter test_v6 out
174 # ipv6 traffic-filter temp_v6 in
175 # interface GigabitEthernet0/2
176 # ip access-group 110 in
177 # ip access-group 123 out
178
179 # Using Replaced
180
181 # Before state:
182 # -------------
183 #
184 # vios#sh running-config | include interface|ip access-group|ipv6 traffic-filter
185 # interface Loopback888
186 # interface GigabitEthernet0/0
187 # interface GigabitEthernet0/1
188 # ip access-group 110 in
189 # ip access-group 123 out
190 # ipv6 traffic-filter test_v6 out
191 # ipv6 traffic-filter temp_v6 in
192 # interface GigabitEthernet0/2
193 # ip access-group 110 in
194 # ip access-group 123 out
195
196 - name: Replace module attributes of given access-groups
197 cisco.ios.ios_acl_interfaces:
198 config:
199 - name: GigabitEthernet0/1
200 access_groups:
201 - afi: ipv4
202 acls:
203 - name: 100
204 direction: out
205 - name: 110
206 direction: in
207 state: replaced
208
209 # Commands Fired:
210 # ---------------
211 #
212 # interface GigabitEthernet0/1
213 # no ip access-group 123 out
214 # no ipv6 traffic-filter temp_v6 in
215 # no ipv6 traffic-filter test_v6 out
216 # ip access-group 100 out
217
218 # After state:
219 # -------------
220 #
221 # vios#sh running-config | include interface|ip access-group|ipv6 traffic-filter
222 # interface Loopback888
223 # interface GigabitEthernet0/0
224 # interface GigabitEthernet0/1
225 # ip access-group 100 out
226 # ip access-group 110 in
227 # interface GigabitEthernet0/2
228 # ip access-group 110 in
229 # ip access-group 123 out
230
231 # Using Overridden
232
233 # Before state:
234 # -------------
235 #
236 # vios#sh running-config | include interface|ip access-group|ipv6 traffic-filter
237 # interface Loopback888
238 # interface GigabitEthernet0/0
239 # interface GigabitEthernet0/1
240 # ip access-group 110 in
241 # ip access-group 123 out
242 # ipv6 traffic-filter test_v6 out
243 # ipv6 traffic-filter temp_v6 in
244 # interface GigabitEthernet0/2
245 # ip access-group 110 in
246 # ip access-group 123 out
247
248 - name: Overridden module attributes of given access-groups
249 cisco.ios.ios_acl_interfaces:
250 config:
251 - name: GigabitEthernet0/1
252 access_groups:
253 - afi: ipv4
254 acls:
255 - name: 100
256 direction: out
257 - name: 110
258 direction: in
259 state: overridden
260
261 # Commands Fired:
262 # ---------------
263 #
264 # interface GigabitEthernet0/1
265 # no ip access-group 123 out
266 # no ipv6 traffic-filter test_v6 out
267 # no ipv6 traffic-filter temp_v6 in
268 # ip access-group 100 out
269 # interface GigabitEthernet0/2
270 # no ip access-group 110 in
271 # no ip access-group 123 out
272
273 # After state:
274 # -------------
275 #
276 # vios#sh running-config | include interface|ip access-group|ipv6 traffic-filter
277 # interface Loopback888
278 # interface GigabitEthernet0/0
279 # interface GigabitEthernet0/1
280 # ip access-group 100 out
281 # ip access-group 110 in
282 # interface GigabitEthernet0/2
283
284 # Using Deleted
285
286 # Before state:
287 # -------------
288 #
289 # vios#sh running-config | include interface|ip access-group|ipv6 traffic-filter
290 # interface Loopback888
291 # interface GigabitEthernet0/0
292 # interface GigabitEthernet0/1
293 # ip access-group 110 in
294 # ip access-group 123 out
295 # ipv6 traffic-filter test_v6 out
296 # ipv6 traffic-filter temp_v6 in
297 # interface GigabitEthernet0/2
298 # ip access-group 110 in
299 # ip access-group 123 out
300
301 - name: Delete module attributes of given Interface
302 cisco.ios.ios_acl_interfaces:
303 config:
304 - name: GigabitEthernet0/1
305 state: deleted
306
307 # Commands Fired:
308 # ---------------
309 #
310 # interface GigabitEthernet0/1
311 # no ip access-group 110 in
312 # no ip access-group 123 out
313 # no ipv6 traffic-filter test_v6 out
314 # no ipv6 traffic-filter temp_v6 in
315
316 # After state:
317 # -------------
318 #
319 # vios#sh running-config | include interface|ip access-group|ipv6 traffic-filter
320 # interface Loopback888
321 # interface GigabitEthernet0/0
322 # interface GigabitEthernet0/1
323 # interface GigabitEthernet0/2
324 # ip access-group 110 in
325 # ip access-group 123 out
326
327 # Using DELETED without any config passed
328 # "(NOTE: This will delete all of configured resource module attributes from each configured interface)"
329
330 # Before state:
331 # -------------
332 #
333 # vios#sh running-config | include interface|ip access-group|ipv6 traffic-filter
334 # interface Loopback888
335 # interface GigabitEthernet0/0
336 # interface GigabitEthernet0/1
337 # ip access-group 110 in
338 # ip access-group 123 out
339 # ipv6 traffic-filter test_v6 out
340 # ipv6 traffic-filter temp_v6 in
341 # interface GigabitEthernet0/2
342 # ip access-group 110 in
343 # ip access-group 123 out
344
345 - name: Delete module attributes of given access-groups from ALL Interfaces
346 cisco.ios.ios_acl_interfaces:
347 config:
348 state: deleted
349
350 # Commands Fired:
351 # ---------------
352 #
353 # interface GigabitEthernet0/1
354 # no ip access-group 110 in
355 # no ip access-group 123 out
356 # no ipv6 traffic-filter test_v6 out
357 # no ipv6 traffic-filter temp_v6 in
358 # interface GigabitEthernet0/2
359 # no ip access-group 110 out
360 # no ip access-group 123 out
361
362 # After state:
363 # -------------
364 #
365 # vios#sh running-config | include interface|ip access-group|ipv6 traffic-filter
366 # interface Loopback888
367 # interface GigabitEthernet0/0
368 # interface GigabitEthernet0/1
369 # interface GigabitEthernet0/2
370
371 # Using Gathered
372
373 # Before state:
374 # -------------
375 #
376 # vios#sh running-config | include interface|ip access-group|ipv6 traffic-filter
377 # interface Loopback888
378 # interface GigabitEthernet0/0
379 # interface GigabitEthernet0/1
380 # ip access-group 110 in
381 # ip access-group 123 out
382 # ipv6 traffic-filter test_v6 out
383 # ipv6 traffic-filter temp_v6 in
384 # interface GigabitEthernet0/2
385 # ip access-group 110 in
386 # ip access-group 123 out
387
388 - name: Gather listed acl interfaces with provided configurations
389 cisco.ios.ios_acl_interfaces:
390 config:
391 state: gathered
392
393 # Module Execution Result:
394 # ------------------------
395 #
396 # "gathered": [
397 # {
398 # "name": "Loopback888"
399 # },
400 # {
401 # "name": "GigabitEthernet0/0"
402 # },
403 # {
404 # "access_groups": [
405 # {
406 # "acls": [
407 # {
408 # "direction": "in",
409 # "name": "110"
410 # },
411 # {
412 # "direction": "out",
413 # "name": "123"
414 # }
415 # ],
416 # "afi": "ipv4"
417 # },
418 # {
419 # "acls": [
420 # {
421 # "direction": "in",
422 # "name": "temp_v6"
423 # },
424 # {
425 # "direction": "out",
426 # "name": "test_v6"
427 # }
428 # ],
429 # "afi": "ipv6"
430 # }
431 # ],
432 # "name": "GigabitEthernet0/1"
433 # },
434 # {
435 # "access_groups": [
436 # {
437 # "acls": [
438 # {
439 # "direction": "in",
440 # "name": "100"
441 # },
442 # {
443 # "direction": "out",
444 # "name": "123"
445 # }
446 # ],
447 # "afi": "ipv4"
448 # }
449 # ],
450 # "name": "GigabitEthernet0/2"
451 # }
452 # ]
453
454 # After state:
455 # ------------
456 #
457 # vios#sh running-config | include interface|ip access-group|ipv6 traffic-filter
458 # interface Loopback888
459 # interface GigabitEthernet0/0
460 # interface GigabitEthernet0/1
461 # ip access-group 110 in
462 # ip access-group 123 out
463 # ipv6 traffic-filter test_v6 out
464 # ipv6 traffic-filter temp_v6 in
465 # interface GigabitEthernet0/2
466 # ip access-group 110 in
467 # ip access-group 123 out
468
469 # Using Rendered
470
471 - name: Render the commands for provided configuration
472 cisco.ios.ios_acl_interfaces:
473 config:
474 - name: GigabitEthernet0/1
475 access_groups:
476 - afi: ipv4
477 acls:
478 - name: 110
479 direction: in
480 - name: 123
481 direction: out
482 - afi: ipv6
483 acls:
484 - name: test_v6
485 direction: out
486 - name: temp_v6
487 direction: in
488 state: rendered
489
490 # Module Execution Result:
491 # ------------------------
492 #
493 # "rendered": [
494 # "interface GigabitEthernet0/1",
495 # "ip access-group 110 in",
496 # "ip access-group 123 out",
497 # "ipv6 traffic-filter temp_v6 in",
498 # "ipv6 traffic-filter test_v6 out"
499 # ]
500
501 # Using Parsed
502
503 # File: parsed.cfg
504 # ----------------
505 #
506 # interface GigabitEthernet0/1
507 # ip access-group 110 in
508 # ip access-group 123 out
509 # ipv6 traffic-filter temp_v6 in
510 # ipv6 traffic-filter test_v6 out
511
512 - name: Parse the commands for provided configuration
513 cisco.ios.ios_acl_interfaces:
514 running_config: "{{ lookup('file', 'parsed.cfg') }}"
515 state: parsed
516
517 # Module Execution Result:
518 # ------------------------
519 #
520 # "parsed": [
521 # {
522 # "access_groups": [
523 # {
524 # "acls": [
525 # {
526 # "direction": "in",
527 # "name": "110"
528 # }
529 # ],
530 # "afi": "ipv4"
531 # },
532 # {
533 # "acls": [
534 # {
535 # "direction": "in",
536 # "name": "temp_v6"
537 # }
538 # ],
539 # "afi": "ipv6"
540 # }
541 # ],
542 # "name": "GigabitEthernet0/1"
543 # }
544 # ]
545 """
546
547
548 RETURN = """
549 before:
550 description: The configuration prior to the module execution.
551 returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted) or C(purged)
552 type: dict
553 sample: >
554 This output will always be in the same format as the
555 module argspec.
556 after:
557 description: The resulting configuration after module execution.
558 returned: when changed
559 type: dict
560 sample: >
561 This output will always be in the same format as the
562 module argspec.
563 commands:
564 description: The set of commands pushed to the remote device.
565 returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted) or C(purged)
566 type: list
567 sample:
568 - "interface GigabitEthernet0/1"
569 - "no ip access-group 123 out"
570 - "no ipv6 traffic-filter test_v6 out"
571 rendered:
572 description: The provided configuration in the task rendered in device-native format (offline).
573 returned: when I(state) is C(rendered)
574 type: list
575 sample:
576 - "interface GigabitEthernet0/1"
577 - "no ip access-group 123 out"
578 - "no ipv6 traffic-filter test_v6 out"
579 gathered:
580 description: Facts about the network resource gathered from the remote device as structured data.
581 returned: when I(state) is C(gathered)
582 type: list
583 sample: >
584 This output will always be in the same format as the
585 module argspec.
586 parsed:
587 description: The device native config provided in I(running_config) option parsed into structured data as per module argspec.
588 returned: when I(state) is C(parsed)
589 type: list
590 sample: >
591 This output will always be in the same format as the
592 module argspec.
593 """
594
595 from ansible.module_utils.basic import AnsibleModule
596
597 from ansible_collections.cisco.ios.plugins.module_utils.network.ios.argspec.acl_interfaces.acl_interfaces import (
598 Acl_interfacesArgs,
599 )
600 from ansible_collections.cisco.ios.plugins.module_utils.network.ios.config.acl_interfaces.acl_interfaces import (
601 Acl_interfaces,
602 )
603
604
605 def main():
606 """
607 Main entry point for module execution
608
609 :returns: the result form module invocation
610 """
611 module = AnsibleModule(
612 argument_spec=Acl_interfacesArgs.argument_spec,
613 mutually_exclusive=[["config", "running_config"]],
614 required_if=[
615 ["state", "merged", ["config"]],
616 ["state", "replaced", ["config"]],
617 ["state", "overridden", ["config"]],
618 ["state", "rendered", ["config"]],
619 ["state", "parsed", ["running_config"]],
620 ],
621 supports_check_mode=True,
622 )
623
624 result = Acl_interfaces(module).execute_module()
625 module.exit_json(**result)
626
627
628 if __name__ == "__main__":
629 main()