Debian Codegraph @

ansible_collections/cisco/nxos/plugins/modules/nxos_l2_interfaces.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Copyright 2025 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 nxos_l2_interfaces
9 """
10
11 from __future__ import absolute_import, division, print_function
12
13
14 __metaclass__ = type
15
16 DOCUMENTATION = """
17 module: nxos_l2_interfaces
18 short_description: L2 interfaces resource module
19 description: This module manages Layer-2 interfaces attributes of NX-OS Interfaces.
20 version_added: 1.0.0
21 author:
22 - Trishna Guha (@trishnaguha)
23 - Vinay Mulugund (@roverflow)
24 notes:
25 - Tested against NXOS 10.3(2) on CML
26 - Unsupported for Cisco MDS
27 options:
28 config:
29 description: A List of Layer-2 interface options
30 type: list
31 elements: dict
32 suboptions:
33 name:
34 description:
35 - Full name of interface, i.e. Ethernet1/1.
36 type: str
37 required: true
38 access:
39 description:
40 - Switchport mode access command to configure the interface as a Layer-2 access.
41 type: dict
42 suboptions:
43 vlan:
44 description:
45 - Configure given VLAN in access port. It's used as the access VLAN ID.
46 type: int
47 trunk:
48 description:
49 - Switchport mode trunk command to configure the interface as a Layer-2 trunk.
50 type: dict
51 suboptions:
52 native_vlan:
53 description:
54 - Native VLAN to be configured in trunk port. It is used as the trunk
55 native VLAN ID.
56 type: int
57 allowed_vlans:
58 description:
59 - List of allowed VLANs in a given trunk port. These are the only VLANs
60 that will be configured on the trunk.
61 - The module would consider 1-4093 as the default range enabled if switchport
62 mode is trunk.
63 type: str
64 allowed_vlans_none:
65 description:
66 - This option governs the `switchport trunk allowed vlan none` configuration
67 - negation of this command would consider 1-4093 as the default.
68 - This attribute does not ensure idempotency.
69 type: bool
70 mode:
71 description:
72 - Mode in which interface needs to be configured.
73 - Access mode is not shown in interface facts, so idempotency will not be
74 maintained for switchport mode access and every time the output will come
75 as changed=True.
76 type: str
77 choices:
78 - dot1q-tunnel
79 - access
80 - trunk
81 - fex-fabric
82 - fabricpath
83 aliases:
84 - switchport_mode
85 cdp_enable:
86 type: bool
87 description: Enable/disable CDP on the interface
88 link_flap:
89 type: dict
90 description: Configure actions on link-flap
91 suboptions:
92 error_disable:
93 type: dict
94 description: Configure the port to error-disable on link-flap
95 suboptions:
96 count:
97 type: int
98 description: Configure the number of link-flaps to trigger the action
99 interval:
100 type: int
101 description: Configure the interval to trigger the action
102 beacon:
103 type: bool
104 description: Disable/enable the beacon for an interface
105 state:
106 description:
107 - The state of the configuration after module completion.
108 type: str
109 choices:
110 - merged
111 - replaced
112 - overridden
113 - deleted
114 - gathered
115 - rendered
116 - parsed
117 default: merged
118 running_config:
119 description:
120 - This option is used only with state I(parsed).
121 - The value of this option should be the output received from the NX-OS device
122 by executing the command B(show running-config | section ^interface).
123 - The state I(parsed) reads the configuration from C(running_config) option and
124 transforms it into Ansible structured data as per the resource module's argspec
125 and the value is then returned in the I(parsed) key within the result.
126 type: str
127 """
128
129 EXAMPLES = """
130 # Using merged
131
132 # Before state:
133 # -------------
134 #
135 # switch# show running-config | section interface
136 # interface Ethernet1/1
137 # interface Ethernet1/2
138 # switchport trunk native vlan 20
139 # interface mgmt0
140 # ip address dhcp
141 # ipv6 address auto-config
142
143 - name: Merge provided configuration with device configuration
144 cisco.nxos.nxos_l2_interfaces:
145 config:
146 - name: Ethernet1/1
147 trunk:
148 native_vlan: 10
149 allowed_vlans: 2,4,15
150 - name: Ethernet1/2
151 access:
152 vlan: 30
153 state: merged
154
155 # Task Output
156 # -----------
157 #
158 # before:
159 # - name: Loopback999
160 # - name: Ethernet1/2
161 # - name: mgmt0
162 # - name: Ethernet1/1
163 # commands:
164 # - interface Ethernet1/1
165 # - switchport trunk allowed vlan 2,4,15
166 # - switchport trunk native vlan 10
167 # - interface Ethernet1/2
168 # - switchport access vlan 30
169 # after:
170 # - name: Ethernet1/1
171 # trunk:
172 # allowed_vlans: 2,4,15
173 # native_vlan: 10
174 # - access:
175 # vlan: 30
176 # name: Ethernet1/2
177 # - name: mgmt0
178 # - name: Loopback999
179
180 # After state:
181 # ------------
182 #
183 # switch# show running-config | section interface
184 # interface Ethernet1/1
185 # switchport trunk native vlan 10
186 # switchport trunk allowed vlans 2,4,15
187 # interface Ethernet1/2
188 # switchport access vlan 30
189 # interface mgmt0
190 # ip address dhcp
191 # ipv6 address auto-config
192
193 # Using replaced
194
195 # Before state:
196 # -------------
197 #
198 # switch# show running-config | section interface
199 # interface Ethernet1/1
200 # switchport trunk native vlan 10
201 # switchport trunk allowed vlans 2,4,15
202 # interface Ethernet1/2
203 # switchport access vlan 30
204 # interface mgmt0
205 # ip address dhcp
206 # ipv6 address auto-config
207
208 - name: Replace device configuration of specified L2 interfaces with provided configuration.
209 cisco.nxos.nxos_l2_interfaces:
210 config:
211 - name: Ethernet1/1
212 trunk:
213 native_vlan: 20
214 allowed_vlans: 5-10, 15
215 state: replaced
216
217 # Task Output
218 # -----------
219 #
220 # before:
221 # - name: Ethernet1/1
222 # trunk:
223 # allowed_vlans: 2,4,15
224 # native_vlan: 10
225 # - access:
226 # vlan: 30
227 # name: Ethernet1/2
228 # - name: mgmt0
229 # commands:
230 # - interface Ethernet1/1
231 # - no switchport trunk native vlan
232 # - switchport trunk allowed vlan 5-10,15
233 # - switchport trunk native vlan 20
234 # after:
235 # - name: Ethernet1/1
236 # trunk:
237 # allowed_vlans: 5-10,15
238 # native_vlan: 20
239 # - access:
240 # vlan: 30
241 # name: Ethernet1/2
242 # - name: mgmt0
243
244 # After state:
245 # ------------
246 #
247 # switch# show running-config | section interface
248 # interface Ethernet1/1
249 # switchport trunk native vlan 20
250 # switchport trunk allowed vlan 5-10,15
251 # interface Ethernet1/2
252 # switchport trunk native vlan 20
253 # switchport mode trunk
254 # interface mgmt0
255 # ip address dhcp
256 # ipv6 address auto-config
257
258 # Using overridden
259
260 # Before state:
261 # -------------
262 #
263 # switch# show running-config | section interface
264 # interface Ethernet1/1
265 # switchport trunk native vlan 20
266 # switchport trunk allowed vlan 5-10,15
267 # interface Ethernet1/2
268 # switchport trunk native vlan 20
269 # switchport mode trunk
270 # interface mgmt0
271 # ip address dhcp
272 # ipv6 address auto-config
273
274 - name: Override device configuration with provided configuration.
275 cisco.nxos.nxos_l2_interfaces:
276 config:
277 - name: Ethernet1/2
278 access:
279 vlan: 30
280 state: overridden
281
282 # Task Output
283 # -----------
284 #
285 # before:
286 # - name: Ethernet1/1
287 # trunk:
288 # allowed_vlans: 5,6,7,8,9,10,15
289 # native_vlan: 20
290 # - access:
291 # vlan: 30
292 # name: Ethernet1/2
293 # - name: mgmt0
294 # commands:
295 # - interface Ethernet1/1
296 # - no switchport trunk allowed vlan
297 # - no switchport trunk native vlan
298 # after:
299 # - name: Ethernet1/1
300 # - access:
301 # vlan: 30
302 # name: Ethernet1/2
303 # - name: mgmt0
304
305 # After state:
306 # ------------
307 #
308 # switch# show running-config | section interface
309 # interface Ethernet1/1
310 # interface Ethernet1/2
311 # switchport access vlan 30
312 # interface mgmt0
313 # ip address dhcp
314 # ipv6 address auto-config
315
316
317 # Using deleted
318
319 # Before state:
320 # -------------
321 #
322 # switch# show running-config | section interface
323 # interface Ethernet1/1
324 # switchport trunk native vlan 10
325 # switchport trunk allowed vlan 2,4,15
326 # interface Ethernet1/2
327 # switchport access vlan 30
328 # interface mgmt0
329 # ip address dhcp
330 # ipv6 address auto-config
331
332 - name: Delete L2 attributes of given interfaces (Note This won't delete the interface
333 itself).
334 cisco.nxos.nxos_l2_interfaces:
335 config:
336 - name: Ethernet1/1
337 - name: Ethernet1/2
338 state: deleted
339
340 # Task Output
341 # -----------
342 #
343 # before:
344 # - name: Ethernet1/1
345 # trunk:
346 # allowed_vlans: 2,4,15
347 # native_vlan: 10
348 # - access:
349 # vlan: 30
350 # name: Ethernet1/2
351 # - name: mgmt0
352 # commands:
353 # - interface Ethernet1/1
354 # - no switchport trunk allowed vlan
355 # - no switchport trunk native vlan
356 # - interface Ethernet1/2
357 # - no switchport access vlan
358 # after:
359 # - name: Ethernet1/1
360 # - name: Ethernet1/2
361 # - name: mgmt0
362
363 # After state:
364 # ------------
365 #
366 # switch# show running-config | section interface
367 # interface Ethernet1/1
368 # interface Ethernet1/2
369 # interface mgmt0
370 # ip address dhcp
371 # ipv6 address auto-config
372
373 # Using rendered
374
375 - name: Render platform specific configuration lines (without connecting to the device)
376 cisco.nxos.nxos_l2_interfaces:
377 config:
378 - name: Ethernet1/1
379 trunk:
380 native_vlan: 10
381 allowed_vlans: 2,4,15
382 - name: Ethernet1/2
383 access:
384 vlan: 30
385 - name: Ethernet1/3
386 trunk:
387 native_vlan: 20
388 allowed_vlans: 5-10, 15
389 state: rendered
390
391 # Task Output
392 # -----------
393 #
394 # rendered:
395 # - interface Ethernet1/1
396 # - switchport trunk allowed vlan 2,4,15
397 # - switchport trunk native vlan 10
398 # - interface Ethernet1/2
399 # - switchport access vlan 30
400 # - interface Ethernet1/3
401 # - switchport trunk allowed vlan 5-10,15
402 # - switchport trunk native vlan 20
403
404 # Using parsed
405
406 # parsed.cfg
407 # ------------
408 #
409 # interface Ethernet1/800
410 # switchport access vlan 18
411 # switchport trunk allowed vlan 210
412 # interface Ethernet1/801
413 # switchport trunk allowed vlan 2,4,15
414
415 - name: Use parsed state to convert externally supplied config to structured format
416 cisco.nxos.nxos_l2_interfaces:
417 running_config: "{{ lookup('file', 'parsed.cfg') }}"
418 state: parsed
419
420 # Task output
421 # -----------
422 #
423 # parsed:
424 # - name: Ethernet1/800
425 # access:
426 # vlan: 18
427 # trunk:
428 # allowed_vlans: "210"
429 # - name: Ethernet1/801
430 # trunk:
431 # allowed_vlans: "2,4,15"
432
433 # Using gathered
434
435 # Before state:
436 # -------------
437 #
438 # switch# sh running-config | section ^interface
439 # interface Ethernet1/1
440 # switchport access vlan 6
441 # switchport trunk allowed vlan 200
442 # interface Ethernet1/2
443 # switchport trunk native vlan 10
444
445 - name: Gather l2_interfaces facts from the device using nxos_l2_interfaces
446 cisco.nxos.nxos_l2_interfaces:
447 state: gathered
448
449 # Task output
450 # -----------
451 #
452 # gathered:
453 # - name: "Ethernet1/1"
454 # access:
455 # vlan: 6
456 # trunk:
457 # allowed_vlans: "200"
458 # - name: "Ethernet1/2"
459 # trunk:
460 # native_vlan: 10
461
462 # Using allowed_vlans_none
463
464 # Before state:
465 # -------------
466 #
467 # switch# show running-config | section interface
468 # interface Ethernet1/1
469 # switchport mode trunk
470 # switchport trunk allowed vlan 10-100
471 # interface Ethernet1/2
472 # switchport mode trunk
473
474 - name: Configure trunk interface to allow no VLANs (block all traffic)
475 cisco.nxos.nxos_l2_interfaces:
476 config:
477 - name: Ethernet1/1
478 trunk:
479 allowed_vlans_none: true
480 state: merged
481
482 # Task Output
483 # -----------
484 #
485 # before:
486 # - name: Ethernet1/1
487 # trunk:
488 # allowed_vlans: "10-100"
489 # - name: Ethernet1/2
490 # trunk:
491 # allowed_vlans: "1-4094"
492 # commands:
493 # - interface Ethernet1/1
494 # - switchport trunk allowed vlan none
495 # after:
496 # - name: Ethernet1/1
497 # trunk:
498 # allowed_vlans_none: true
499 # - name: Ethernet1/2
500 # trunk:
501 # allowed_vlans: "1-4094"
502
503 # After state:
504 # ------------
505 #
506 # switch# show running-config | section interface
507 # interface Ethernet1/1
508 # switchport mode trunk
509 # switchport trunk allowed vlan none
510 # interface Ethernet1/2
511 # switchport mode trunk
512
513 # Note: When allowed_vlans_none is not configured on a trunk interface,
514 # the module assumes the default allowed VLANs range of 1-4094.
515 # This is consistent with NX-OS default behavior where all VLANs are
516 # allowed on trunk ports unless explicitly restricted.
517
518 - name: Configure specific VLANs after removing none restriction
519 cisco.nxos.nxos_l2_interfaces:
520 config:
521 - name: Ethernet1/1
522 trunk:
523 allowed_vlans: "10,20,30"
524 - name: Ethernet1/2
525 trunk:
526 allowed_vlans_none: true
527 state: replaced
528
529 # Task Output
530 # -----------
531 #
532 # commands:
533 # - interface Ethernet1/1
534 # - switchport trunk allowed vlan 10,20,30
535 # - interface Ethernet1/2
536 # - switchport trunk allowed vlan none
537 """
538
539 RETURN = """
540 before:
541 description: The configuration as structured data prior to module invocation.
542 returned: always
543 type: list
544 sample: >
545 The configuration returned will always be in the same format
546 of the parameters above.
547 after:
548 description: The configuration as structured data after module completion.
549 returned: when changed
550 type: list
551 sample: >
552 The configuration returned will always be in the same format
553 of the parameters above.
554 commands:
555 description: The set of commands pushed to the remote device.
556 returned: always
557 type: list
558 sample:
559 - "interface Ethernet1/1"
560 - "switchport trunk allowed vlan 2,4,15"
561 - "switchport trunk native vlan 10"
562 - "interface Ethernet1/2"
563 - "switchport access vlan 30"
564 - "interface Ethernet1/3"
565 - "switchport trunk allowed vlan 5,6,7,8,9,10,15"
566 - "switchport trunk native vlan 20"
567 gathered:
568 description: Facts about the network resource gathered from the remote device as structured data.
569 returned: when I(state) is C(gathered)
570 type: list
571 sample: >
572 This output will always be in the same format as the
573 module argspec.
574 parsed:
575 description: The device native config provided in I(running_config) option parsed into structured data as per module argspec.
576 returned: when I(state) is C(parsed)
577 type: list
578 sample: >
579 This output will always be in the same format as the
580 module argspec.
581 """
582
583 from ansible.module_utils.basic import AnsibleModule
584
585 from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.argspec.l2_interfaces.l2_interfaces import (
586 L2_interfacesArgs,
587 )
588 from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.config.l2_interfaces.l2_interfaces import (
589 L2_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=L2_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 = L2_interfaces(module).execute_module()
613 module.exit_json(**result)
614
615
616 if __name__ == "__main__":
617 main()