| 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_interfaces |
| 9 |
""" |
| 10 |
|
| 11 |
from __future__ import absolute_import, division, print_function |
| 12 |
|
| 13 |
|
| 14 |
__metaclass__ = type |
| 15 |
|
| 16 |
DOCUMENTATION = """ |
| 17 |
module: nxos_interfaces |
| 18 |
short_description: Interfaces resource module |
| 19 |
description: This module manages the interface 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.4(2) on CML |
| 26 |
- Unsupported for Cisco MDS |
| 27 |
- This module assumes the device is configured with the default switchport |
| 28 |
behavior, specifically that C(system default switchport shutdown) is not |
| 29 |
enabled. If C(system default switchport shutdown) is enabled on the device, |
| 30 |
the module will not function correctly. The only resolution is to revert |
| 31 |
the device to the default setting using C(no system default switchport shutdown). |
| 32 |
There is currently no workaround for devices with this non-default configuration. |
| 33 |
options: |
| 34 |
config: |
| 35 |
description: A dictionary of interface options |
| 36 |
type: list |
| 37 |
elements: dict |
| 38 |
suboptions: |
| 39 |
name: |
| 40 |
description: |
| 41 |
- 'Full name of interface, e.g. Ethernet1/1, port-channel10.' |
| 42 |
type: str |
| 43 |
required: true |
| 44 |
description: |
| 45 |
description: |
| 46 |
- Interface description. |
| 47 |
type: str |
| 48 |
enabled: |
| 49 |
description: |
| 50 |
- Administrative state of the interface. Set the value to C(true) to |
| 51 |
administratively enable the interface or C(false) to disable it |
| 52 |
type: bool |
| 53 |
speed: |
| 54 |
description: |
| 55 |
- Interface link speed. Applicable for Ethernet interfaces only. |
| 56 |
type: str |
| 57 |
mode: |
| 58 |
description: |
| 59 |
- Manage Layer2 or Layer3 state of the interface. Applicable for |
| 60 |
Ethernet and port channel interfaces only. |
| 61 |
choices: |
| 62 |
- layer2 |
| 63 |
- layer3 |
| 64 |
type: str |
| 65 |
mtu: |
| 66 |
description: |
| 67 |
- MTU for a specific interface. Must be an even number between 576 and |
| 68 |
9216. Applicable for Ethernet interfaces only. |
| 69 |
type: str |
| 70 |
duplex: |
| 71 |
description: |
| 72 |
- Interface link status. Applicable for Ethernet interfaces only. |
| 73 |
type: str |
| 74 |
choices: |
| 75 |
- full |
| 76 |
- half |
| 77 |
- auto |
| 78 |
ip_forward: |
| 79 |
description: |
| 80 |
- Enable or disable IP forward feature on SVIs. Set the value to |
| 81 |
C(true) to enable or C(false) to disable. |
| 82 |
type: bool |
| 83 |
fabric_forwarding_anycast_gateway: |
| 84 |
description: |
| 85 |
- Associate SVI with anycast gateway under VLAN configuration mode. |
| 86 |
Applicable for SVI interfaces only. |
| 87 |
type: bool |
| 88 |
mac_address: |
| 89 |
description: E.E.E MAC address. |
| 90 |
type: str |
| 91 |
logging: |
| 92 |
description: "Logging interface events" |
| 93 |
type: "dict" |
| 94 |
suboptions: |
| 95 |
link_status: |
| 96 |
description: "UPDOWN and CHANGE messages" |
| 97 |
type: bool |
| 98 |
trunk_status: |
| 99 |
description: "TRUNK status messages" |
| 100 |
type: bool |
| 101 |
snmp: |
| 102 |
description: "Modify SNMP interface parameters" |
| 103 |
type: dict |
| 104 |
suboptions: |
| 105 |
trap: |
| 106 |
description: Allow a specific SNMP trap |
| 107 |
type: dict |
| 108 |
suboptions: |
| 109 |
link_status: |
| 110 |
description: Allow SNMP LINKUP and LINKDOWN traps (snmp trap link-status permit duplicates) |
| 111 |
type: bool |
| 112 |
service_policy: |
| 113 |
description: Service policy configuration |
| 114 |
type: dict |
| 115 |
suboptions: |
| 116 |
input: |
| 117 |
description: Assign policy-map to the input of an interface |
| 118 |
type: str |
| 119 |
output: |
| 120 |
description: Assign policy-map to the output of an interface |
| 121 |
type: str |
| 122 |
type_options: |
| 123 |
description: Specify the type of this policy |
| 124 |
type: dict |
| 125 |
suboptions: |
| 126 |
qos: |
| 127 |
description: Configure qos Service Policy |
| 128 |
type: dict |
| 129 |
suboptions: |
| 130 |
input: |
| 131 |
description: Assign policy-map to the input of an interface |
| 132 |
type: str |
| 133 |
output: |
| 134 |
description: Assign policy-map to the output of an interface |
| 135 |
type: str |
| 136 |
queuing: |
| 137 |
description: Configure queuing Service Policy |
| 138 |
type: dict |
| 139 |
suboptions: |
| 140 |
input: |
| 141 |
description: Assign policy-map to the input of an interface |
| 142 |
type: str |
| 143 |
output: |
| 144 |
description: Assign policy-map to the output of an interface |
| 145 |
type: str |
| 146 |
running_config: |
| 147 |
description: |
| 148 |
- This option is used only with state I(parsed). |
| 149 |
- The value of this option should be the output received from the NX-OS |
| 150 |
device by executing the command B(show running-config | section |
| 151 |
^interface) |
| 152 |
- The state I(parsed) reads the configuration from C(running_config) |
| 153 |
option and transforms it into Ansible structured data as per the |
| 154 |
resource module's argspec and the value is then returned in the |
| 155 |
I(parsed) key within the result. |
| 156 |
type: str |
| 157 |
state: |
| 158 |
description: |
| 159 |
- The state of the configuration after module completion |
| 160 |
- The state I(rendered) considers the system default mode for interfaces |
| 161 |
to be "Layer 3" and the system default state for interfaces to be |
| 162 |
shutdown. |
| 163 |
- The state I(purged) negates virtual interfaces that are specified in |
| 164 |
task from running-config. |
| 165 |
type: str |
| 166 |
choices: |
| 167 |
- merged |
| 168 |
- replaced |
| 169 |
- overridden |
| 170 |
- deleted |
| 171 |
- gathered |
| 172 |
- rendered |
| 173 |
- parsed |
| 174 |
- purged |
| 175 |
default: merged |
| 176 |
""" |
| 177 |
|
| 178 |
EXAMPLES = """ |
| 179 |
# Using merged |
| 180 |
|
| 181 |
# Before state: |
| 182 |
# ------------- |
| 183 |
# |
| 184 |
# switch# show running-config | section interface |
| 185 |
# interface Ethernet1/1 |
| 186 |
# description testing |
| 187 |
# interface mgmt0 |
| 188 |
# description mgmt interface |
| 189 |
# ip address dhcp |
| 190 |
# vrf member management |
| 191 |
|
| 192 |
- name: Merge provided configuration with device configuration |
| 193 |
cisco.nxos.nxos_interfaces: |
| 194 |
config: |
| 195 |
- name: Ethernet1/1 |
| 196 |
description: Configured by Ansible |
| 197 |
enabled: true |
| 198 |
- name: Ethernet1/2 |
| 199 |
description: Configured by Ansible Network |
| 200 |
enabled: false |
| 201 |
state: merged |
| 202 |
|
| 203 |
# Task Output |
| 204 |
# ----------- |
| 205 |
# |
| 206 |
# before: |
| 207 |
# - description: testing |
| 208 |
# name: Ethernet1/1 |
| 209 |
# - description: mgmt interface |
| 210 |
# name: mgmt0 |
| 211 |
# commands: |
| 212 |
# - interface Ethernet1/1 |
| 213 |
# - description Configured by Ansible |
| 214 |
# - interface Ethernet1/2 |
| 215 |
# - description Configured by Ansible Network |
| 216 |
# - shutdown |
| 217 |
# after: |
| 218 |
# - description: Configured by Ansible |
| 219 |
# name: Ethernet1/1 |
| 220 |
# - description: Configured by Ansible Network |
| 221 |
# enabled: false |
| 222 |
# name: Ethernet1/2 |
| 223 |
# - description: mgmt interface |
| 224 |
# name: mgmt0 |
| 225 |
|
| 226 |
# After state: |
| 227 |
# ------------ |
| 228 |
# |
| 229 |
# switch# show running-config | section interface |
| 230 |
# interface Ethernet1/1 |
| 231 |
# description Configured by Ansible |
| 232 |
# interface Ethernet1/2 |
| 233 |
# description Configured by Ansible Network |
| 234 |
# shutdown |
| 235 |
# interface mgmt0 |
| 236 |
# description mgmt interface |
| 237 |
# ip address dhcp |
| 238 |
# vrf member management |
| 239 |
|
| 240 |
# Using replaced |
| 241 |
|
| 242 |
# Before state: |
| 243 |
# ------------- |
| 244 |
# |
| 245 |
# switch# show running-config | section interface |
| 246 |
# interface Ethernet1/1 |
| 247 |
# description Updated by Ansible |
| 248 |
# interface Ethernet1/2 |
| 249 |
# description Configured by Ansible Network |
| 250 |
# shutdown |
| 251 |
# interface mgmt0 |
| 252 |
# description mgmt interface |
| 253 |
# ip address dhcp |
| 254 |
# vrf member management |
| 255 |
|
| 256 |
- name: Replaces device configuration of listed interfaces with provided configuration |
| 257 |
cisco.nxos.nxos_interfaces: |
| 258 |
config: |
| 259 |
- name: Ethernet1/1 |
| 260 |
description: Configured by Ansible |
| 261 |
enabled: true |
| 262 |
mtu: 9000 |
| 263 |
- name: Ethernet1/2 |
| 264 |
description: Configured by Ansible Network |
| 265 |
enabled: false |
| 266 |
mode: layer2 |
| 267 |
state: replaced |
| 268 |
|
| 269 |
# Task Output |
| 270 |
# ----------- |
| 271 |
# |
| 272 |
# before: |
| 273 |
# - description: Updated by Ansible |
| 274 |
# name: Ethernet1/1 |
| 275 |
# - description: Configured by Ansible Network |
| 276 |
# enabled: false |
| 277 |
# name: Ethernet1/2 |
| 278 |
# - description: mgmt interface |
| 279 |
# name: mgmt0 |
| 280 |
# commands: |
| 281 |
# - interface Ethernet1/1 |
| 282 |
# - mtu 1500 |
| 283 |
# - interface Ethernet1/2 |
| 284 |
# - description Updated by Ansible |
| 285 |
# after: |
| 286 |
# - description: Updated by Ansible |
| 287 |
# name: Ethernet1/1 |
| 288 |
# - description: Updated by Ansible |
| 289 |
# enabled: false |
| 290 |
# name: Ethernet1/2 |
| 291 |
# - description: mgmt interface |
| 292 |
# name: mgmt0 |
| 293 |
|
| 294 |
# After state: |
| 295 |
# ------------ |
| 296 |
# |
| 297 |
# switch# show running-config | section interface |
| 298 |
# interface Ethernet1/1 |
| 299 |
# description Updated by Ansible |
| 300 |
# interface Ethernet1/2 |
| 301 |
# description Updated by Ansible |
| 302 |
# shutdown |
| 303 |
# interface mgmt0 |
| 304 |
# description mgmt interface |
| 305 |
# ip address dhcp |
| 306 |
# vrf member management |
| 307 |
|
| 308 |
# Using overridden |
| 309 |
|
| 310 |
# Before state: |
| 311 |
# ------------- |
| 312 |
# |
| 313 |
# switch# show running-config | section interface |
| 314 |
# interface Ethernet1/1 |
| 315 |
# description Updated by Ansible |
| 316 |
# interface Ethernet1/2 |
| 317 |
# description Updated by Ansible |
| 318 |
# shutdown |
| 319 |
# interface mgmt0 |
| 320 |
# description mgmt interface |
| 321 |
# ip address dhcp |
| 322 |
# vrf member management |
| 323 |
|
| 324 |
- name: Override device configuration of all interfaces with provided configuration |
| 325 |
cisco.nxos.nxos_interfaces: |
| 326 |
config: |
| 327 |
- name: Ethernet1/1 |
| 328 |
enabled: true |
| 329 |
- name: Ethernet1/2 |
| 330 |
description: Configured by Ansible Network |
| 331 |
enabled: false |
| 332 |
- description: mgmt interface |
| 333 |
name: mgmt0 |
| 334 |
state: overridden |
| 335 |
|
| 336 |
# Task Output |
| 337 |
# ----------- |
| 338 |
# |
| 339 |
# before: |
| 340 |
# - description: Updated by Ansible |
| 341 |
# name: Ethernet1/1 |
| 342 |
# - description: Updated by Ansible |
| 343 |
# enabled: false |
| 344 |
# name: Ethernet1/2 |
| 345 |
# - description: mgmt interface |
| 346 |
# name: mgmt0 |
| 347 |
# commands: |
| 348 |
# - interface Ethernet1/1 |
| 349 |
# - no description |
| 350 |
# - interface Ethernet1/2 |
| 351 |
# - description Configured by Ansible Network |
| 352 |
# after: |
| 353 |
# - name: Ethernet1/1 |
| 354 |
# - description: Configured by Ansible Network |
| 355 |
# enabled: false |
| 356 |
# name: Ethernet1/2 |
| 357 |
# - description: mgmt interface |
| 358 |
# name: mgmt0 |
| 359 |
|
| 360 |
# After state: |
| 361 |
# ------------ |
| 362 |
# |
| 363 |
# switch# show running-config | section interface |
| 364 |
# interface Ethernet1/1 |
| 365 |
# interface Ethernet1/2 |
| 366 |
# description Configured by Ansible Network |
| 367 |
# shutdown |
| 368 |
# interface mgmt0 |
| 369 |
# description mgmt interface |
| 370 |
# ip address dhcp |
| 371 |
# vrf member management |
| 372 |
|
| 373 |
# Using deleted |
| 374 |
|
| 375 |
# Before state: |
| 376 |
# ------------- |
| 377 |
# |
| 378 |
# switch# show running-config | section interface |
| 379 |
# interface Ethernet1/1 |
| 380 |
# interface Ethernet1/2 |
| 381 |
# description Configured by Ansible Network |
| 382 |
# shutdown |
| 383 |
# interface mgmt0 |
| 384 |
# description mgmt interface |
| 385 |
# ip address dhcp |
| 386 |
# vrf member management |
| 387 |
|
| 388 |
- name: Delete or return interface parameters to default settings |
| 389 |
cisco.nxos.nxos_interfaces: |
| 390 |
config: |
| 391 |
- name: Ethernet1/2 |
| 392 |
state: deleted |
| 393 |
|
| 394 |
# Task Output |
| 395 |
# ----------- |
| 396 |
# |
| 397 |
# before: |
| 398 |
# - name: Ethernet1/1 |
| 399 |
# - description: Configured by Ansible Network |
| 400 |
# enabled: false |
| 401 |
# name: Ethernet1/2 |
| 402 |
# - description: mgmt interface |
| 403 |
# name: mgmt0 |
| 404 |
# commands: |
| 405 |
# - interface Ethernet1/2 |
| 406 |
# - no description |
| 407 |
# - no shutdown |
| 408 |
# after: |
| 409 |
# - name: Ethernet1/1 |
| 410 |
# - name: Ethernet1/2 |
| 411 |
# - description: mgmt interface |
| 412 |
# name: mgmt0 |
| 413 |
|
| 414 |
# After state: |
| 415 |
# ------------ |
| 416 |
# |
| 417 |
# switch# show running-config | section interface |
| 418 |
# interface Ethernet1/1 |
| 419 |
# interface Ethernet1/2 |
| 420 |
# interface mgmt0 |
| 421 |
# description mgmt interface |
| 422 |
# ip address dhcp |
| 423 |
# vrf member management |
| 424 |
|
| 425 |
# Using rendered |
| 426 |
|
| 427 |
- name: Use rendered state to convert task input to device specific commands |
| 428 |
cisco.nxos.nxos_interfaces: |
| 429 |
config: |
| 430 |
- name: Ethernet1/1 |
| 431 |
description: outbound-intf |
| 432 |
mode: layer3 |
| 433 |
speed: 100 |
| 434 |
- name: Ethernet1/2 |
| 435 |
mode: layer2 |
| 436 |
enabled: true |
| 437 |
duplex: full |
| 438 |
state: rendered |
| 439 |
|
| 440 |
# Task Output |
| 441 |
# ----------- |
| 442 |
# |
| 443 |
# rendered: |
| 444 |
# - "interface Ethernet1/1" |
| 445 |
# - "description outbound-intf" |
| 446 |
# - "speed 100" |
| 447 |
# - "interface Ethernet1/2" |
| 448 |
# - "switchport" |
| 449 |
# - "duplex full" |
| 450 |
# - "no shutdown" |
| 451 |
|
| 452 |
# Using parsed |
| 453 |
|
| 454 |
# parsed.cfg |
| 455 |
# ------------ |
| 456 |
# |
| 457 |
# interface Ethernet1/800 |
| 458 |
# description test-1 |
| 459 |
# speed 1000 |
| 460 |
# shutdown |
| 461 |
# no switchport |
| 462 |
# duplex half |
| 463 |
# interface Ethernet1/801 |
| 464 |
# description test-2 |
| 465 |
# switchport |
| 466 |
# no shutdown |
| 467 |
# mtu 1800 |
| 468 |
|
| 469 |
- name: Use parsed state to convert externally supplied config to structured format |
| 470 |
cisco.nxos.nxos_interfaces: |
| 471 |
running_config: "{{ lookup('file', 'parsed.cfg') }}" |
| 472 |
state: parsed |
| 473 |
|
| 474 |
# Task output |
| 475 |
# ----------- |
| 476 |
# |
| 477 |
# parsed: |
| 478 |
# - description: "test-1" |
| 479 |
# duplex: "half" |
| 480 |
# enabled: false |
| 481 |
# mode: "layer3" |
| 482 |
# name: "Ethernet1/800" |
| 483 |
# speed: "1000" |
| 484 |
# - description: "test-2" |
| 485 |
# enabled: true |
| 486 |
# mode: "layer2" |
| 487 |
# mtu: "1800" |
| 488 |
# name: "Ethernet1/801" |
| 489 |
|
| 490 |
# Using gathered |
| 491 |
|
| 492 |
# Before state: |
| 493 |
# ------------- |
| 494 |
# |
| 495 |
# switch# show running-config | section interface |
| 496 |
# interface Ethernet1/1 |
| 497 |
# description outbound-intf |
| 498 |
# switchport |
| 499 |
# no shutdown |
| 500 |
# interface Ethernet1/2 |
| 501 |
# description intf-l3 |
| 502 |
# speed 1000 |
| 503 |
# interface Ethernet1/3 |
| 504 |
# interface Ethernet1/4 |
| 505 |
# interface Ethernet1/5 |
| 506 |
|
| 507 |
- name: Gather interfaces facts from the device using nxos_interfaces |
| 508 |
cisco.nxos.nxos_interfaces: |
| 509 |
state: gathered |
| 510 |
|
| 511 |
# Task output |
| 512 |
# ----------- |
| 513 |
# |
| 514 |
# - name: Ethernet1/1 |
| 515 |
# description: outbound-intf |
| 516 |
# mode: layer2 |
| 517 |
# enabled: True |
| 518 |
# - name: Ethernet1/2 |
| 519 |
# description: intf-l3 |
| 520 |
# speed: "1000" |
| 521 |
|
| 522 |
# Using purged |
| 523 |
|
| 524 |
# Before state: |
| 525 |
# ------------- |
| 526 |
# |
| 527 |
# switch# show running-config | section interface |
| 528 |
# interface Vlan1 |
| 529 |
# interface Vlan42 |
| 530 |
# mtu 1800 |
| 531 |
# interface port-channel10 |
| 532 |
# interface port-channel11 |
| 533 |
# interface Ethernet1/1 |
| 534 |
# interface Ethernet1/2 |
| 535 |
# interface Ethernet1/2.100 |
| 536 |
# description sub-intf |
| 537 |
|
| 538 |
- name: Purge virtual interfaces from running-config |
| 539 |
cisco.nxos.nxos_interfaces: |
| 540 |
config: |
| 541 |
- name: Vlan42 |
| 542 |
- name: port-channel10 |
| 543 |
- name: Ethernet1/2.100 |
| 544 |
state: purged |
| 545 |
|
| 546 |
# Task output |
| 547 |
# ------------ |
| 548 |
# |
| 549 |
# before: |
| 550 |
# - name: Vlan1 |
| 551 |
# - mtu: '1800' |
| 552 |
# name: Vlan42 |
| 553 |
# - name: port-channel10 |
| 554 |
# - name: port-channel11 |
| 555 |
# - name: Ethernet1/1 |
| 556 |
# - name: Ethernet1/2 |
| 557 |
# - description: sub-intf |
| 558 |
# name: Ethernet1/2.100 |
| 559 |
# commands: |
| 560 |
# - no interface port-channel10 |
| 561 |
# - no interface Ethernet1/2.100 |
| 562 |
# - no interface Vlan42 |
| 563 |
# after: |
| 564 |
# - name: Vlan1 |
| 565 |
# - name: port-channel11 |
| 566 |
# - name: Ethernet1/1 |
| 567 |
# - name: Ethernet1/2 |
| 568 |
|
| 569 |
# After state: |
| 570 |
# ------------- |
| 571 |
# |
| 572 |
# switch# show running-config | section interface |
| 573 |
# interface Vlan1 |
| 574 |
# interface port-channel11 |
| 575 |
# interface Ethernet1/1 |
| 576 |
# interface Ethernet1/2 |
| 577 |
""" |
| 578 |
|
| 579 |
RETURN = """ |
| 580 |
before: |
| 581 |
description: The configuration as structured data prior to module invocation. |
| 582 |
returned: always |
| 583 |
type: list |
| 584 |
sample: > |
| 585 |
The configuration returned will always be in the same format |
| 586 |
of the parameters above. |
| 587 |
after: |
| 588 |
description: The configuration as structured data after module completion. |
| 589 |
returned: when changed |
| 590 |
type: list |
| 591 |
sample: > |
| 592 |
The configuration returned will always be in the same format |
| 593 |
of the parameters above. |
| 594 |
commands: |
| 595 |
description: The set of commands pushed to the remote device. |
| 596 |
returned: always |
| 597 |
type: list |
| 598 |
sample: ['interface Ethernet1/1', 'mtu 1800'] |
| 599 |
""" |
| 600 |
|
| 601 |
from ansible.module_utils.basic import AnsibleModule |
| 602 |
|
| 603 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.argspec.interfaces.interfaces import ( |
| 604 |
InterfacesArgs, |
| 605 |
) |
| 606 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.config.interfaces.interfaces import ( |
| 607 |
Interfaces, |
| 608 |
) |
| 609 |
|
| 610 |
|
| 611 |
def main(): |
| 612 |
""" |
| 613 |
Main entry point for module execution |
| 614 |
|
| 615 |
:returns: the result form module invocation |
| 616 |
""" |
| 617 |
module = AnsibleModule( |
| 618 |
argument_spec=InterfacesArgs.argument_spec, |
| 619 |
mutually_exclusive=[["config", "running_config"]], |
| 620 |
required_if=[ |
| 621 |
["state", "merged", ["config"]], |
| 622 |
["state", "replaced", ["config"]], |
| 623 |
["state", "overridden", ["config"]], |
| 624 |
["state", "rendered", ["config"]], |
| 625 |
["state", "parsed", ["running_config"]], |
| 626 |
], |
| 627 |
supports_check_mode=True, |
| 628 |
) |
| 629 |
|
| 630 |
result = Interfaces(module).execute_module() |
| 631 |
module.exit_json(**result) |
| 632 |
|
| 633 |
|
| 634 |
if __name__ == "__main__": |
| 635 |
main() |