Debian Codegraph @

ansible_collections/cisco/iosxr/plugins/modules/iosxr_vrf_global.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Copyright 2024 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_vrf_global
9 """
10
11 from __future__ import absolute_import, division, print_function
12
13
14 __metaclass__ = type
15
16 DOCUMENTATION = """
17 module: iosxr_vrf_global
18 short_description: Manages global VRF configuration.
19 description:
20 - This module manages VRF configurations on Cisco IOS-XR devices.
21 - It enables playbooks to handle either individual VRFs or the complete VRF collection.
22 - It also permits removing non-explicitly stated VRF definitions from the setup.
23 version_added: 9.0.0
24 author: Ruchi Pakhle (@Ruchip16)
25 notes:
26 - Tested against Cisco IOS-XR Version 9.0.0
27 - This module works with connection C(network_cli).
28 - See L(the IOS_XR Platform Options, https://github.com/ansible-collections/cisco.iosxr/blob/main/platform_guide.rst).
29 - The module examples uses callback plugin (stdout_callback = yaml) to generate task output in yaml format.
30 options:
31 config:
32 description: A dictionary of options for VRF configurations.
33 type: list
34 elements: dict
35 suboptions:
36 name:
37 description: Name of the VRF.
38 type: str
39 required: true
40 description:
41 description: A description for the VRF.
42 type: str
43 evpn_route_sync:
44 description: EVPN Instance VPN ID used to synchronize the VRF route(s).
45 type: int
46 fallback_vrf:
47 description: Fallback VRF name
48 type: str
49 mhost:
50 description: Multicast host stack options
51 type: dict
52 suboptions:
53 afi:
54 description: Address Family Identifier (AFI)
55 type: str
56 choices: ['ipv4', 'ipv6']
57 default_interface:
58 description: Default interface for multicast.
59 type: str
60 rd:
61 description: VPN Route Distinguisher (RD).
62 type: str
63 remote_route_filtering:
64 description: Enable/Disable remote route filtering per VRF
65 type: dict
66 suboptions:
67 disable:
68 description: Disable remote route filtering per VRF
69 type: bool
70 vpn:
71 description: VPN ID for the VRF
72 type: dict
73 suboptions:
74 id:
75 description: VPN ID for the VRF.
76 type: str
77 running_config:
78 description:
79 - This option is used only with state I(parsed).
80 - The value of this option should be the output received from the IOS-XR device by
81 executing the command B(show running-config vrf).
82 - The state I(parsed) reads the configuration from C(running_config) option and
83 transforms it into Ansible structured data as per the resource module's argspec
84 and the value is then returned in the I(parsed) key within the result.
85 type: str
86 state:
87 choices: [parsed, gathered, deleted, merged, replaced, rendered, overridden, purged]
88 default: merged
89 description:
90 - The state the configuration should be left in
91 - The states I(rendered), I(gathered) and I(parsed) does not perform any change
92 on the device.
93 - The state I(rendered) will transform the configuration in C(config) option to
94 platform specific CLI commands which will be returned in the I(rendered) key
95 within the result. For state I(rendered) active connection to remote host is
96 not required.
97 - The state I(gathered) will fetch the running configuration from device and transform
98 it into structured data in the format as per the resource module argspec and
99 the value is returned in the I(gathered) key within the result.
100 - The state I(parsed) reads the configuration from C(running_config) option and
101 transforms it into JSON format as per the resource module parameters and the
102 value is returned in the I(parsed) key within the result. The value of C(running_config)
103 option should be the same format as the output of command I(show running-config vrf).
104 connection to remote host is not required.
105 - The state I(purged) removes all the VRF configurations from the
106 target device. Use caution with this state.
107 - The state I(deleted) only removes the VRF attributes that this module
108 manages and does not negate the VRF completely. Thereby, preserving
109 address-family related configurations under VRF context.
110 - Refer to examples for more details.
111 type: str
112 """
113
114 EXAMPLES = """
115
116 # Using merged
117 #
118 # Before state:
119 # -------------
120 #
121 # RP/0/0/CPU0:iosxr-02#show running-config vrf
122 # Fri Feb 9 07:02:35.789 UTC
123 # !
124 # vrf test
125 #
126
127 - name: Merge provided configuration with device configuration
128 cisco.iosxr.iosxr_vrf_global:
129 config:
130 - name: VRF4
131 description: VRF4 Description
132 evpn_route_sync: 793
133 fallback_vrf: "test-vrf"
134 remote_route_filtering:
135 disable: "true"
136 rd: "3:4"
137 mhost:
138 afi: "ipv4"
139 default_interface: "Loopback0"
140 vpn:
141 id: "2:3"
142 state: merged
143
144 # Task Output:
145 # ------------
146 #
147 # before: []
148 #
149 # commands:
150 # - vrf VRF4
151 # - description VRF4 Description
152 # - evpn-route-sync 793
153 # - fallback-vrf test-vrf
154 # - mhost ipv4 default-interface Loopback0
155 # - rd 3:4
156 # - remote-route-filtering disable
157 # - vpn id 2:3
158 #
159 # after:
160 # - name: VRF4
161 # description: VRF4 Description
162 # evpn_route_sync: 793
163 # fallback_vrf: "test-vrf"
164 # mhost:
165 # afi: "ipv4"
166 # default_interface: "Loopback0"
167 # rd: "3:4"
168 # remote_route_filtering:
169 # disable: "true"
170 # vpn:
171 # id: "2:3"
172 #
173 # After state:
174 # ------------
175 #
176 # RP/0/0/CPU0:iosxr-02#show running-config vrf
177 # Sat Feb 20 03:49:43.618 UTC
178 # vrf VRF4
179 # description "VRF4 Description"
180 # mhost ipv4 default-interface Loopback0
181 # evpn-route-sync 793
182 # vpn id 2:3
183 # fallback-vrf "test-vrf"
184 # remote-route-filtering disable
185 # rd "3:4"
186
187 # Using replaced
188 #
189 # Before state:
190 # -------------
191 #
192 # RP/0/0/CPU0:iosxr-02#show running-config vrf
193 # Sat Feb 20 03:49:43.618 UTC
194 # vrf VRF4
195 # description "VRF4 Description"
196 # mhost ipv4 default-interface Loopback0
197 # evpn-route-sync 793
198 # vpn id 2:3
199 # fallback-vrf "test-vrf"
200 # remote-route-filtering disable
201 # rd "3:4"
202
203 - name: Replace the provided configuration with the existing running configuration
204 cisco.iosxr.iosxr_vrf_global:
205 config:
206 - name: VRF7
207 description: VRF7 description
208 evpn_route_sync: 398
209 fallback_vrf: "replaced-vrf"
210 remote_route_filtering:
211 disable: "true"
212 rd: "67:9"
213 mhost:
214 afi: "ipv4"
215 default_interface: "Loopback0"
216 vpn:
217 id: "4:5"
218 state: replaced
219
220 # Task Output:
221 # ------------
222 #
223 # before:
224 # - name: VRF4
225 # description: VRF4 Description
226 # evpn_route_sync: 793
227 # fallback_vrf: "test-vrf"
228 # mhost:
229 # afi: "ipv4"
230 # default_interface: "Loopback0"
231 # rd: "3:4"
232 # remote_route_filtering:
233 # disable: "true"
234 # vpn:
235 # id: "2:3"
236 #
237 # commands:
238 # - vrf VRF4
239 # - no vpn id 2:3
240 # - vrf VRF7
241 # - description VRF7 description
242 # - evpn-route-sync 398
243 # - fallback-vrf replaced-vrf
244 # - mhost ipv4 default-interface Loopback0
245 # - rd 6:9
246 # - remote-route-filtering disable
247 # - vpn id 4:5
248 #
249 # after:
250 # - name: VRF4
251 # description: VRF4 Description
252 # evpn_route_sync: 793
253 # fallback_vrf: "test-vrf"
254 # mhost:
255 # afi: "ipv4"
256 # default_interface: "Loopback0"
257 # rd: "3:4"
258 # remote_route_filtering:
259 # disable: "true"
260 # - name: VRF7
261 # description: VRF7 description
262 # evpn_route_sync: 398
263 # fallback_vrf: "replaced-vrf"
264 # remote_route_filtering:
265 # disable: true
266 # rd: "67:9"
267 # mhost:
268 # afi: "ipv4"
269 # default_interface: "Loopback0"
270 # vpn:
271 # id: "4:5"
272 #
273 # After state:
274 # ------------
275 #
276 # RP/0/RP0/CPU0:ios(config)#show running-config vrf
277 # Sun Mar 10 16:48:53.204 UTC
278 # vrf VRF4
279 # mhost ipv4 default-interface Loopback0
280 # evpn-route-sync 793
281 # description VRF4 Description
282 # fallback-vrf test-vrf
283 # remote-route-filtering disable
284 # rd 3:4
285 # !
286 # vrf VRF7
287 # mhost ipv4 default-interface Loopback0
288 # evpn-route-sync 398
289 # description VRF7 description
290 # vpn id 4:5
291 # fallback-vrf replaced-vrf
292 # remote-route-filtering disable
293 # rd 67:9
294 # !
295 # !
296
297 # Using overridden
298 #
299 # Before state:
300 # -------------
301 #
302 # RP/0/RP0/CPU0:ios(config)#show running-config vrf
303 # Sun Mar 10 16:48:53.204 UTC
304 # vrf VRF4
305 # mhost ipv4 default-interface Loopback0
306 # evpn-route-sync 793
307 # description VRF4 Description
308 # fallback-vrf test-vrf
309 # remote-route-filtering disable
310 # rd 3:4
311 # !
312 # !
313 # vrf VRF7
314 # mhost ipv4 default-interface Loopback0
315 # evpn-route-sync 398
316 # description VRF7 description
317 # vpn id 4:5
318 # fallback-vrf replaced-vrf
319 # remote-route-filtering disable
320 # rd 67:9
321 # !
322 # !
323
324 - name: Override the provided configuration with the existing running configuration
325 cisco.iosxr.iosxr_vrf_global:
326 state: overridden
327 config:
328 - name: VRF6
329 description: VRF6 Description
330 evpn_route_sync: 101
331 fallback_vrf: "overridden-vrf"
332 remote_route_filtering:
333 disable: "true"
334 rd: "9:8"
335 mhost:
336 afi: "ipv4"
337 default_interface: "Loopback0"
338 vpn:
339 id: "23:3"
340
341 # Task Output:
342 # ------------
343 #
344 # before:
345 # - name: VRF4
346 # description: VRF4 Description
347 # evpn_route_sync: 793
348 # fallback_vrf: "test-vrf"
349 # mhost:
350 # afi: "ipv4"
351 # default_interface: "Loopback0"
352 # rd: "3:4"
353 # remote_route_filtering:
354 # disable: "true"
355 # - name: VRF7
356 # description: VRF7 description
357 # evpn_route_sync: 398
358 # fallback_vrf: "replaced-vrf"
359 # remote_route_filtering:
360 # disable: true
361 # rd: "67:9"
362 # mhost:
363 # afi: "ipv4"
364 # default_interface: "Loopback0"
365 # vpn:
366 # id: "4:5"
367 #
368 # commands:
369 # - vrf VRF4
370 # - no description VRF4 Description
371 # - no evpn-route-sync 793
372 # - no fallback-vrf test-vrf
373 # - no mhost ipv4 default-interface Loopback0
374 # - no rd 3:4
375 # - no remote-route-filtering disable
376 # - vrf VRF7
377 # - no description VRF7 description
378 # - no evpn-route-sync 398
379 # - no fallback-vrf replaced-vrf
380 # - no mhost ipv4 default-interface Loopback0
381 # - no rd 67:9
382 # - no remote-route-filtering disable
383 # - no vpn id 4:5
384 # - vrf VRF6
385 # - description VRF6 Description
386 # - evpn-route-sync 101
387 # - fallback-vrf overridden-vrf
388 # - mhost ipv4 default-interface Loopback0
389 # - rd 9:8
390 # - remote-route-filtering disable
391 # - vpn id 23:3
392 #
393 # after:
394 # - name: VRF4
395 # - name: VRF6
396 # description: VRF6 Description
397 # evpn_route_sync: 101
398 # fallback_vrf: "overridden-vrf"
399 # remote_route_filtering:
400 # disable: "true"
401 # rd: "9:8"
402 # mhost:
403 # afi: "ipv4"
404 # default_interface: "Loopback0"
405 # vpn:
406 # id: "23:3"
407 # - name: VRF7
408 #
409 # After state:
410 # -------------
411 # RP/0/RP0/CPU0:ios(config)#show running-config vrf
412 # Sun Mar 10 16:54:53.007 UTC
413 # vrf VRF4
414 # vrf VRF6
415 # mhost ipv4 default-interface Loopback0
416 # evpn-route-sync 101
417 # description VRF6 Description
418 # vpn id 23:3
419 # fallback-vrf overridden-vrf
420 # remote-route-filtering disable
421 # rd 9:8
422 # vrf VRF7
423
424 # Using deleted
425 #
426 # Before state:
427 # -------------
428 #
429 # RP/0/RP0/CPU0:ios(config)#show running-config vrf
430 # Sun Mar 10 16:54:53.007 UTC
431 # vrf VRF4
432 # vrf VRF6
433 # mhost ipv4 default-interface Loopback0
434 # evpn-route-sync 101
435 # description VRF6 Description
436 # vpn id 23:3
437 # fallback-vrf overridden-vrf
438 # remote-route-filtering disable
439 # rd 9:8
440 # vrf VRF7
441
442 - name: Delete the provided configuration
443 cisco.iosxr.iosxr_vrf_global:
444 config:
445 state: deleted
446
447 # Task Output:
448 # ------------
449 #
450 # before:
451 # - name: VRF4
452 # - name: VRF6
453 # description: VRF6 Description
454 # evpn_route_sync: 101
455 # fallback_vrf: "overridden-vrf"
456 # remote_route_filtering:
457 # disable: "true"
458 # rd: "9:8"
459 # mhost:
460 # afi: "ipv4"
461 # default_interface: "Loopback0"
462 # vpn:
463 # id: "23:3"
464 # - name: VRF7
465
466 # commands:
467 # - vrf VRF4
468 # - vrf VRF6
469 # - no description VRF6 Description
470 # - no evpn-route-sync 101
471 # - no fallback-vrf overridden-vrf
472 # - no mhost ipv4 default-interface Loopback0
473 # - no rd 9:8
474 # - no remote-route-filtering disable
475 # - no vpn id 23:3
476 # - vrf VRF7
477 #
478 # after:
479 # - name: VRF4
480 # - name: VRF6
481 # - name: VRF7
482 #
483 # After state:
484 # ------------
485 #
486 # RP/0/RP0/CPU0:ios(config)#show running-config vrf
487 # Sun Mar 10 17:02:38.981 UTC
488 # vrf VRF4
489 # vrf VRF6
490 # vrf VRF7
491
492 # Using purged
493 #
494 # Before state:
495 # -------------
496 #
497 # RP/0/RP0/CPU0:ios(config)#show running-config vrf
498 # vrf VRF4
499 # vrf VRF6
500 # vrf VRF7
501
502 - name: Purge all the configuration from the device
503 cisco.iosxr.iosxr_vrf_global:
504 state: purged
505
506 # Task Output:
507 # ------------
508 #
509 # before:
510 # - name: VRF4
511 # - name: VRF6
512 # - name: VRF7
513 #
514 # commands:
515 # - no vrf VRF4
516 # - no vrf VRF6
517 # - no vrf VRF7
518 #
519 # after: []
520 #
521 # After state:
522 # -------------
523 # RP/0/RP0/CPU0:ios(config)#show running-config vrf
524 # Sun Mar 10 17:02:38.981 UTC
525 # -
526
527 # Using rendered
528 #
529 - name: Render provided configuration with device configuration
530 cisco.iosxr.iosxr_vrf_global:
531 config:
532 - name: VRF4
533 description: VRF4 Description
534 evpn_route_sync: 793
535 fallback_vrf: "test-vrf"
536 remote_route_filtering:
537 disable: "true"
538 rd: "3:4"
539 mhost:
540 afi: "ipv4"
541 default_interface: "Loopback0"
542 vpn:
543 id: "2:3"
544 state: rendered
545
546 # Task Output:
547 # ------------
548 #
549 # rendered:
550 # - vrf VRF4
551 # - description VRF4 Description
552 # - evpn-route-sync 793
553 # - fallback-vrf test-vrf
554 # - mhost ipv4 default-interface Loopback0
555 # - rd 3:4
556 # - remote-route-filtering disable
557 # - vpn id 2:3
558
559 # Using gathered
560 #
561 # Before state:
562 # -------------
563 #
564 # RP/0/RP0/CPU0:ios(config)#show running-config vrf
565 # Sun Mar 10 17:02:38.981 UTC
566 # vrf VRF4
567 # description "VRF4 Description"
568 # mhost ipv4 default-interface Loopback0
569 # evpn-route-sync 793
570 # vpn id 2:3
571 # fallback-vrf "test-vrf"
572 # remote-route-filtering disable
573 # rd "3:4"
574
575 - name: Gather existing running configuration
576 cisco.iosxr.iosxr_vrf_global:
577 state: gathered
578
579 # Task Output:
580 # ------------
581 #
582 # gathered:
583 # - name: VRF4
584 # description: VRF4 Description
585 # evpn_route_sync: 793
586 # fallback_vrf: "test-vrf"
587 # mhost:
588 # afi: "ipv4"
589 # default_interface: "Loopback0"
590 # rd: "3:4"
591 # remote_route_filtering:
592 # disable: "true"
593 # vpn:
594 # id: "2:3"
595
596 # Using parsed
597 #
598 # File: parsed.cfg
599 # ----------------
600 #
601 # vrf test
602 # description "This is test VRF"
603 # mhost ipv4 default-interface Loopback0
604 # evpn-route-sync 456
605 # vpn id 56
606 # fallback-vrf "test-vrf"
607 # remote-route-filtering disable
608 # rd "testing"
609 # !
610 # !
611 # vrf my_vrf
612 # mhost ipv4 default-interface Loopback0
613 # evpn-route-sync 235
614 # description "this is sample vrf for feature testing"
615 # fallback-vrf "parsed-vrf"
616 # rd "2:3"
617 # remote-route-filtering disable
618 # vpn id 23
619 # !
620 # !
621
622 - name: Parse the provided configuration
623 cisco.iosxr.iosxr_vrf_global:
624 running_config: "{{ lookup('file', 'parsed.cfg') }}"
625 state: parsed
626
627 # Task Output:
628 # ------------
629 #
630 # parsed:
631 # - description: This is test VRF
632 # evpn_route_sync: 456
633 # fallback_vrf: test-vrf
634 # mhost:
635 # afi: ipv4
636 # default_interface: Loopback0
637 # name: test
638 # rd: testing
639 # remote_route_filtering:
640 # disable: true
641 # vpn:
642 # id: '56'
643 # - description: this is sample vrf for feature testing
644 # evpn_route_sync: 235
645 # fallback_vrf: parsed-vrf
646 # mhost:
647 # afi: ipv4
648 # default_interface: Loopback0
649 # name: my_vrf
650 # rd: '2:3'
651 # remote_route_filtering:
652 # disable: true
653 # vpn:
654 # id: '23'
655 """
656
657 RETURN = """
658 before:
659 description: The configuration prior to the model invocation.
660 returned: always
661 type: dict
662 sample: >
663 The configuration returned will always be in the same format
664 of the parameters above.
665 after:
666 description: The resulting configuration model invocation.
667 returned: when changed
668 type: dict
669 sample: >
670 The configuration returned will always be in the same format
671 of the parameters above.
672 commands:
673 description: The set of commands pushed to the remote device.
674 returned: always
675 type: list
676 sample:
677 - vrf VRF7
678 - description VRF7 description
679 - rd: 67:9
680 - fallback-vrf replaced-vrf
681 rendered:
682 description: The provided configuration in the task rendered in device-native format (offline).
683 returned: when I(state) is C(rendered)
684 type: list
685 sample:
686 - vrf VRF4
687 - description VRF4 Description
688 - evpn-route-sync 793
689 - fallback-vrf parsed-vrf
690 gathered:
691 description: Facts about the network resource gathered from the remote device as structured data.
692 returned: when I(state) is C(gathered)
693 type: list
694 sample: >
695 This output will always be in the same format as the
696 module argspec.
697 parsed:
698 description: The device native config provided in I(running_config) option parsed into structured data as per module argspec.
699 returned: when I(state) is C(parsed)
700 type: list
701 sample: >
702 This output will always be in the same format as the
703 module argspec.
704 """
705
706 from ansible.module_utils.basic import AnsibleModule
707
708 from ansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.argspec.vrf_global.vrf_global import (
709 Vrf_globalArgs,
710 )
711 from ansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.config.vrf_global.vrf_global import (
712 Vrf_global,
713 )
714
715
716 def main():
717 """
718 Main entry point for module execution
719
720 :returns: the result form module invocation
721 """
722 module = AnsibleModule(
723 argument_spec=Vrf_globalArgs.argument_spec,
724 mutually_exclusive=[["config", "running_config"]],
725 required_if=[
726 ["state", "merged", ["config"]],
727 ["state", "replaced", ["config"]],
728 ["state", "overridden", ["config"]],
729 ["state", "rendered", ["config"]],
730 ["state", "parsed", ["running_config"]],
731 ],
732 supports_check_mode=True,
733 )
734
735 result = Vrf_global(module).execute_module()
736 module.exit_json(**result)
737
738
739 if __name__ == "__main__":
740 main()