Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17 #
18
19 from __future__ import absolute_import, division, print_function
20
21
22 __metaclass__ = type
23
24 GENERATOR_VERSION = "1.0"
25
26
27 DOCUMENTATION = """
28 module: iosxr_l2_interfaces
29 short_description: Resource Module to configure L2 interfaces.
30 description: This module manages the Layer-2 interface attributes on Cisco IOS-XR
31 devices.
32 version_added: 1.0.0
33 author:
34 - Sumit Jaiswal (@justjais)
35 - Rohit Thakur (@rohitthakur2590)
36 notes:
37 - This module works with connection C(network_cli). See L(the IOS-XR Platform Options,../network/user_guide/platform_iosxr.html).
38 options:
39 config:
40 description: A dictionary of Layer-2 interface options
41 type: list
42 elements: dict
43 suboptions:
44 name:
45 description:
46 - Full name of the interface/sub-interface excluding any logical unit number,
47 e.g. GigabitEthernet0/0/0/1 or GigabitEthernet0/0/0/1.100.
48 type: str
49 required: true
50 native_vlan:
51 description:
52 - Configure a native VLAN ID for the trunk
53 type: int
54 l2transport:
55 description:
56 - Switchport mode access command to configure the interface as a layer 2 access
57 type: bool
58 l2protocol:
59 description:
60 - Configures Layer 2 protocol tunneling and protocol data unit (PDU) filtering
61 on an interface.
62 type: list
63 elements: dict
64 suboptions:
65 cdp:
66 description:
67 - Cisco Discovery Protocol (CDP) tunneling and data unit parameters.
68 choices:
69 - drop
70 - forward
71 - tunnel
72 type: str
73 pvst:
74 description:
75 - Configures the per-VLAN Spanning Tree Protocol (PVST) tunneling and
76 data unit parameters.
77 choices:
78 - drop
79 - forward
80 - tunnel
81 type: str
82 stp:
83 description:
84 - Spanning Tree Protocol (STP) tunneling and data unit parameters.
85 choices:
86 - drop
87 - forward
88 - tunnel
89 type: str
90 vtp:
91 description:
92 - VLAN Trunk Protocol (VTP) tunneling and data unit parameters.
93 choices:
94 - drop
95 - forward
96 - tunnel
97 type: str
98 cpsv:
99 description:
100 - CDP, PVST+, STP, and VTP protocols.
101 choices:
102 - drop
103 - reverse-tunnel
104 - tunnel
105 type: str
106 encapsulation:
107 description: Specify which packets will be matched by this sub-interface.
108 type: dict
109 suboptions:
110 dot1q:
111 type: int
112 description: IEEE 802.1Q VLAN-tagged packets.
113 second_dot1q:
114 type: int
115 description: IEEE 802.1Q VLAN-tagged packets.
116 q_vlan:
117 description:
118 - 802.1Q VLAN configuration. Note that it can accept either 2 VLAN IDs when
119 configuring Q-in-Q VLAN, or it will accept 1 VLAN ID and 'any' as input
120 list when configuring Q-in-any vlan as input. Note, that this option is
121 valid only with respect to Sub-Interface and is not valid when configuring
122 for Interface.
123 - This option is DEPRECATED and replaced with qvlan,
124 this attribute will be removed after 2026-06-01.
125 type: list
126 elements: int
127 qvlan:
128 description:
129 - 802.1Q VLAN configuration. Note that it can accept either 2 VLAN IDs when
130 configuring Q-in-Q VLAN, or it will accept 1 VLAN ID and 'any' as input
131 list when configuring Q-in-any vlan as input. Note, that this option is
132 valid only with respect to Sub-Interface and is not valid when configuring
133 for Interface.
134 type: list
135 elements: str
136 propagate:
137 description:
138 - Propagate Layer 2 transport events. Note that it will work only when the
139 I(l2tranport) option is set to TRUE
140 type: bool
141 running_config:
142 description:
143 - This option is used only with state I(parsed).
144 - The value of this option should be the output received from the IOS-XR device
145 by executing the command B(show running-config interface).
146 - The state I(parsed) reads the configuration from C(running_config) option and
147 transforms it into Ansible structured data as per the resource module's argspec
148 and the value is then returned in the I(parsed) key within the result.
149 type: str
150 state:
151 choices:
152 - merged
153 - replaced
154 - overridden
155 - deleted
156 - rendered
157 - gathered
158 - parsed
159 default: merged
160 description:
161 - The state of the configuration after module completion
162 type: str
163 """
164
165 EXAMPLES = """
166 # Using merged
167 #
168 # Before state:
169 # -------------
170 #
171 # viosxr#show running-config interface
172 # interface GigabitEthernet0/0/0/3
173 # description Ansible Network
174 # vrf custB
175 # ipv4 address 10.10.0.2 255.255.255.0
176 # duplex half
177 # shutdown
178 # !
179 # interface GigabitEthernet0/0/0/4
180 # description Test description
181 # !
182
183 - name: Merge provided configuration with device configuration
184 cisco.iosxr.iosxr_l2_interfaces:
185 config:
186 - name: GigabitEthernet0/0/0/3
187 native_vlan: 20
188 - name: GigabitEthernet0/0/0/4
189 native_vlan: 40
190 l2transport: true
191 l2protocol:
192 - stp: tunnel
193 - name: GigabitEthernet0/0/0/3.900
194 l2transport: true
195 q_vlan:
196 - 20
197 - 40
198 state: merged
199
200
201 # After state:
202 # ------------
203 #
204 # viosxr#show running-config interface
205 # interface GigabitEthernet0/0/0/3
206 # description Ansible Network
207 # vrf custB
208 # ipv4 address 10.10.0.2 255.255.255.0
209 # duplex half
210 # shutdown
211 # dot1q native vlan 20
212 # !
213 # interface GigabitEthernet0/0/0/4
214 # description Test description
215 # dot1q native vlan 10
216 # l2transport
217 # l2protocol stp tunnel
218 # !
219 # !
220 # interface GigabitEthernet0/0/0/3.900 l2transport
221 # dot1q vlan 20 40
222 # !
223
224 # Using replaced
225 #
226 # Before state:
227 # -------------
228 #
229 # viosxr#show running-config interface
230 # interface GigabitEthernet0/0/0/3
231 # description Ansible Network
232 # vrf custB
233 # ipv4 address 10.10.0.2 255.255.255.0
234 # duplex half
235 # shutdown
236 # dot1q native vlan 20
237 # !
238 # interface GigabitEthernet0/0/0/4
239 # description Test description
240 # dot1q native vlan 10
241 # l2transport
242 # l2protocol stp tunnel
243 # !
244 # !
245 # interface GigabitEthernet0/0/0/3.900 l2transport
246 # dot1q vlan 20 40
247 # !
248
249 - name: >-
250 Replaces device configuration of listed interfaces with provided
251 configuration
252 cisco.iosxr.iosxr_l2_interfaces:
253 config:
254 - name: GigabitEthernet0/0/0/4
255 native_vlan: 40
256 l2transport: true
257 l2protocol:
258 - stp: forward
259 - name: GigabitEthernet0/0/0/3.900
260 q_vlan:
261 - 20
262 - any
263 state: replaced
264
265
266 # After state:
267 # -------------
268 #
269 # viosxr#show running-config interface
270 # interface GigabitEthernet0/0/0/3
271 # description Ansible Network
272 # vrf custB
273 # ipv4 address 10.10.0.2 255.255.255.0
274 # duplex half
275 # shutdown
276 # dot1q native vlan 20
277 # !
278 # interface GigabitEthernet0/0/0/4
279 # description Test description
280 # dot1q native vlan 40
281 # l2transport
282 # l2protocol stp forward
283 # !
284 # !
285 # interface GigabitEthernet0/0/0/3.900 l2transport
286 # dot1q vlan 20 any
287 # !
288
289 # Using overridden
290 #
291 # Before state:
292 # -------------
293 #
294 # viosxr#show running-config interface
295 # interface GigabitEthernet0/0/0/3
296 # description Ansible Network
297 # vrf custB
298 # ipv4 address 10.10.0.2 255.255.255.0
299 # duplex half
300 # shutdown
301 # dot1q native vlan 20
302 # !
303 # interface GigabitEthernet0/0/0/4
304 # description Test description
305 # dot1q native vlan 10
306 # l2transport
307 # l2protocol stp tunnel
308 # !
309 # !
310 # interface GigabitEthernet0/0/0/3.900 l2transport
311 # dot1q vlan 20 40
312 # !
313
314 - name: Override device configuration of all interfaces with provided configuration
315 cisco.iosxr.iosxr_l2_interfaces:
316 config:
317 - name: GigabitEthernet0/0/0/4
318 native_vlan: 40
319 l2transport: true
320 l2protocol:
321 - stp: forward
322 - name: GigabitEthernet0/0/0/3.900
323 q_vlan:
324 - 20
325 - any
326 state: overridden
327
328
329 # After state:
330 # -------------
331 #
332 # viosxr#show running-config interface
333 # interface GigabitEthernet0/0/0/3
334 # description Ansible Network
335 # vrf custB
336 # ipv4 address 10.10.0.2 255.255.255.0
337 # duplex half
338 # shutdown
339 # !
340 # interface GigabitEthernet0/0/0/4
341 # description Test description
342 # dot1q native vlan 40
343 # l2transport
344 # l2protocol stp forward
345 # !
346 # !
347 # interface GigabitEthernet0/0/0/3.900
348 # dot1q vlan 20 any
349 # !
350
351 # Using deleted
352 #
353 # Before state:
354 # -------------
355 #
356 # viosxr#show running-config interface
357 # interface GigabitEthernet0/0/0/3
358 # description Ansible Network
359 # vrf custB
360 # ipv4 address 10.10.0.2 255.255.255.0
361 # duplex half
362 # shutdown
363 # dot1q native vlan 20
364 # !
365 # interface GigabitEthernet0/0/0/4
366 # description Test description
367 # dot1q native vlan 10
368 # l2transport
369 # l2protocol stp tunnel
370 # !
371 # !
372 #
373
374 - name: "Delete L2 attributes of given interfaces (Note: This won't delete the interface itself)"
375 cisco.iosxr.iosxr_l2_interfaces:
376 config:
377 - name: GigabitEthernet0/0/0/4
378 state: deleted
379
380 # After state:
381 # ------------
382 #
383 # viosxr#show running-config interface
384 # interface GigabitEthernet0/0/0/3
385 # description Ansible Network
386 # vrf custB
387 # ipv4 address 10.10.0.2 255.255.255.0
388 # duplex half
389 # shutdown
390 # dot1q native vlan 20
391 # !
392 # interface GigabitEthernet0/0/0/4
393 # description Test description
394 # !
395
396 # Using Deleted without any config passed
397 # "(NOTE: This will delete all of configured resource module attributes from each configured interface)"
398 #
399 # Before state:
400 # -------------
401 #
402 # viosxr#show running-config interface
403 # interface GigabitEthernet0/0/0/3
404 # description Ansible Network
405 # vrf custB
406 # ipv4 address 10.10.0.2 255.255.255.0
407 # duplex half
408 # shutdown
409 # dot1q native vlan 20
410 # !
411 # interface GigabitEthernet0/0/0/4
412 # description Test description
413 # dot1q native vlan 10
414 # l2transport
415 # l2protocol stp tunnel
416 # !
417 # !
418
419 - name: "Delete L2 attributes of all interfaces (Note: This won't delete the interface itself)"
420 cisco.iosxr.iosxr_l2_interfaces:
421 state: deleted
422
423 # After state:
424 # ------------
425 #
426 # viosxr#show running-config interface
427 # interface GigabitEthernet0/0/0/3
428 # description Ansible Network
429 # vrf custB
430 # ipv4 address 10.10.0.2 255.255.255.0
431 # duplex half
432 # shutdown
433 # !
434 # interface GigabitEthernet0/0/0/4
435 # description Test description
436 # !
437
438
439 # Using parsed
440 # parsed.cfg
441 # ------------
442 #
443 # interface Loopback888
444 # description test for ansible
445 # shutdown
446 # !
447 # interface MgmtEth0/0/CPU0/0
448 # ipv4 address 10.8.38.70 255.255.255.0
449 # !
450 # interface GigabitEthernet0/0/0/0
451 # description Configured and Merged by Ansible-Network
452 # mtu 110
453 # ipv4 address 172.31.1.1 255.255.255.0
454 # duplex half
455 # !
456 # interface GigabitEthernet0/0/0/1
457 # dot1q native vlan 10
458 # l2transport
459 # l2protocol cdp forward
460 # l2protocol pvst tunnel
461 # propagate remote-status
462 # !
463 # !
464 # interface GigabitEthernet0/0/0/3
465 # shutdown
466 # !
467 # interface GigabitEthernet0/0/0/3.900
468 # encapsulation dot1q 20 second-dot1q 40
469 # !
470 # interface GigabitEthernet0/0/0/4
471 # shutdown
472 # dot1q native vlan 40
473 # !
474 - name: Convert L2 interfaces config to argspec without connecting to the appliance
475 cisco.iosxr.iosxr_l2_interfaces:
476 running_config: "{{ lookup('file', './parsed.cfg') }}"
477 state: parsed
478 # Task Output (redacted)
479 # -----------------------
480 # "parsed": [
481 # {
482 # "name": "GigabitEthernet0/0/0/0"
483 # },
484 # {
485 # "l2protocol": [
486 # {
487 # "cdp": "forward"
488 # },
489 # {
490 # "pvst": "tunnel"
491 # }
492 # ],
493 # "l2transport": true,
494 # "name": "GigabitEthernet0/0/0/1",
495 # "native_vlan": 10,
496 # "propagate": true
497 # },
498 # {
499 # "name": "GigabitEthernet0/0/0/3"
500 # },
501 # {
502 # "name": "GigabitEthernet0/0/0/3.900",
503 # "q_vlan": [
504 # 20,
505 # 40
506 # ]
507 # },
508 # {
509 # "name": "GigabitEthernet0/0/0/4",
510 # "native_vlan": 40
511 # }
512 # ]
513
514
515 # Using rendered
516 - name: Render platform specific commands from task input using rendered state
517 cisco.iosxr.iosxr_l2_interfaces:
518 config:
519 - name: GigabitEthernet0/0/0/1
520 native_vlan: 10
521 l2transport: true
522 l2protocol:
523 - pvst: tunnel
524 - cdp: forward
525 propagate: true
526 - name: GigabitEthernet0/0/0/3.900
527 q_vlan:
528 - 20
529 - 40
530 - name: GigabitEthernet0/0/0/4
531 native_vlan: 40
532 state: rendered
533
534 # Task Output (redacted)
535 # -----------------------
536 # "rendered": [
537 # "interface GigabitEthernet0/0/0/1",
538 # "dot1q native vlan 10",
539 # "l2transport l2protocol pvst tunnel",
540 # "l2transport l2protocol cdp forward",
541 # "l2transport propagate remote-status",
542 # "interface GigabitEthernet0/0/0/3.900",
543 # "dot1q vlan 20 40",
544 # "interface GigabitEthernet0/0/0/4",
545 # "dot1q native vlan 40"
546 # ]
547
548
549 # Using gathered
550 # Before state:
551 # ------------
552 #
553 # RP/0/0/CPU0:an-iosxr-02#show running-config interface
554 # interface Loopback888
555 # description test for ansible
556 # shutdown
557 # !
558 # interface MgmtEth0/0/CPU0/0
559 # ipv4 address 10.8.38.70 255.255.255.0
560 # !
561 # interface GigabitEthernet0/0/0/0
562 # description Configured and Merged by Ansible-Network
563 # mtu 110
564 # ipv4 address 172.31.1.1 255.255.255.0
565 # duplex half
566 # !
567 # interface GigabitEthernet0/0/0/1
568 # dot1q native vlan 10
569 # l2transport
570 # l2protocol cdp forward
571 # l2protocol pvst tunnel
572 # propagate remote-status
573 # !
574 # !
575 # interface GigabitEthernet0/0/0/3
576 # shutdown
577 # !
578 # interface GigabitEthernet0/0/0/3.900
579 # encapsulation dot1q 20 second-dot1q 40
580 # !
581 # interface GigabitEthernet0/0/0/4
582 # shutdown
583 # dot1q native vlan 40
584 # !
585 - name: Gather IOSXR l2 interfaces as in given arguments
586 cisco.iosxr.iosxr_l2_interfaces:
587 config:
588 state: gathered
589 # Task Output (redacted)
590 # -----------------------
591 #
592 # "gathered": [
593 # {
594 # "name": "GigabitEthernet0/0/0/0"
595 # },
596 # {
597 # "l2protocol": [
598 # {
599 # "cdp": "forward"
600 # },
601 # {
602 # "pvst": "tunnel"
603 # }
604 # ],
605 # "l2transport": true,
606 # "name": "GigabitEthernet0/0/0/1",
607 # "native_vlan": 10,
608 # "propagate": true
609 # },
610 # {
611 # "name": "GigabitEthernet0/0/0/3"
612 # },
613 # {
614 # "name": "GigabitEthernet0/0/0/3.900",
615 # "q_vlan": [
616 # 20,
617 # 40
618 # ]
619 # },
620 # {
621 # "name": "GigabitEthernet0/0/0/4",
622 # "native_vlan": 40
623 # }
624 # ]
625 # After state:
626 # ------------
627 #
628 # RP/0/0/CPU0:an-iosxr-02#show running-config interface
629 # interface Loopback888
630 # description test for ansible
631 # shutdown
632 # !
633 # interface MgmtEth0/0/CPU0/0
634 # ipv4 address 10.8.38.70 255.255.255.0
635 # !
636 # interface GigabitEthernet0/0/0/0
637 # description Configured and Merged by Ansible-Network
638 # mtu 110
639 # ipv4 address 172.31.1.1 255.255.255.0
640 # duplex half
641 # !
642 # interface GigabitEthernet0/0/0/1
643 # dot1q native vlan 10
644 # l2transport
645 # l2protocol cdp forward
646 # l2protocol pvst tunnel
647 # propagate remote-status
648 # !
649 # !
650 # interface GigabitEthernet0/0/0/3
651 # shutdown
652 # !
653 # interface GigabitEthernet0/0/0/3.900
654 # encapsulation dot1q 20 second-dot1q 40
655 # !
656 # interface GigabitEthernet0/0/0/4
657 # shutdown
658 # dot1q native vlan 40
659 # !
660 """
661
662 RETURN = """
663 before:
664 description: The configuration as structured data prior to module invocation.
665 returned: always
666 type: list
667 sample: The configuration returned will always be in the same format of the parameters above.
668 after:
669 description: The configuration as structured data after module completion.
670 returned: when changed
671 type: list
672 sample: The configuration returned will always be in the same format of the parameters above.
673 commands:
674 description: The set of commands pushed to the remote device
675 returned: always
676 type: list
677 sample: ['interface GigabitEthernet0/0/0/2', 'l2transport l2protocol pvst tunnel']
678 """
679
680 from ansible.module_utils.basic import AnsibleModule
681
682 from ansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.argspec.l2_interfaces.l2_interfaces import (
683 L2_InterfacesArgs,
684 )
685 from ansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.config.l2_interfaces.l2_interfaces import (
686 L2_Interfaces,
687 )
688
689
690 def main():
691 """
692 Main entry point for module execution
693 :returns: the result form module invocation
694 """
695 required_if = [
696 ("state", "merged", ("config",)),
697 ("state", "replaced", ("config",)),
698 ("state", "rendered", ("config",)),
699 ("state", "overridden", ("config",)),
700 ("state", "parsed", ("running_config",)),
701 ]
702 mutually_exclusive = [("config", "running_config")]
703 module = AnsibleModule(
704 argument_spec=L2_InterfacesArgs.argument_spec,
705 required_if=required_if,
706 supports_check_mode=True,
707 mutually_exclusive=mutually_exclusive,
708 )
709
710 result = L2_Interfaces(module).execute_module()
711 module.exit_json(**result)
712
713
714 if __name__ == "__main__":
715 main()