Debian Codegraph @

ansible_collections/cisco/iosxr/plugins/modules/iosxr_prefix_lists.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 iosxr_prefix_lists
9 """
10
11 from __future__ import absolute_import, division, print_function
12
13
14 __metaclass__ = type
15
16 DOCUMENTATION = """
17 module: iosxr_prefix_lists
18 short_description: Resource module to configure prefix lists.
19 description:
20 - This module manages prefix-lists configuration on devices running Cisco IOSXR.
21 version_added: 2.3.0
22 notes:
23 - Tested against IOSXR 7.0.2.
24 - This module works with connection C(network_cli).
25 author: Ashwini Mhatre (@amhatre)
26 options:
27 running_config:
28 description:
29 - This option is used only with state I(parsed).
30 - The value of this option should be the output received from the Iosxr device by
31 executing the command B(show running-config prefix-list).
32 - The state I(parsed) reads the configuration from C(running_config) option and
33 transforms it into Ansible structured data as per the resource module's argspec
34 and the value is then returned in the I(parsed) key within the result.
35 type: str
36 config:
37 description: A list of prefix-lists configuration.
38 type: list
39 elements: dict
40 suboptions:
41 afi:
42 description:
43 - The Address Family Identifier (AFI) for the prefix-lists.
44 type: str
45 choices: ["ipv4", "ipv6"]
46 prefix_lists:
47 description: List of prefix-list configurations.
48 type: list
49 elements: dict
50 suboptions:
51 name:
52 description: Name of the prefix-list.
53 type: str
54 entries:
55 description: List of configurations for the specified prefix-list
56 type: list
57 elements: dict
58 suboptions:
59 sequence:
60 description: Sequence Number.
61 type: int
62 action:
63 description: Prefix-List permit or deny.
64 type: str
65 choices: ["permit", "deny", "remark"]
66 description:
67 description: Description of the prefix list. only applicable for action "remark".
68 type: str
69 prefix:
70 description: IP or IPv6 prefix in A.B.C.D/LEN or A:B::C:D/LEN format. only applicable for action "permit" and "deny"
71 type: str
72 eq:
73 description: Exact prefix length to be matched.
74 type: int
75 ge:
76 description: Minimum prefix length to be matched.
77 type: int
78 le:
79 description: Maximum prefix length to be matched.
80 type: int
81 state:
82 description:
83 - The state the configuration should be left in.
84 - Refer to examples for more details.
85 - With state I(replaced), for the listed prefix-lists,
86 sequences that are in running-config but not in the task are negated.
87 - With state I(overridden), all prefix-lists that are in running-config but
88 not in the task are negated.
89 - Please refer to examples for more details.
90 type: str
91 choices:
92 - merged
93 - replaced
94 - overridden
95 - deleted
96 - parsed
97 - gathered
98 - rendered
99 default: merged
100 """
101
102 EXAMPLES = """
103 # Using merged
104
105
106 # Before state
107 # RP/0/0/CPU0:10#show running-config
108 # Thu Feb 4 09:38:36.245 UTC
109 # % No such configuration item(s)
110 # RP/0/0/CPU0:10#
111 #
112
113
114 - name: Merge the provided configuration with the existing running configuration
115 cisco.iosxr.iosxr_prefix_lists:
116 state: merged
117 config:
118 - afi: ipv6
119 prefix_lists:
120 - name: pl_1
121 entries:
122 - prefix: '2001:db8:1234::/48'
123 action: deny
124 sequence: 1
125 - name: pl_2
126 entries:
127 - sequence: 2
128 action: remark
129 description: TEST_PL_2_REMARK
130 - afi: ipv4
131 prefix_lists:
132 - name: pl1
133 entries:
134 - sequence: 3
135 action: remark
136 description: TEST_PL1_2_REMARK
137 - sequence: 4
138 action: permit
139 prefix: 10.0.0.0/24
140 - name: pl2
141 entries:
142 - sequence: 5
143 action: remark
144 description: TEST_PL2_REMARK
145 - name: pl3
146 entries:
147 - sequence: 6
148 action: permit
149 prefix: 35.0.0.0/8
150 eq: 0
151
152 # Task Output
153 # -------------
154 # before: []
155 # commands:
156 # - ipv6 prefix-list pl_1 1 deny 2001:db8:1234::/48
157 # - ipv6 prefix-list pl_2 2 remark TEST_PL_2_REMARK
158 # - ipv4 prefix-list pl1 3 remark TEST_PL1_2_REMARK
159 # - ipv4 prefix-list pl1 4 permit 10.0.0.0/24
160 # - ipv4 prefix-list pl2 5 remark TEST_PL2_REMARK
161 # - ipv4 prefix-list pl3 6 permit 35.0.0.0/8 eq 0
162 # after:
163 # - afi: ipv6
164 # prefix_lists:
165 # - name: pl_1
166 # entries:
167 # - prefix: 2001:db8:1234::/48
168 # action: deny
169 # sequence: 1
170 # - name: pl_2
171 # entries:
172 # - sequence: 2
173 # action: remark
174 # description: TEST_PL_2_REMARK
175 # - afi: ipv4
176 # prefix_lists:
177 # - name: pl1
178 # entries:
179 # - sequence: 3
180 # action: remark
181 # description: TEST_PL1_2_REMARK
182 # - sequence: 4
183 # action: permit
184 # prefix: 10.0.0.0/24
185 # - name: pl2
186 # entries:
187 # - sequence: 5
188 # action: remark
189 # description: TEST_PL2_REMARK
190 # - name: pl3
191 # entries:
192 # - sequence: 6
193 # action: permit
194 # prefix: 35.0.0.0/8
195 # eq: 0
196
197
198 # After state:
199 # ------------
200 # RP/0/0/CPU0:10#show running-config
201 # ipv6 prefix-list pl_1
202 # 1 deny 2001:db8:1234::/48
203 # !
204 # ipv6 prefix-list pl_2
205 # 2 remark TEST_PL_2_REMAR
206 # !
207 # ipv4 prefix-list pl1
208 # 3 remark TEST_PL1_2_REMARK
209 # 4 permit 10.0.0.0/24
210 # !
211 # ipv4 prefix-list pl2
212 # 5 remark TEST_PL2_REMARK
213 # !
214 # ipv4 prefix-list pl3
215 # 6 permit 35.0.0.0/8 eq 0
216 # !
217
218
219 # Using replaced:
220
221
222 # Before state:
223 # -------------
224 # RP/0/0/CPU0:10#show running-config
225 #
226 # ipv6 prefix-list pl_1
227 # 1 deny 2001:db8:1234::/48
228 # !
229 # ipv6 prefix-list pl_2
230 # 2 remark TEST_PL_2_REMARK
231 # !
232 # ipv4 prefix-list pl1
233 # 3 remark TEST_PL1_2_REMARK
234 # 4 permit 10.0.0.0/24
235 # !
236 # ipv4 prefix-list pl2
237 # 5 remark TEST_PL2_REMARK
238 # !
239 #
240
241
242 - name: >-
243 Replace device configurations of listed prefix lists with provided
244 configurations
245 register: result
246 cisco.iosxr.iosxr_prefix_lists:
247 config:
248 - afi: ipv4
249 prefix_lists:
250 - name: pl1
251 entries:
252 - sequence: 3
253 action: permit
254 prefix: 10.0.0.0/24
255 - afi: ipv6
256 prefix_lists:
257 - name: pl_1
258 entries:
259 - prefix: '2001:db8:1234::/48'
260 action: permit
261 sequence: 1
262 - name: pl_2
263 entries:
264 - sequence: 2
265 action: remark
266 description: TEST_PL1_2
267 state: replaced
268
269
270 # Task Output
271 # -------------
272 # before:
273 # - afi: ipv6
274 # prefix_lists:
275 # - entries:
276 # - action: deny
277 # prefix: 2001:db8:1234::/48
278 # sequence: 1
279 # name: pl_1
280 # - entries:
281 # - action: remark
282 # description: TEST_PL_2_REMARK
283 # sequence: 2
284 # name: pl_2
285 # - afi: ipv4
286 # prefix_lists:
287 # - entries:
288 # - action: remark
289 # description: TEST_PL1_2_REMARK
290 # sequence: 3
291 # - action: permit
292 # prefix: 10.0.0.0/24
293 # sequence: 4
294 # name: pl1
295 # - entries:
296 # - action: remark
297 # description: TEST_PL2_REMARK
298 # sequence: 5
299 # name: pl2
300 # commands:
301 # - no ipv4 prefix-list pl1 3 remark TEST_PL1_2_REMARK
302 # - no ipv4 prefix-list pl1 4 permit 10.0.0.0/24
303 # - ipv4 prefix-list pl1 3 permit 10.0.0.0/24
304 # - ipv6 prefix-list pl_2 2 remark TEST_PL1_2
305 # after:
306 # - afi: ipv6
307 # prefix_lists:
308 # - entries:
309 # - action: deny
310 # prefix: 2001:db8:1234::/48
311 # sequence: 1
312 # name: pl_1
313 # - entries:
314 # - action: remark
315 # description: TEST_PL1_2
316 # sequence: 2
317 # name: pl_2
318 # - afi: ipv4
319 # prefix_lists:
320 # - entries:
321 # - action: permit
322 # prefix: 10.0.0.0/24
323 # sequence: 3
324 # name: pl1
325 # - entries:
326 # - action: remark
327 # description: TEST_PL2_REMARK
328 # sequence: 5
329 # name: pl2
330
331
332 # After state:
333 # RP/0/0/CPU0:10#show running-config
334 #
335 # ipv6 prefix-list pl_1
336 # 1 deny 2001:db8:1234::/48
337 # !
338 # ipv6 prefix-list pl_2
339 # 2 remark TEST_PL1_2
340 # !
341 # ipv4 prefix-list pl1
342 # 3 permit 10.0.0.0/24
343 # !
344 # ipv4 prefix-list pl2
345 # 5 remark TEST_PL2_REMARK
346 #
347 # Module Execution:
348 #
349
350
351 # Using deleted:
352
353
354 # Before state:
355 # -------------
356 # RP/0/0/CPU0:10#show running-config
357 #
358 # ipv6 prefix-list pl_1
359 # 1 deny 2001:db8:1234::/48
360 # !
361 # ipv6 prefix-list pl_2
362 # 2 remark TEST_PL_2_REMARK
363 # !
364 # ipv4 prefix-list pl1
365 # 3 remark TEST_PL1_2_REMARK
366 # 4 permit 10.0.0.0/24
367 # !
368 # ipv4 prefix-list pl2
369 # 5 remark TEST_PL2_REMARK
370 # ipv4 prefix-list pl3
371 # 6 permit 35.0.0.0/8 eq 0
372
373 - name: Delete all prefix-lists from the device
374 cisco.iosxr.iosxr_prefix_lists:
375 state: deleted
376
377 # Task Output
378 # -------------
379 # before:
380 # - afi: ipv6
381 # prefix_lists:
382 # - name: pl_1
383 # entries:
384 # - prefix: 2001:db8:1234::/48
385 # action: deny
386 # sequence: 1
387 # - name: pl_2
388 # entries:
389 # - sequence: 2
390 # action: remark
391 # description: TEST_PL_2_REMARK
392 # - afi: ipv4
393 # prefix_lists:
394 # - name: pl1
395 # entries:
396 # - sequence: 3
397 # action: remark
398 # description: TEST_PL1_2_REMARK
399 # - sequence: 4
400 # action: permit
401 # prefix: 10.0.0.0/24
402 # - name: pl2
403 # entries:
404 # - sequence: 5
405 # action: remark
406 # description: TEST_PL2_REMARK
407 # - name: pl3
408 # entries:
409 # - sequence: 6
410 # action: permit
411 # prefix: 35.0.0.0/8
412 # eq: 0
413 # commands:
414 # - no ipv6 prefix-list pl_1
415 # - no ipv6 prefix-list pl_2
416 # - no ipv4 prefix-list pl1
417 # - no ipv4 prefix-list pl2
418 # - no ipv4 prefix-list pl3
419 # after: []
420
421
422 # After state:
423 # RP/0/0/CPU0:10#show running-config
424 #
425
426 # using gathered:
427
428
429 # After state:
430 # ------------
431 # RP/0/0/CPU0:10#show running-config
432 # ipv6 prefix-list pl_1
433 # 1 deny 2001:db8:1234::/48
434 # !
435 # ipv6 prefix-list pl_2
436 # 2 remark TEST_PL_2_REMARK
437 # !
438 # ipv4 prefix-list pl1
439 # 3 remark TEST_PL1_2_REMARK
440 # 4 permit 10.0.0.0/24
441 # !
442 # ipv4 prefix-list pl2
443 # 5 remark TEST_PL2_REMARK
444 # !
445 # ipv4 prefix-list pl3
446 # 6 permit 35.0.0.0/8 eq 0
447 # !
448
449
450 - name: Gather ACL interfaces facts using gathered state
451 cisco.iosxr.iosxr_prefix_lists:
452 state: gathered
453
454 # gathered:
455 # - afi: ipv6
456 # prefix_lists:
457 # - name: pl_1
458 # entries:
459 # - prefix: 2001:db8:1234::/48
460 # action: deny
461 # sequence: 1
462 # - name: pl_2
463 # entries:
464 # - sequence: 2
465 # action: remark
466 # description: TEST_PL_2_REMARK
467 # - afi: ipv4
468 # prefix_lists:
469 # - name: pl1
470 # entries:
471 # - sequence: 3
472 # action: remark
473 # description: TEST_PL1_2_REMARK
474 # - sequence: 4
475 # action: permit
476 # prefix: 10.0.0.0/24
477 # - name: pl2
478 # entries:
479 # - sequence: 5
480 # action: remark
481 # description: TEST_PL2_REMARK
482 # - name: pl3
483 # entries:
484 # - sequence: 6
485 # action: permit
486 # prefix: 35.0.0.0/8
487 # eq: 0
488
489
490 # Using parsed:
491
492
493 # parsed.cfg
494 # ------------------------------
495 # ipv6 prefix-list pl_1
496 # 1 deny 2001:db8:1234::/48
497 # !
498 # ipv6 prefix-list pl_2
499 # 2 remark TEST_PL_2_REMARK
500 # !
501 # ipv4 prefix-list pl1
502 # 3 remark TEST_PL1_2_REMARK
503 # 4 permit 10.0.0.0/24
504 # !
505 # ipv4 prefix-list pl2
506 # 5 remark TEST_PL2_REMARK
507
508
509 - name: Parse externally provided Prefix_lists config to agnostic model
510 cisco.iosxr.iosxr_prefix_lists:
511 running_config: '{{ lookup(''file'', ''./fixtures/parsed.cfg'') }}'
512 state: parsed
513
514
515 # Task Output
516 # -------------
517 # parsed:
518 # - afi: ipv6
519 # prefix_lists:
520 # - name: pl_1
521 # entries:
522 # - prefix: 2001:db8:1234::/48
523 # action: deny
524 # sequence: 1
525 # - name: pl_2
526 # entries:
527 # - sequence: 2
528 # action: remark
529 # description: TEST_PL_2_REMARK
530 # - afi: ipv4
531 # prefix_lists:
532 # - name: pl1
533 # entries:
534 # - sequence: 3
535 # action: remark
536 # description: TEST_PL1_2_REMARK
537 # - sequence: 4
538 # action: permit
539 # prefix: 10.0.0.0/24
540 # - name: pl2
541 # entries:
542 # - sequence: 5
543 # action: remark
544 # description: TEST_PL2_REMARK
545 # - sequence: 6
546 # action: permit
547 # prefix: 35.0.0.0/8
548 # eq: 0
549
550
551 # Using rendered:
552
553
554 - name: Render platform specific commands from task input using rendered state
555 register: result
556 cisco.iosxr.iosxr_prefix_lists:
557 config:
558 - afi: ipv6
559 prefix_lists:
560 - name: pl_1
561 entries:
562 - prefix: '2001:db8:1234::/48'
563 action: deny
564 sequence: 1
565 - name: pl_2
566 entries:
567 - sequence: 2
568 action: remark
569 description: TEST_PL_2_REMARK
570 - afi: ipv4
571 prefix_lists:
572 - name: pl1
573 entries:
574 - sequence: 3
575 action: remark
576 description: TEST_PL1_2_REMARK
577 - sequence: 4
578 action: permit
579 prefix: 10.0.0.0/24
580 - name: pl2
581 entries:
582 - sequence: 5
583 action: remark
584 description: TEST_PL2_REMARK
585 - sequence: 6
586 action: permit
587 prefix: 35.0.0.0/8
588 eq: 0
589 state: rendered
590
591
592 # Task Output
593 # -------------
594 # "rendered": [
595 # "ipv6 prefix-list pl_1 1 deny 2001:db8:1234::/48",
596 # "ipv6 prefix-list pl_2 2 remark TEST_PL_2_REMARK",
597 # "ipv4 prefix-list pl1 3 remark TEST_PL1_2_REMARK",
598 # "ipv4 prefix-list pl1 4 permit 10.0.0.0/24",
599 # "ipv4 prefix-list pl2 5 remark TEST_PL2_REMARK",
600 # "ipv4 prefix-list pl2 6 permit 35.0.0.0/8 eq 0"
601 # ]
602
603 # Using overridden:
604
605
606 # Before state:
607 # -------------
608 # RP/0/0/CPU0:10#show running-config
609 #
610 # ipv6 prefix-list pl_1
611 # 1 deny 2001:db8:1234::/48
612 # !
613 # ipv6 prefix-list pl_2
614 # 2 remark TEST_PL_2_REMARK
615 # !
616 # ipv4 prefix-list pl1
617 # 3 remark TEST_PL1_2_REMARK
618 # 4 permit 10.0.0.0/24
619 # !
620 # ipv4 prefix-list pl2
621 # 5 remark TEST_PL2_REMARK
622 #
623 - name: Overridde all Prefix_lists configuration with provided configuration
624 cisco.iosxr.iosxr_prefix_lists:
625 config:
626 - afi: ipv4
627 prefix_lists:
628 - name: pl3
629 entries:
630 - sequence: 3
631 action: remark
632 description: TEST_PL1_3_REMARK
633 - sequence: 4
634 action: permit
635 prefix: 10.0.0.0/24
636 - sequence: 6
637 action: permit
638 prefix: 35.0.0.0/8
639 eq: 0
640 state: overridden
641
642
643 # Task Output
644 # -------------
645 # before:
646 # - afi: ipv6
647 # prefix_lists:
648 # - entries:
649 # - action: deny
650 # prefix: 2001:db8:1234::/48
651 # sequence: 1
652 # name: pl_1
653 # - entries:
654 # - action: remark
655 # description: TEST_PL_2_REMARK
656 # sequence: 2
657 # name: pl_2
658 # - afi: ipv4
659 # prefix_lists:
660 # - entries:
661 # - action: remark
662 # description: TEST_PL1_2_REMARK
663 # sequence: 3
664 # - action: permit
665 # prefix: 10.0.0.0/24
666 # sequence: 4
667 # name: pl1
668 # - entries:
669 # - action: remark
670 # description: TEST_PL2_REMARK
671 # sequence: 5
672 # name: pl2
673 # commands:
674 # - no ipv6 prefix-list pl_1
675 # - no ipv6 prefix-list pl_2
676 # - no ipv4 prefix-list pl1
677 # - no ipv4 prefix-list pl2
678 # - ipv4 prefix-list pl3 3 remark TEST_PL1_3_REMARK
679 # - ipv4 prefix-list pl3 4 permit 10.0.0.0/24
680 # - ipv4 prefix-list pl3 6 permit 35.0.0.0/8 eq 0
681 # after:
682 # - afi: ipv4
683 # prefix_lists:
684 # - entries:
685 # - action: remark
686 # description: TEST_PL1_3_REMARK
687 # sequence: 3
688 # - action: permit
689 # prefix: 10.0.0.0/24
690 # sequence: 4
691 # - action: permit
692 # prefix: 35.0.0.0/8
693 # sequence: 6
694 # eq: 0
695 # name: pl3
696
697
698 # After state:
699 # RP/0/0/CPU0:10#show running-config
700 #
701 # ipv4 prefix-list pl3
702 # 3 remark TEST_PL1_3_REMARK
703 # 4 permit 10.0.0.0/24
704 # 6 permit 35.0.0.0/8 eq 0
705 # !
706 # !
707 """
708
709 RETURN = """
710 before:
711 description: The configuration prior to the module execution.
712 returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted) or C(purged)
713 type: dict
714 sample: >
715 This output will always be in the same format as the
716 module argspec.
717 after:
718 description: The resulting configuration after module execution.
719 returned: when changed
720 type: dict
721 sample: >
722 This output will always be in the same format as the
723 module argspec.
724 commands:
725 description: The set of commands pushed to the remote device.
726 returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted) or C(purged)
727 type: list
728 sample:
729 - "ipv6 prefix-list pl_1 1 deny 2001:db8:1234::/48"
730 - "ipv6 prefix-list pl_2 2 remark TEST_PL_2_REMARK"
731 - "ipv4 prefix-list pl1 3 remark TEST_PL1_2_REMARK"
732 - "ipv4 prefix-list pl1 4 permit 10.0.0.0/24"
733 - "ipv4 prefix-list pl2 5 remark TEST_PL2_REMARK"
734 rendered:
735 description: The provided configuration in the task rendered in device-native format (offline).
736 returned: when I(state) is C(rendered)
737 type: list
738 sample:
739 - "ipv6 prefix-list pl_1 1 deny 2001:db8:1234::/48"
740 - "ipv6 prefix-list pl_2 2 remark TEST_PL_2_REMARK"
741 - "ipv4 prefix-list pl1 3 remark TEST_PL1_2_REMARK"
742 - "ipv4 prefix-list pl1 4 permit 10.0.0.0/24"
743 - "ipv4 prefix-list pl2 5 remark TEST_PL2_REMARK"
744 gathered:
745 description: Facts about the network resource gathered from the remote device as structured data.
746 returned: when I(state) is C(gathered)
747 type: list
748 sample: >
749 This output will always be in the same format as the
750 module argspec.
751 parsed:
752 description: The device native config provided in I(running_config) option parsed into structured data as per module argspec.
753 returned: when I(state) is C(parsed)
754 type: list
755 sample: >
756 This output will always be in the same format as the
757 module argspec.
758 """
759
760 from ansible.module_utils.basic import AnsibleModule
761
762 from ansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.argspec.prefix_lists.prefix_lists import (
763 Prefix_listsArgs,
764 )
765 from ansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.config.prefix_lists.prefix_lists import (
766 Prefix_lists,
767 )
768
769
770 def main():
771 """
772 Main entry point for module execution
773
774 :returns: the result form module invocation
775 """
776 module = AnsibleModule(
777 argument_spec=Prefix_listsArgs.argument_spec,
778 mutually_exclusive=[["config", "running_config"]],
779 required_if=[
780 ["state", "merged", ["config"]],
781 ["state", "replaced", ["config"]],
782 ["state", "overridden", ["config"]],
783 ["state", "rendered", ["config"]],
784 ["state", "parsed", ["running_config"]],
785 ],
786 supports_check_mode=True,
787 )
788
789 result = Prefix_lists(module).execute_module()
790 module.exit_json(**result)
791
792
793 if __name__ == "__main__":
794 main()