| 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 |
The module file for ios_lacp_interfaces |
| 20 |
""" |
| 21 |
from __future__ import absolute_import, division, print_function |
| 22 |
|
| 23 |
|
| 24 |
__metaclass__ = type |
| 25 |
|
| 26 |
|
| 27 |
DOCUMENTATION = """ |
| 28 |
module: ios_lacp_interfaces |
| 29 |
short_description: Resource module to configure LACP interfaces. |
| 30 |
description: |
| 31 |
This module provides declarative management of LACP on Cisco IOS network |
| 32 |
devices lacp_interfaces. |
| 33 |
version_added: 1.0.0 |
| 34 |
author: Sumit Jaiswal (@justjais) |
| 35 |
notes: |
| 36 |
- Tested against Cisco IOSXE Version 17.3 on CML. |
| 37 |
- This module works with connection C(network_cli). |
| 38 |
See U(https://docs.ansible.com/ansible/latest/network/user_guide/platform_ios.html) |
| 39 |
options: |
| 40 |
config: |
| 41 |
description: A dictionary of LACP lacp_interfaces option |
| 42 |
type: list |
| 43 |
elements: dict |
| 44 |
suboptions: |
| 45 |
name: |
| 46 |
description: |
| 47 |
- Name of the Interface for configuring LACP. |
| 48 |
type: str |
| 49 |
required: true |
| 50 |
port_priority: |
| 51 |
description: |
| 52 |
- LACP priority on this interface. |
| 53 |
- Refer to vendor documentation for valid port values. |
| 54 |
type: int |
| 55 |
fast_switchover: |
| 56 |
description: |
| 57 |
- LACP fast switchover supported on this port channel. |
| 58 |
type: bool |
| 59 |
max_bundle: |
| 60 |
description: |
| 61 |
- LACP maximum number of ports to bundle in this port channel. |
| 62 |
- Refer to vendor documentation for valid port values. |
| 63 |
type: int |
| 64 |
running_config: |
| 65 |
description: |
| 66 |
- This option is used only with state I(parsed). |
| 67 |
- The value of this option should be the output received from the IOS device by |
| 68 |
executing the command B(show running-config | section ^interface). |
| 69 |
- The state I(parsed) reads the configuration from C(running_config) option and |
| 70 |
transforms it into Ansible structured data as per the resource module's argspec |
| 71 |
and the value is then returned in the I(parsed) key within the result. |
| 72 |
type: str |
| 73 |
state: |
| 74 |
description: |
| 75 |
- The state the configuration should be left in |
| 76 |
- The states I(rendered), I(gathered) and I(parsed) does not perform any change |
| 77 |
on the device. |
| 78 |
- The state I(rendered) will transform the configuration in C(config) option to |
| 79 |
platform specific CLI commands which will be returned in the I(rendered) key |
| 80 |
within the result. For state I(rendered) active connection to remote host is |
| 81 |
not required. |
| 82 |
- The state I(gathered) will fetch the running configuration from device and transform |
| 83 |
it into structured data in the format as per the resource module argspec and |
| 84 |
the value is returned in the I(gathered) key within the result. |
| 85 |
- The state I(parsed) reads the configuration from C(running_config) option and |
| 86 |
transforms it into JSON format as per the resource module parameters and the |
| 87 |
value is returned in the I(parsed) key within the result. The value of C(running_config) |
| 88 |
option should be the same format as the output of command I(show running-config |
| 89 |
| include ip route|ipv6 route) executed on device. For state I(parsed) active |
| 90 |
connection to remote host is not required. |
| 91 |
type: str |
| 92 |
choices: |
| 93 |
- merged |
| 94 |
- replaced |
| 95 |
- overridden |
| 96 |
- deleted |
| 97 |
- rendered |
| 98 |
- gathered |
| 99 |
- parsed |
| 100 |
default: merged |
| 101 |
""" |
| 102 |
|
| 103 |
EXAMPLES = """ |
| 104 |
# Using merged |
| 105 |
# |
| 106 |
# Before state: |
| 107 |
# ------------- |
| 108 |
# |
| 109 |
# vios#show running-config | section ^interface |
| 110 |
# interface Port-channel10 |
| 111 |
# interface Port-channel20 |
| 112 |
# interface Port-channel30 |
| 113 |
# interface GigabitEthernet0/1 |
| 114 |
# shutdown |
| 115 |
# interface GigabitEthernet0/2 |
| 116 |
# shutdown |
| 117 |
# interface GigabitEthernet0/3 |
| 118 |
# shutdown |
| 119 |
|
| 120 |
- name: Merge provided configuration with device configuration |
| 121 |
cisco.ios.ios_lacp_interfaces: |
| 122 |
config: |
| 123 |
- name: GigabitEthernet0/1 |
| 124 |
port_priority: 10 |
| 125 |
- name: GigabitEthernet0/2 |
| 126 |
port_priority: 20 |
| 127 |
- name: GigabitEthernet0/3 |
| 128 |
port_priority: 30 |
| 129 |
- name: Port-channel10 |
| 130 |
fast_switchover: true |
| 131 |
max_bundle: 5 |
| 132 |
state: merged |
| 133 |
|
| 134 |
# After state: |
| 135 |
# ------------ |
| 136 |
# |
| 137 |
# vios#show running-config | section ^interface |
| 138 |
# interface Port-channel10 |
| 139 |
# lacp fast-switchover |
| 140 |
# lacp max-bundle 5 |
| 141 |
# interface Port-channel20 |
| 142 |
# interface Port-channel30 |
| 143 |
# interface GigabitEthernet0/1 |
| 144 |
# shutdown |
| 145 |
# lacp port-priority 10 |
| 146 |
# interface GigabitEthernet0/2 |
| 147 |
# shutdown |
| 148 |
# lacp port-priority 20 |
| 149 |
# interface GigabitEthernet0/3 |
| 150 |
# shutdown |
| 151 |
# lacp port-priority 30 |
| 152 |
|
| 153 |
# Using overridden |
| 154 |
# |
| 155 |
# Before state: |
| 156 |
# ------------- |
| 157 |
# |
| 158 |
# vios#show running-config | section ^interface |
| 159 |
# interface Port-channel10 |
| 160 |
# lacp fast-switchover |
| 161 |
# interface Port-channel20 |
| 162 |
# interface Port-channel30 |
| 163 |
# interface GigabitEthernet0/1 |
| 164 |
# shutdown |
| 165 |
# lacp port-priority 10 |
| 166 |
# interface GigabitEthernet0/2 |
| 167 |
# shutdown |
| 168 |
# lacp port-priority 20 |
| 169 |
# interface GigabitEthernet0/3 |
| 170 |
# shutdown |
| 171 |
# lacp port-priority 30 |
| 172 |
|
| 173 |
- name: Override device configuration of all lacp_interfaces with provided configuration |
| 174 |
cisco.ios.ios_lacp_interfaces: |
| 175 |
config: |
| 176 |
- name: GigabitEthernet0/1 |
| 177 |
port_priority: 20 |
| 178 |
- name: Port-channel10 |
| 179 |
max_bundle: 2 |
| 180 |
state: overridden |
| 181 |
|
| 182 |
# After state: |
| 183 |
# ------------ |
| 184 |
# |
| 185 |
# vios#show running-config | section ^interface |
| 186 |
# interface Port-channel10 |
| 187 |
# lacp max-bundle 2 |
| 188 |
# interface Port-channel20 |
| 189 |
# interface Port-channel30 |
| 190 |
# interface GigabitEthernet0/1 |
| 191 |
# shutdown |
| 192 |
# lacp port-priority 20 |
| 193 |
# interface GigabitEthernet0/2 |
| 194 |
# shutdown |
| 195 |
# interface GigabitEthernet0/3 |
| 196 |
# shutdown |
| 197 |
|
| 198 |
# Using replaced |
| 199 |
# |
| 200 |
# Before state: |
| 201 |
# ------------- |
| 202 |
# |
| 203 |
# vios#show running-config | section ^interface |
| 204 |
# interface Port-channel10 |
| 205 |
# lacp max-bundle 5 |
| 206 |
# interface Port-channel20 |
| 207 |
# interface Port-channel30 |
| 208 |
# interface GigabitEthernet0/1 |
| 209 |
# shutdown |
| 210 |
# lacp port-priority 10 |
| 211 |
# interface GigabitEthernet0/2 |
| 212 |
# shutdown |
| 213 |
# lacp port-priority 20 |
| 214 |
# interface GigabitEthernet0/3 |
| 215 |
# shutdown |
| 216 |
# lacp port-priority 30 |
| 217 |
|
| 218 |
- name: Replaces device configuration of listed lacp_interfaces with provided configuration |
| 219 |
cisco.ios.ios_lacp_interfaces: |
| 220 |
config: |
| 221 |
- name: GigabitEthernet0/3 |
| 222 |
port_priority: 40 |
| 223 |
- name: Port-channel10 |
| 224 |
fast_switchover: true |
| 225 |
max_bundle: 2 |
| 226 |
state: replaced |
| 227 |
|
| 228 |
# After state: |
| 229 |
# ------------ |
| 230 |
# |
| 231 |
# vios#show running-config | section ^interface |
| 232 |
# interface Port-channel10 |
| 233 |
# lacp fast-switchover |
| 234 |
# lacp max-bundle 2 |
| 235 |
# interface Port-channel20 |
| 236 |
# interface Port-channel30 |
| 237 |
# interface GigabitEthernet0/1 |
| 238 |
# shutdown |
| 239 |
# lacp port-priority 10 |
| 240 |
# interface GigabitEthernet0/2 |
| 241 |
# shutdown |
| 242 |
# lacp port-priority 20 |
| 243 |
# interface GigabitEthernet0/3 |
| 244 |
# shutdown |
| 245 |
# lacp port-priority 40 |
| 246 |
|
| 247 |
# Using Deleted |
| 248 |
# |
| 249 |
# Before state: |
| 250 |
# ------------- |
| 251 |
# |
| 252 |
# vios#show running-config | section ^interface |
| 253 |
# interface Port-channel10 |
| 254 |
# flowcontrol receive on |
| 255 |
# interface Port-channel20 |
| 256 |
# interface Port-channel30 |
| 257 |
# interface GigabitEthernet0/1 |
| 258 |
# shutdown |
| 259 |
# lacp port-priority 10 |
| 260 |
# interface GigabitEthernet0/2 |
| 261 |
# shutdown |
| 262 |
# lacp port-priority 20 |
| 263 |
# interface GigabitEthernet0/3 |
| 264 |
# shutdown |
| 265 |
# lacp port-priority 30 |
| 266 |
|
| 267 |
- name: "Delete LACP attributes of given interfaces (Note: This won't delete the interface itself)" |
| 268 |
cisco.ios.ios_lacp_interfaces: |
| 269 |
config: |
| 270 |
- name: GigabitEthernet0/1 |
| 271 |
state: deleted |
| 272 |
|
| 273 |
# After state: |
| 274 |
# ------------- |
| 275 |
# |
| 276 |
# vios#show running-config | section ^interface |
| 277 |
# interface Port-channel10 |
| 278 |
# interface Port-channel20 |
| 279 |
# interface Port-channel30 |
| 280 |
# interface GigabitEthernet0/1 |
| 281 |
# shutdown |
| 282 |
# interface GigabitEthernet0/2 |
| 283 |
# shutdown |
| 284 |
# lacp port-priority 20 |
| 285 |
# interface GigabitEthernet0/3 |
| 286 |
# shutdown |
| 287 |
# lacp port-priority 30 |
| 288 |
|
| 289 |
# Using Deleted without any config passed |
| 290 |
# "(NOTE: This will delete all of configured LLDP module attributes)" |
| 291 |
# |
| 292 |
# Before state: |
| 293 |
# ------------- |
| 294 |
# |
| 295 |
# vios#show running-config | section ^interface |
| 296 |
# interface Port-channel10 |
| 297 |
# lacp fast-switchover |
| 298 |
# interface Port-channel20 |
| 299 |
# lacp max-bundle 2 |
| 300 |
# interface Port-channel30 |
| 301 |
# interface GigabitEthernet0/1 |
| 302 |
# shutdown |
| 303 |
# lacp port-priority 10 |
| 304 |
# interface GigabitEthernet0/2 |
| 305 |
# shutdown |
| 306 |
# lacp port-priority 20 |
| 307 |
# interface GigabitEthernet0/3 |
| 308 |
# shutdown |
| 309 |
# lacp port-priority 30 |
| 310 |
|
| 311 |
- name: "Delete LACP attributes for all configured interfaces (Note: This won't delete the interface itself)" |
| 312 |
cisco.ios.ios_lacp_interfaces: |
| 313 |
state: deleted |
| 314 |
|
| 315 |
# After state: |
| 316 |
# ------------- |
| 317 |
# |
| 318 |
# vios#show running-config | section ^interface |
| 319 |
# interface Port-channel10 |
| 320 |
# interface Port-channel20 |
| 321 |
# interface Port-channel30 |
| 322 |
# interface GigabitEthernet0/1 |
| 323 |
# shutdown |
| 324 |
# interface GigabitEthernet0/2 |
| 325 |
# shutdown |
| 326 |
# interface GigabitEthernet0/3 |
| 327 |
# shutdown |
| 328 |
|
| 329 |
# Using Gathered |
| 330 |
|
| 331 |
# Before state: |
| 332 |
# ------------- |
| 333 |
# |
| 334 |
# vios#sh running-config | section ^interface |
| 335 |
# interface Port-channel10 |
| 336 |
# lacp fast-switchover |
| 337 |
# lacp max-bundle 2 |
| 338 |
# interface Port-channel40 |
| 339 |
# lacp max-bundle 5 |
| 340 |
# interface GigabitEthernet0/0 |
| 341 |
# interface GigabitEthernet0/1 |
| 342 |
# lacp port-priority 30 |
| 343 |
# interface GigabitEthernet0/2 |
| 344 |
# lacp port-priority 20 |
| 345 |
|
| 346 |
- name: Gather listed LACP interfaces with provided configurations |
| 347 |
cisco.ios.ios_lacp_interfaces: |
| 348 |
config: |
| 349 |
state: gathered |
| 350 |
|
| 351 |
# Module Execution Result: |
| 352 |
# ------------------------ |
| 353 |
# |
| 354 |
# "gathered": [ |
| 355 |
# { |
| 356 |
# "fast_switchover": true, |
| 357 |
# "max_bundle": 2, |
| 358 |
# "name": "Port-channel10" |
| 359 |
# }, |
| 360 |
# { |
| 361 |
# "max_bundle": 5, |
| 362 |
# "name": "Port-channel40" |
| 363 |
# }, |
| 364 |
# { |
| 365 |
# "name": "GigabitEthernet0/0" |
| 366 |
# }, |
| 367 |
# { |
| 368 |
# "name": "GigabitEthernet0/1", |
| 369 |
# "port_priority": 30 |
| 370 |
# }, |
| 371 |
# { |
| 372 |
# "name": "GigabitEthernet0/2", |
| 373 |
# "port_priority": 20 |
| 374 |
# } |
| 375 |
# ] |
| 376 |
|
| 377 |
# After state: |
| 378 |
# ------------ |
| 379 |
# |
| 380 |
# vios#sh running-config | section ^interface |
| 381 |
# interface Port-channel10 |
| 382 |
# lacp fast-switchover |
| 383 |
# lacp max-bundle 2 |
| 384 |
# interface Port-channel40 |
| 385 |
# lacp max-bundle 5 |
| 386 |
# interface GigabitEthernet0/0 |
| 387 |
# interface GigabitEthernet0/1 |
| 388 |
# lacp port-priority 30 |
| 389 |
# interface GigabitEthernet0/2 |
| 390 |
# lacp port-priority 20 |
| 391 |
|
| 392 |
# Using Rendered |
| 393 |
|
| 394 |
- name: Render the commands for provided configuration |
| 395 |
cisco.ios.ios_lacp_interfaces: |
| 396 |
config: |
| 397 |
- name: GigabitEthernet0/1 |
| 398 |
port_priority: 10 |
| 399 |
- name: GigabitEthernet0/2 |
| 400 |
port_priority: 20 |
| 401 |
- name: Port-channel10 |
| 402 |
fast_switchover: true |
| 403 |
max_bundle: 2 |
| 404 |
state: rendered |
| 405 |
|
| 406 |
# Module Execution Result: |
| 407 |
# ------------------------ |
| 408 |
# |
| 409 |
# "rendered": [ |
| 410 |
# "interface GigabitEthernet0/1", |
| 411 |
# "lacp port-priority 10", |
| 412 |
# "interface GigabitEthernet0/2", |
| 413 |
# "lacp port-priority 20", |
| 414 |
# "interface Port-channel10", |
| 415 |
# "lacp max-bundle 2", |
| 416 |
# "lacp fast-switchover" |
| 417 |
# ] |
| 418 |
|
| 419 |
# Using Parsed |
| 420 |
|
| 421 |
# File: parsed.cfg |
| 422 |
# ---------------- |
| 423 |
# |
| 424 |
# interface GigabitEthernet0/1 |
| 425 |
# lacp port-priority 10 |
| 426 |
# interface GigabitEthernet0/2 |
| 427 |
# lacp port-priority 20 |
| 428 |
# interface Port-channel10 |
| 429 |
# lacp max-bundle 2 fast-switchover |
| 430 |
|
| 431 |
- name: Parse the commands for provided configuration |
| 432 |
cisco.ios.ios_lacp_interfaces: |
| 433 |
running_config: "{{ lookup('file', 'parsed.cfg') }}" |
| 434 |
state: parsed |
| 435 |
|
| 436 |
# Module Execution Result: |
| 437 |
# ------------------------ |
| 438 |
# |
| 439 |
# "parsed": [ |
| 440 |
# { |
| 441 |
# "name": "GigabitEthernet0/1", |
| 442 |
# "port_priority": 10 |
| 443 |
# }, |
| 444 |
# { |
| 445 |
# "name": "GigabitEthernet0/2", |
| 446 |
# "port_priority": 20 |
| 447 |
# }, |
| 448 |
# { |
| 449 |
# "fast_switchover": true, |
| 450 |
# "max_bundle": 2, |
| 451 |
# "name": "Port-channel10" |
| 452 |
# } |
| 453 |
# ] |
| 454 |
""" |
| 455 |
|
| 456 |
RETURN = """ |
| 457 |
before: |
| 458 |
description: The configuration as structured data prior to module invocation. |
| 459 |
returned: always |
| 460 |
type: list |
| 461 |
sample: > |
| 462 |
The configuration returned will always be in the same format |
| 463 |
of the parameters above. |
| 464 |
after: |
| 465 |
description: The configuration as structured data after module completion. |
| 466 |
returned: when changed |
| 467 |
type: list |
| 468 |
sample: > |
| 469 |
The configuration returned will always be in the same format |
| 470 |
of the parameters above. |
| 471 |
commands: |
| 472 |
description: The set of commands pushed to the remote device. |
| 473 |
returned: always |
| 474 |
type: list |
| 475 |
sample: ['interface GigabitEthernet 0/1', 'lacp port-priority 30'] |
| 476 |
""" |
| 477 |
from ansible.module_utils.basic import AnsibleModule |
| 478 |
|
| 479 |
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.argspec.lacp_interfaces.lacp_interfaces import ( |
| 480 |
Lacp_InterfacesArgs, |
| 481 |
) |
| 482 |
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.config.lacp_interfaces.lacp_interfaces import ( |
| 483 |
Lacp_Interfaces, |
| 484 |
) |
| 485 |
|
| 486 |
|
| 487 |
def main(): |
| 488 |
""" |
| 489 |
Main entry point for module execution |
| 490 |
|
| 491 |
:returns: the result form module invocation |
| 492 |
""" |
| 493 |
required_if = [ |
| 494 |
("state", "merged", ("config",)), |
| 495 |
("state", "replaced", ("config",)), |
| 496 |
("state", "overridden", ("config",)), |
| 497 |
("state", "rendered", ("config",)), |
| 498 |
("state", "parsed", ("running_config",)), |
| 499 |
] |
| 500 |
mutually_exclusive = [("config", "running_config")] |
| 501 |
|
| 502 |
module = AnsibleModule( |
| 503 |
argument_spec=Lacp_InterfacesArgs.argument_spec, |
| 504 |
required_if=required_if, |
| 505 |
mutually_exclusive=mutually_exclusive, |
| 506 |
supports_check_mode=True, |
| 507 |
) |
| 508 |
result = Lacp_Interfaces(module).execute_module() |
| 509 |
module.exit_json(**result) |
| 510 |
|
| 511 |
|
| 512 |
if __name__ == "__main__": |
| 513 |
main() |