Debian Codegraph @

ansible_collections/cisco/ios/plugins/modules/ios_vrf_interfaces.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 ios_vrf_interfaces
9 """
10
11 from __future__ import absolute_import, division, print_function
12
13
14 __metaclass__ = type
15
16 DOCUMENTATION = """
17 ---
18 module: ios_vrf_interfaces
19 extends_documentation_fragment:
20 - cisco.ios.ios
21 short_description: Manages VRF configuration on interfaces.
22 description:
23 - Manages Virtual Routing and Forwarding (VRF) configuration on interfaces of Cisco IOS devices.
24 version_added: "1.0.0"
25 author: "AAYUSH ANAND (@AAYUSH2091)"
26 notes:
27 - Tested against Cisco IOS XE Version 17.13.01a
28 - VRF must exist before assigning to an interface
29 - When removing VRF from interface, associated IP addresses will be removed
30 options:
31 config:
32 description: A list of interface VRF configurations.
33 type: list
34 elements: dict
35 suboptions:
36 name:
37 description:
38 - Full name of the interface to be configured.
39 - Example - GigabitEthernet1, Loopback24
40 type: str
41 required: true
42 vrf_name:
43 description:
44 - Name of the VRF to be configured on the interface.
45 - When configured, applies 'vrf forwarding <vrf_name>' under the interface.
46 type: str
47 running_config:
48 description:
49 - This option is used only with state I(parsed).
50 - The value of this option should be the output received from the IOS device by executing
51 the command B(show running-config | section ^interface).
52 - The state I(parsed) reads the configuration from C(running_config) option and transforms
53 it into Ansible structured data as per the resource module's argspec and the value
54 is then returned in the I(parsed) key within the result.
55 type: str
56 state:
57 description:
58 - The state the configuration should be left in.
59 type: str
60 choices:
61 - merged
62 - replaced
63 - overridden
64 - deleted
65 - gathered
66 - rendered
67 - parsed
68 default: merged
69 """
70
71 EXAMPLES = """
72 # Using merged
73
74 # Before state:
75 # -------------
76 #
77 # vios#show running-config | section ^interface
78 # interface Loopback24
79 # no ip address
80 # interface GigabitEthernet1
81 # ip address dhcp
82 # negotiation auto
83 # interface GigabitEthernet2
84 # no ip address
85 # shutdown
86 # negotiation auto
87 # interface GigabitEthernet3
88 # no ip address
89 # negotiation auto
90 # interface GigabitEthernet4
91 # no ip address
92 # shutdown
93 # negotiation auto
94
95 - name: Merge provided configuration with device configuration
96 cisco.ios.ios_vrf_interfaces:
97 config:
98 - name: GigabitEthernet1
99 - name: GigabitEthernet2
100 vrf_name: vrf_D
101 - name: GigabitEthernet3
102 - name: GigabitEthernet4
103 state: merged
104
105 # Task Output:
106 # ------------
107 #
108 # before:
109 # - name: "Loopback24"
110 # - name: "GigabitEthernet1"
111 # - name: "GigabitEthernet2"
112 # - name: "GigabitEthernet3"
113 # - name: "GigabitEthernet4"
114 #
115 # commands:
116 # - interface GigabitEthernet2
117 # - vrf forwarding vrf_D
118 #
119 # after:
120 # - name: "Loopback24"
121 # - name: "GigabitEthernet1"
122 # - name: "GigabitEthernet2"
123 # vrf_name: "vrf_D"
124 # - name: "GigabitEthernet3"
125 # - name: "GigabitEthernet4"
126
127 # After state:
128 # ------------
129 #
130 # vios#show running-config | section ^interface
131 # interface Loopback24
132 # no ip address
133 # interface GigabitEthernet1
134 # ip address dhcp
135 # negotiation auto
136 # interface GigabitEthernet2
137 # vrf forwarding vrf_D
138 # no ip address
139 # shutdown
140 # negotiation auto
141 # interface GigabitEthernet3
142 # no ip address
143 # negotiation auto
144 # interface GigabitEthernet4
145 # no ip address
146 # shutdown
147 # negotiation auto
148
149 # Using overridden
150
151 # Before state:
152 # -------------
153 #
154 # vios#show running-config | section ^interface
155 # interface Loopback24
156 # no ip address
157 # interface GigabitEthernet1
158 # ip address dhcp
159 # negotiation auto
160 # interface GigabitEthernet2
161 # vrf forwarding vrf_B
162 # no ip address
163 # shutdown
164 # negotiation auto
165 # interface GigabitEthernet3
166 # no ip address
167 # negotiation auto
168 # interface GigabitEthernet4
169 # no ip address
170 # shutdown
171 # negotiation auto
172
173 - name: Override device configuration with provided configuration
174 cisco.ios.ios_vrf_interfaces:
175 config:
176 - name: GigabitEthernet1
177 - name: GigabitEthernet2
178 - name: GigabitEthernet3
179 - name: GigabitEthernet4
180 state: overridden
181
182 # Task Output:
183 # ------------
184 #
185 # before:
186 # - name: "Loopback24"
187 # - name: "GigabitEthernet1"
188 # - name: "GigabitEthernet2"
189 # vrf_name: "vrf_B"
190 # - name: "GigabitEthernet3"
191 # - name: "GigabitEthernet4"
192 #
193 # commands:
194 # - interface GigabitEthernet2
195 # - no vrf forwarding vrf_B
196 #
197 # after:
198 # - name: "Loopback24"
199 # - name: "GigabitEthernet1"
200 # - name: "GigabitEthernet2"
201 # - name: "GigabitEthernet3"
202 # - name: "GigabitEthernet4"
203
204 # After state:
205 # ------------
206 #
207 # vios#show running-config | section ^interface
208 # interface Loopback24
209 # no ip address
210 # interface GigabitEthernet1
211 # ip address dhcp
212 # negotiation auto
213 # interface GigabitEthernet2
214 # no ip address
215 # shutdown
216 # negotiation auto
217 # interface GigabitEthernet3
218 # no ip address
219 # negotiation auto
220 # interface GigabitEthernet4
221 # no ip address
222 # shutdown
223 # negotiation auto
224
225 # Using gathered
226
227 # Before state:
228 # -------------
229 #
230 # vios#show running-config | section ^interface
231 # interface Loopback24
232 # no ip address
233 # interface GigabitEthernet1
234 # ip address dhcp
235 # negotiation auto
236 # interface GigabitEthernet2
237 # vrf forwarding vrf_B
238 # no ip address
239 # shutdown
240 # negotiation auto
241 # interface GigabitEthernet3
242 # no ip address
243 # negotiation auto
244 # interface GigabitEthernet4
245 # no ip address
246 # shutdown
247 # negotiation auto
248
249 - name: Gather listed VRF interfaces
250 cisco.ios.ios_vrf_interfaces:
251 state: gathered
252
253 # Task Output:
254 # ------------
255 #
256 # gathered:
257 # - name: "Loopback24"
258 # - name: "GigabitEthernet1"
259 # - name: "GigabitEthernet2"
260 # vrf_name: "vrf_B"
261 # - name: "GigabitEthernet3"
262 # - name: "GigabitEthernet4"
263
264 # Using rendered
265
266 - name: Render VRF configuration
267 cisco.ios.ios_vrf_interfaces:
268 config:
269 - name: GigabitEthernet1
270 - name: GigabitEthernet2
271 vrf_name: vrf_D
272 - name: GigabitEthernet3
273 - name: GigabitEthernet4
274 state: rendered
275
276 # Task Output:
277 # ------------
278 #
279 # rendered:
280 # - interface GigabitEthernet2
281 # - vrf forwarding vrf_D
282
283 # Using parsed
284
285 # File: parsed.cfg
286 # ---------------
287 #
288 # interface GigabitEthernet1
289 # vrf vrf_C
290 # shutdown
291 # !
292 # interface GigabitEthernet2
293 # vrf vrf_D
294 # shutdown
295 # !
296 # interface GigabitEthernet3
297 # shutdown
298 # !
299 # interface GigabitEthernet4
300 # shutdown
301 # !
302
303 - name: Parse configuration from device running config
304 cisco.ios.ios_vrf_interfaces:
305 running_config: "{{ lookup('file', 'parsed.cfg') }}"
306 state: parsed
307
308 # Task Output:
309 # ------------
310 #
311 # parsed:
312 # - name: "GigabitEthernet1"
313 # vrf_name: "vrf_C"
314 # - name: "GigabitEthernet2"
315 # vrf_name: "vrf_D"
316 # - name: "GigabitEthernet3"
317 # - name: "GigabitEthernet4"
318
319 # Using replaced
320
321 # Before state:
322 # -------------
323 #
324 # vios#show running-config | section ^interface
325 # interface Loopback24
326 # no ip address
327 # interface GigabitEthernet1
328 # vrf forwarding vrf_A
329 # ip address dhcp
330 # negotiation auto
331 # interface GigabitEthernet2
332 # vrf forwarding vrf_B
333 # no ip address
334 # shutdown
335 # negotiation auto
336 # interface GigabitEthernet3
337 # no ip address
338 # negotiation auto
339 # interface GigabitEthernet4
340 # vrf forwarding vrf_C
341 # no ip address
342 # shutdown
343 # negotiation auto
344
345 - name: Replace device configuration of listed VRF interfaces with provided configuration
346 cisco.ios.ios_vrf_interfaces:
347 config:
348 - name: GigabitEthernet1
349 vrf_name: vrf_D
350 - name: GigabitEthernet2
351 vrf_name: vrf_E
352 state: replaced
353
354 # Task Output:
355 # ------------
356 #
357 # before:
358 # - name: "Loopback24"
359 # - name: "GigabitEthernet1"
360 # vrf_name: "vrf_A"
361 # - name: "GigabitEthernet2"
362 # vrf_name: "vrf_B"
363 # - name: "GigabitEthernet3"
364 # - name: "GigabitEthernet4"
365 # vrf_name: "vrf_C"
366 #
367 # commands:
368 # - interface GigabitEthernet1
369 # - no vrf forwarding vrf_A
370 # - vrf forwarding vrf_D
371 # - interface GigabitEthernet2
372 # - no vrf forwarding vrf_B
373 # - vrf forwarding vrf_E
374 #
375 # after:
376 # - name: "Loopback24"
377 # - name: "GigabitEthernet1"
378 # vrf_name: "vrf_D"
379 # - name: "GigabitEthernet2"
380 # vrf_name: "vrf_E"
381 # - name: "GigabitEthernet3"
382 # - name: "GigabitEthernet4"
383 # vrf_name: "vrf_C"
384
385 # Using deleted
386
387 # Before state:
388 # -------------
389 #
390 # vios#show running-config | section ^interface
391 # interface Loopback24
392 # no ip address
393 # interface GigabitEthernet1
394 # vrf forwarding vrf_A
395 # ip address dhcp
396 # negotiation auto
397 # interface GigabitEthernet2
398 # vrf forwarding vrf_B
399 # no ip address
400 # shutdown
401 # negotiation auto
402 # interface GigabitEthernet3
403 # no ip address
404 # negotiation auto
405 # interface GigabitEthernet4
406 # vrf forwarding vrf_C
407 # no ip address
408 # shutdown
409 # negotiation auto
410
411 - name: Delete VRF configuration of specified interfaces
412 cisco.ios.ios_vrf_interfaces:
413 config:
414 - name: GigabitEthernet1
415 - name: GigabitEthernet2
416 state: deleted
417
418 # Task Output:
419 # ------------
420 #
421 # before:
422 # - name: "Loopback24"
423 # - name: "GigabitEthernet1"
424 # vrf_name: "vrf_A"
425 # - name: "GigabitEthernet2"
426 # vrf_name: "vrf_B"
427 # - name: "GigabitEthernet3"
428 # - name: "GigabitEthernet4"
429 # vrf_name: "vrf_C"
430 #
431 # commands:
432 # - interface GigabitEthernet1
433 # - no vrf forwarding vrf_A
434 # - interface GigabitEthernet2
435 # - no vrf forwarding vrf_B
436 #
437 # after:
438 # - name: "Loopback24"
439 # - name: "GigabitEthernet1"
440 # - name: "GigabitEthernet2"
441 # - name: "GigabitEthernet3"
442 # - name: "GigabitEthernet4"
443 # vrf_name: "vrf_C"
444
445 # After state:
446 # ------------
447 #
448 # vios#show running-config | section ^interface
449 # interface Loopback24
450 # no ip address
451 # interface GigabitEthernet1
452 # ip address dhcp
453 # negotiation auto
454 # interface GigabitEthernet2
455 # no ip address
456 # shutdown
457 # negotiation auto
458 # interface GigabitEthernet3
459 # no ip address
460 # negotiation auto
461 # interface GigabitEthernet4
462 # vrf forwarding vrf_C
463 # no ip address
464 # shutdown
465 # negotiation auto
466 """
467
468 RETURN = """
469 before:
470 description: The configuration prior to the module execution.
471 returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted)
472 type: list
473 sample: >
474 [
475 {
476 "name": "Loopback24"
477 },
478 {
479 "name": "GigabitEthernet1"
480 },
481 {
482 "name": "GigabitEthernet2",
483 "vrf_name": "vrf_B"
484 },
485 {
486 "name": "GigabitEthernet3"
487 },
488 {
489 "name": "GigabitEthernet4"
490 }
491 ]
492
493 after:
494 description: The resulting configuration after module execution.
495 returned: when changed
496 type: list
497 sample: >
498 [
499 {
500 "name": "Loopback24"
501 },
502 {
503 "name": "GigabitEthernet1"
504 },
505 {
506 "name": "GigabitEthernet2",
507 "vrf_name": "vrf_D"
508 },
509 {
510 "name": "GigabitEthernet3"
511 },
512 {
513 "name": "GigabitEthernet4"
514 }
515 ]
516
517 commands:
518 description: The set of commands pushed to the remote device.
519 returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted)
520 type: list
521 sample:
522 - "interface GigabitEthernet2"
523 - "vrf forwarding vrf_D"
524 - "no vrf forwarding vrf_B"
525
526 rendered:
527 description: The provided configuration in the task rendered in device-native format (offline).
528 returned: when I(state) is C(rendered)
529 type: list
530 sample:
531 - "interface GigabitEthernet1"
532 - "vrf forwarding vrf_C"
533 - "interface GigabitEthernet2"
534 - "vrf forwarding vrf_D"
535
536 gathered:
537 description: Facts about the network resource gathered from the remote device as structured data.
538 returned: when I(state) is C(gathered)
539 type: list
540 sample: >
541 [
542 {
543 "name": "Loopback24"
544 },
545 {
546 "name": "GigabitEthernet1"
547 },
548 {
549 "name": "GigabitEthernet2",
550 "vrf_name": "vrf_B"
551 },
552 {
553 "name": "GigabitEthernet3"
554 },
555 {
556 "name": "GigabitEthernet4"
557 }
558 ]
559
560 parsed:
561 description: The device native config provided in I(running_config) option parsed into structured data as per module argspec.
562 returned: when I(state) is C(parsed)
563 type: list
564 sample: >
565 [
566 {
567 "name": "GigabitEthernet1",
568 "vrf_name": "vrf_C"
569 },
570 {
571 "name": "GigabitEthernet2",
572 "vrf_name": "vrf_D"
573 },
574 {
575 "name": "GigabitEthernet3"
576 },
577 {
578 "name": "GigabitEthernet4"
579 }
580 ]
581 """
582
583 from ansible.module_utils.basic import AnsibleModule
584
585 from ansible_collections.cisco.ios.plugins.module_utils.network.ios.argspec.vrf_interfaces.vrf_interfaces import (
586 Vrf_interfacesArgs,
587 )
588 from ansible_collections.cisco.ios.plugins.module_utils.network.ios.config.vrf_interfaces.vrf_interfaces import (
589 Vrf_interfaces,
590 )
591
592
593 def main():
594 """
595 Main entry point for module execution
596
597 :returns: the result form module invocation
598 """
599 module = AnsibleModule(
600 argument_spec=Vrf_interfacesArgs.argument_spec,
601 mutually_exclusive=[["config", "running_config"]],
602 required_if=[
603 ["state", "merged", ["config"]],
604 ["state", "replaced", ["config"]],
605 ["state", "overridden", ["config"]],
606 ["state", "rendered", ["config"]],
607 ["state", "parsed", ["running_config"]],
608 ],
609 supports_check_mode=True,
610 )
611
612 result = Vrf_interfaces(module).execute_module()
613 module.exit_json(**result)
614
615
616 if __name__ == "__main__":
617 main()