| 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 ios_bfd_templates |
| 9 |
""" |
| 10 |
|
| 11 |
from __future__ import absolute_import, division, print_function |
| 12 |
|
| 13 |
|
| 14 |
__metaclass__ = type |
| 15 |
|
| 16 |
DOCUMENTATION = """ |
| 17 |
module: ios_bfd_templates |
| 18 |
extends_documentation_fragment: |
| 19 |
- cisco.ios.ios |
| 20 |
short_description: Bidirectional Forwarding Detection (BFD) templates configurations |
| 21 |
description: |
| 22 |
- Manages Bidirectional Forwarding Detection (BFD) templates configurations |
| 23 |
version_added: 11.3.0 |
| 24 |
author: |
| 25 |
- Komal Desai (@komaldesai13) |
| 26 |
notes: |
| 27 |
- Tested against Cisco IOS XE Software, Version 17.13.01a on CML |
| 28 |
- This module works with connection C(network_cli) |
| 29 |
options: |
| 30 |
config: |
| 31 |
description: A dictionary of bfd template options |
| 32 |
type: list |
| 33 |
elements: dict |
| 34 |
suboptions: |
| 35 |
name: |
| 36 |
description: name of the BFD template to be used |
| 37 |
required: true |
| 38 |
type: str |
| 39 |
hop: |
| 40 |
description: type of template to be used |
| 41 |
required: true |
| 42 |
choices: |
| 43 |
- single_hop |
| 44 |
- multi_hop |
| 45 |
type: str |
| 46 |
interval: |
| 47 |
description: defines transmit interval between BFD packets |
| 48 |
type: dict |
| 49 |
suboptions: |
| 50 |
min_tx: |
| 51 |
description: The minimum interval in milliseconds that the local system desires for transmitting BFD control packets |
| 52 |
type: int |
| 53 |
min_rx: |
| 54 |
description: The minimum interval in milliseconds that the local system is capable of supporting between received BFD control packets |
| 55 |
type: int |
| 56 |
multiplier: |
| 57 |
description: Specifies the number of consecutive BFD control packets that must be missed from a BFD peer before BFD declares\ |
| 58 |
that the peer is unavailable and the Layer 3 BFD peer is informed of the failure. |
| 59 |
type: int |
| 60 |
dampening: |
| 61 |
description: enables session dampening |
| 62 |
type: dict |
| 63 |
suboptions: |
| 64 |
half_life_period: |
| 65 |
description: half-life period for the exponential decay algorithm, in minutes. |
| 66 |
type: int |
| 67 |
reuse_threshold: |
| 68 |
description: The threshold at which a dampened session is allowed to be reused (taken out of dampening), in milliseconds. |
| 69 |
type: int |
| 70 |
suppress_threshold: |
| 71 |
description: The threshold at which a session is suppressed (put into dampening), in milliseconds. |
| 72 |
type: int |
| 73 |
max_suppress_time: |
| 74 |
description: The maximum amount of time a session can be suppressed, in minutes. |
| 75 |
type: int |
| 76 |
echo: |
| 77 |
description: enables the BFD echo function for all interfaces which uses this specific template. |
| 78 |
type: bool |
| 79 |
authentication: |
| 80 |
description: Configure authentication for the BFD template |
| 81 |
type: dict |
| 82 |
suboptions: |
| 83 |
type: |
| 84 |
description: Authentication type to use for BFD sessions |
| 85 |
type: str |
| 86 |
choices: |
| 87 |
- sha_1 |
| 88 |
- md5 |
| 89 |
- meticulous_md5 |
| 90 |
- meticulous_sha_1 |
| 91 |
keychain: |
| 92 |
description: Name of the key chain to use for authentication |
| 93 |
type: str |
| 94 |
running_config: |
| 95 |
description: |
| 96 |
- This option is used only with state I(parsed). |
| 97 |
- The value of this option should be the output received from the IOS device by |
| 98 |
executing the command B(show running-config | section ^bfd-template). |
| 99 |
- The state I(parsed) reads the configuration from C(running_config) option and |
| 100 |
transforms it into Ansible structured data as per the resource module's argspec |
| 101 |
and the value is then returned in the I(parsed) key within the result. |
| 102 |
type: str |
| 103 |
state: |
| 104 |
choices: |
| 105 |
- merged |
| 106 |
- replaced |
| 107 |
- overridden |
| 108 |
- deleted |
| 109 |
- rendered |
| 110 |
- gathered |
| 111 |
- purged |
| 112 |
- parsed |
| 113 |
default: merged |
| 114 |
description: |
| 115 |
- The state the configuration should be left in |
| 116 |
- The states I(rendered), I(gathered) and I(parsed) does not perform any change |
| 117 |
on the device. |
| 118 |
- The state I(rendered) will transform the configuration in C(config) option to |
| 119 |
platform specific CLI commands which will be returned in the I(rendered) key |
| 120 |
within the result. For state I(rendered) active connection to remote host is |
| 121 |
not required. |
| 122 |
- The state I(gathered) will fetch the running configuration from device and transform |
| 123 |
it into structured data in the format as per the resource module argspec and |
| 124 |
the value is returned in the I(gathered) key within the result. |
| 125 |
- The state I(parsed) reads the configuration from C(running_config) option and |
| 126 |
transforms it into JSON format as per the resource module parameters and the |
| 127 |
value is returned in the I(parsed) key within the result. The value of C(running_config) |
| 128 |
option should be the same format as the output of command I(show running-config |
| 129 |
| section ^bfd-template) executed on device. For state I(parsed) active |
| 130 |
connection to remote host is not required. |
| 131 |
- The state C(deleted) does not support or guarantee granular deletion of configuration |
| 132 |
the playbook should act as source of truth, and the desired state of the resouce is what |
| 133 |
the playbook should reflect. Use C(overridden) or C(replaced) to get extra configuration |
| 134 |
removed. |
| 135 |
- The state I(purged) removes BFD templates completely using a single top-level |
| 136 |
C(no bfd-template) command, which removes the entire template definition at once. |
| 137 |
type: str |
| 138 |
""" |
| 139 |
|
| 140 |
EXAMPLES = """ |
| 141 |
# Using merged |
| 142 |
# Before state: |
| 143 |
# ------------- |
| 144 |
# router-ios#show running-config | section ^bfd-template |
| 145 |
# (no BFD templates configured) |
| 146 |
|
| 147 |
- name: Merge provided configuration with device configuration |
| 148 |
cisco.ios.ios_bfd_templates: |
| 149 |
config: |
| 150 |
- name: template1 |
| 151 |
hop: single_hop |
| 152 |
interval: |
| 153 |
min_tx: 200 |
| 154 |
min_rx: 200 |
| 155 |
multiplier: 3 |
| 156 |
authentication: |
| 157 |
type: sha_1 |
| 158 |
keychain: bfd_keychain |
| 159 |
echo: true |
| 160 |
- name: template2 |
| 161 |
hop: multi_hop |
| 162 |
interval: |
| 163 |
min_tx: 500 |
| 164 |
min_rx: 500 |
| 165 |
multiplier: 5 |
| 166 |
dampening: |
| 167 |
half_life_period: 30 |
| 168 |
reuse_threshold: 2000 |
| 169 |
suppress_threshold: 5000 |
| 170 |
max_suppress_time: 120 |
| 171 |
state: merged |
| 172 |
|
| 173 |
# Commands Fired: |
| 174 |
# --------------- |
| 175 |
# bfd-template single-hop template1 |
| 176 |
# interval min-tx 200 min-rx 200 multiplier 3 |
| 177 |
# authentication sha-1 keychain bfd_keychain |
| 178 |
# echo |
| 179 |
# bfd-template multi-hop template2 |
| 180 |
# interval min-tx 500 min-rx 500 multiplier 5 |
| 181 |
# dampening 30 2000 5000 120 |
| 182 |
|
| 183 |
# After state: |
| 184 |
# ------------ |
| 185 |
# router-ios#show running-config | section ^bfd-template |
| 186 |
# bfd-template single-hop template1 |
| 187 |
# interval min-tx 200 min-rx 200 multiplier 3 |
| 188 |
# authentication sha-1 keychain bfd_keychain |
| 189 |
# echo |
| 190 |
# bfd-template multi-hop template2 |
| 191 |
# interval min-tx 500 min-rx 500 multiplier 5 |
| 192 |
# dampening 30 2000 5000 120 |
| 193 |
|
| 194 |
# Using replaced |
| 195 |
# Before state: |
| 196 |
# ------------- |
| 197 |
# router-ios#show running-config | section ^bfd-template |
| 198 |
# bfd-template single-hop template1 |
| 199 |
# interval min-tx 200 min-rx 200 multiplier 3 |
| 200 |
# authentication sha-1 keychain bfd_keychain |
| 201 |
# echo |
| 202 |
# bfd-template multi-hop template2 |
| 203 |
# interval min-tx 500 min-rx 500 multiplier 5 |
| 204 |
|
| 205 |
- name: Replace device configuration of specified BFD templates with provided configuration |
| 206 |
cisco.ios.ios_bfd_templates: |
| 207 |
config: |
| 208 |
- name: template1 |
| 209 |
hop: single_hop |
| 210 |
interval: |
| 211 |
min_tx: 300 |
| 212 |
min_rx: 300 |
| 213 |
multiplier: 4 |
| 214 |
authentication: |
| 215 |
type: sha_1 |
| 216 |
keychain: new_keychain |
| 217 |
state: replaced |
| 218 |
|
| 219 |
# Commands Fired: |
| 220 |
# --------------- |
| 221 |
# bfd-template single-hop template1 |
| 222 |
# no echo |
| 223 |
# interval min-tx 300 min-rx 300 multiplier 4 |
| 224 |
# no authentication sha-1 keychain bfd_keychain |
| 225 |
# authentication sha-1 keychain new_keychain |
| 226 |
|
| 227 |
# After state: |
| 228 |
# ------------ |
| 229 |
# router-ios#show running-config | section ^bfd-template |
| 230 |
# bfd-template single-hop template1 |
| 231 |
# interval min-tx 300 min-rx 300 multiplier 4 |
| 232 |
# authentication sha-1 keychain new_keychain |
| 233 |
# bfd-template multi-hop template2 |
| 234 |
# interval min-tx 500 min-rx 500 multiplier 5 |
| 235 |
|
| 236 |
# Using overridden |
| 237 |
# Before state: |
| 238 |
# ------------- |
| 239 |
# router-ios#show running-config | section ^bfd-template |
| 240 |
# bfd-template single-hop template1 |
| 241 |
# interval min-tx 200 min-rx 200 multiplier 3 |
| 242 |
# bfd-template multi-hop template2 |
| 243 |
# interval min-tx 500 min-rx 500 multiplier 5 |
| 244 |
# bfd-template single-hop template3 |
| 245 |
# echo |
| 246 |
|
| 247 |
- name: Override device configuration with provided configuration |
| 248 |
cisco.ios.ios_bfd_templates: |
| 249 |
config: |
| 250 |
- name: template1 |
| 251 |
hop: single_hop |
| 252 |
interval: |
| 253 |
min_tx: 300 |
| 254 |
min_rx: 300 |
| 255 |
multiplier: 5 |
| 256 |
authentication: |
| 257 |
type: md5 |
| 258 |
keychain: secure_key |
| 259 |
state: overridden |
| 260 |
|
| 261 |
# Commands Fired: |
| 262 |
# --------------- |
| 263 |
# no bfd-template multi-hop template2 |
| 264 |
# no bfd-template single-hop template3 |
| 265 |
# bfd-template single-hop template1 |
| 266 |
# interval min-tx 300 min-rx 300 multiplier 5 |
| 267 |
# authentication md5 keychain secure_key |
| 268 |
|
| 269 |
# After state: |
| 270 |
# ------------ |
| 271 |
# router-ios#show running-config | section ^bfd-template |
| 272 |
# bfd-template single-hop template1 |
| 273 |
# interval min-tx 300 min-rx 300 multiplier 5 |
| 274 |
# authentication md5 keychain secure_key |
| 275 |
|
| 276 |
# Using deleted |
| 277 |
# Before state: |
| 278 |
# ------------- |
| 279 |
# router-ios#show running-config | section ^bfd-template |
| 280 |
# bfd-template single-hop template1 |
| 281 |
# interval min-tx 200 min-rx 200 multiplier 3 |
| 282 |
# authentication sha-1 keychain bfd_keychain |
| 283 |
# echo |
| 284 |
# bfd-template multi-hop template2 |
| 285 |
# interval min-tx 500 min-rx 500 multiplier 5 |
| 286 |
|
| 287 |
- name: Delete specified BFD template |
| 288 |
cisco.ios.ios_bfd_templates: |
| 289 |
config: |
| 290 |
- name: template1 |
| 291 |
hop: single_hop |
| 292 |
state: deleted |
| 293 |
|
| 294 |
# Commands Fired: |
| 295 |
# --------------- |
| 296 |
# bfd-template single-hop template1 |
| 297 |
# no echo |
| 298 |
# no interval min-tx 200 min-rx 200 multiplier 3 |
| 299 |
# no authentication sha-1 keychain bfd_keychain |
| 300 |
|
| 301 |
# After state: |
| 302 |
# ------------ |
| 303 |
# router-ios#show running-config | section ^bfd-template |
| 304 |
# bfd-template single-hop template1 |
| 305 |
# bfd-template multi-hop template2 |
| 306 |
# interval min-tx 500 min-rx 500 multiplier 5 |
| 307 |
|
| 308 |
# Using deleted (to delete all BFD templates ) |
| 309 |
# Before state: |
| 310 |
# ------------- |
| 311 |
# router-ios#show running-config | section ^bfd-template |
| 312 |
# bfd-template single-hop template1 |
| 313 |
# interval min-tx 200 min-rx 200 multiplier 3 |
| 314 |
# bfd-template multi-hop template2 |
| 315 |
# interval min-tx 500 min-rx 500 multiplier 5 |
| 316 |
|
| 317 |
- name: Delete all BFD template configurations |
| 318 |
cisco.ios.ios_bfd_templates: |
| 319 |
state: deleted |
| 320 |
|
| 321 |
# Commands Fired: |
| 322 |
# --------------- |
| 323 |
# bfd-template single-hop template1 |
| 324 |
# no interval min-tx 200 min-rx 200 multiplier 3 |
| 325 |
# bfd-template multi-hop template2 |
| 326 |
# no interval min-tx 500 min-rx 500 multiplier 5 |
| 327 |
|
| 328 |
# After state: |
| 329 |
# ------------ |
| 330 |
# router-ios#show running-config | section ^bfd-template |
| 331 |
# bfd-template single-hop template1 |
| 332 |
# bfd-template multi-hop template2 |
| 333 |
|
| 334 |
# Using purged |
| 335 |
# Before state: |
| 336 |
# ------------- |
| 337 |
# router-ios#show running-config | section ^bfd-template |
| 338 |
# bfd-template single-hop template1 |
| 339 |
# interval min-tx 200 min-rx 200 multiplier 3 |
| 340 |
# bfd-template multi-hop template2 |
| 341 |
# interval min-tx 500 min-rx 500 multiplier 5 |
| 342 |
|
| 343 |
- name: Purge specified BFD template configurations (complete removal) |
| 344 |
cisco.ios.ios_bfd_templates: |
| 345 |
config: |
| 346 |
- name: template1 |
| 347 |
hop: single_hop |
| 348 |
state: purged |
| 349 |
|
| 350 |
# Commands Fired: |
| 351 |
# --------------- |
| 352 |
# no bfd-template single-hop template1 |
| 353 |
|
| 354 |
# After state: |
| 355 |
# ------------ |
| 356 |
# router-ios#show running-config | section ^bfd-template |
| 357 |
# bfd-template multi-hop template2 |
| 358 |
# interval min-tx 500 min-rx 500 multiplier 5 |
| 359 |
|
| 360 |
# Using purged (to remove all BFD templates) |
| 361 |
# Before state: |
| 362 |
# ------------- |
| 363 |
# router-ios#show running-config | section ^bfd-template |
| 364 |
# bfd-template single-hop template1 |
| 365 |
# interval min-tx 200 min-rx 200 multiplier 3 |
| 366 |
# authentication sha-1 keychain bfd_keychain |
| 367 |
# bfd-template multi-hop template2 |
| 368 |
# interval min-tx 500 min-rx 500 multiplier 5 |
| 369 |
# bfd-template single-hop template3 |
| 370 |
# echo |
| 371 |
|
| 372 |
- name: Purge all BFD template configurations (complete removal) |
| 373 |
cisco.ios.ios_bfd_templates: |
| 374 |
config: [] |
| 375 |
state: purged |
| 376 |
|
| 377 |
# Commands Fired: |
| 378 |
# --------------- |
| 379 |
# no bfd-template single-hop template1 |
| 380 |
# no bfd-template multi-hop template2 |
| 381 |
# no bfd-template single-hop template3 |
| 382 |
|
| 383 |
# After state: |
| 384 |
# ------------ |
| 385 |
# router-ios#show running-config | section ^bfd-template |
| 386 |
# (no BFD templates configured) |
| 387 |
|
| 388 |
# Using rendered |
| 389 |
- name: Render platform specific commands from task input using rendered state |
| 390 |
cisco.ios.ios_bfd_templates: |
| 391 |
config: |
| 392 |
- name: template1 |
| 393 |
hop: single_hop |
| 394 |
interval: |
| 395 |
min_tx: 200 |
| 396 |
min_rx: 200 |
| 397 |
multiplier: 3 |
| 398 |
authentication: |
| 399 |
type: meticulous_sha_1 |
| 400 |
keychain: secure_chain |
| 401 |
echo: true |
| 402 |
state: rendered |
| 403 |
|
| 404 |
# Module Execution Result: |
| 405 |
# ------------------------ |
| 406 |
# "rendered": [ |
| 407 |
# "bfd-template single-hop template1", |
| 408 |
# "interval min-tx 200 min-rx 200 multiplier 3", |
| 409 |
# "authentication meticulous-sha-1 keychain secure_chain", |
| 410 |
# "echo" |
| 411 |
# ] |
| 412 |
|
| 413 |
# Using gathered |
| 414 |
- name: Gather BFD template configuration from the device |
| 415 |
cisco.ios.ios_bfd_templates: |
| 416 |
state: gathered |
| 417 |
|
| 418 |
# Module Execution Result: |
| 419 |
# ------------------------ |
| 420 |
# "gathered": [ |
| 421 |
# { |
| 422 |
# "name": "template1", |
| 423 |
# "hop": "single_hop", |
| 424 |
# "interval": { |
| 425 |
# "min_tx": 200, |
| 426 |
# "min_rx": 200, |
| 427 |
# "multiplier": 3 |
| 428 |
# }, |
| 429 |
# "authentication": { |
| 430 |
# "type": "sha_1", |
| 431 |
# "keychain": "bfd_keychain" |
| 432 |
# }, |
| 433 |
# "echo": true |
| 434 |
# } |
| 435 |
# ] |
| 436 |
|
| 437 |
# Using parsed |
| 438 |
- name: Parse the provided configuration to structured format |
| 439 |
cisco.ios.ios_bfd_templates: |
| 440 |
running_config: | |
| 441 |
bfd-template single-hop template1 |
| 442 |
interval min-tx 200 min-rx 200 multiplier 3 |
| 443 |
authentication sha-1 keychain bfd_keychain |
| 444 |
echo |
| 445 |
bfd-template multi-hop template2 |
| 446 |
dampening 30 2000 5000 120 |
| 447 |
state: parsed |
| 448 |
|
| 449 |
# Module Execution Result: |
| 450 |
# ------------------------ |
| 451 |
# "parsed": [ |
| 452 |
# { |
| 453 |
# "name": "template1", |
| 454 |
# "hop": "single_hop", |
| 455 |
# "interval": { |
| 456 |
# "min_tx": 200, |
| 457 |
# "min_rx": 200, |
| 458 |
# "multiplier": 3 |
| 459 |
# }, |
| 460 |
# "authentication": { |
| 461 |
# "type": "sha_1", |
| 462 |
# "keychain": "bfd_keychain" |
| 463 |
# }, |
| 464 |
# "echo": true |
| 465 |
# }, |
| 466 |
# { |
| 467 |
# "name": "template2", |
| 468 |
# "hop": "multi_hop", |
| 469 |
# "dampening": { |
| 470 |
# "half_life_period": 30, |
| 471 |
# "reuse_threshold": 2000, |
| 472 |
# "suppress_threshold": 5000, |
| 473 |
# "max_suppress_time": 120 |
| 474 |
# } |
| 475 |
# } |
| 476 |
# ] |
| 477 |
""" |
| 478 |
|
| 479 |
RETURN = """ |
| 480 |
before: |
| 481 |
description: The configuration prior to the module invocation. |
| 482 |
returned: always |
| 483 |
type: list |
| 484 |
sample: > |
| 485 |
This output will always be in the same format as the |
| 486 |
module argspec. |
| 487 |
after: |
| 488 |
description: The resulting configuration after module execution. |
| 489 |
returned: when changed |
| 490 |
type: list |
| 491 |
sample: > |
| 492 |
This output will always be in the same format as the |
| 493 |
module argspec. |
| 494 |
commands: |
| 495 |
description: The set of commands pushed to the remote device. |
| 496 |
returned: always |
| 497 |
type: list |
| 498 |
sample: |
| 499 |
- bfd-template single-hop template1 |
| 500 |
- interval min-tx 200 min-rx 200 multiplier 3 |
| 501 |
- authentication sha-1 keychain bfd_keychain |
| 502 |
- echo |
| 503 |
rendered: |
| 504 |
description: The provided configuration in the task rendered in device native format (offline). |
| 505 |
returned: when I(state) is C(rendered) |
| 506 |
type: list |
| 507 |
sample: |
| 508 |
- bfd-template single-hop template1 |
| 509 |
- interval min-tx 200 min-rx 200 multiplier 3 |
| 510 |
- authentication sha-1 keychain bfd_keychain |
| 511 |
- echo |
| 512 |
gathered: |
| 513 |
description: Facts about the network resource gathered from the remote device as structured data. |
| 514 |
returned: when I(state) is C(gathered) |
| 515 |
type: list |
| 516 |
sample: > |
| 517 |
This output will always be in the same format as the |
| 518 |
module argspec. |
| 519 |
parsed: |
| 520 |
description: The device native config provided in I(running_config) option parsed into structured data as per module argspec. |
| 521 |
returned: when I(state) is C(parsed) |
| 522 |
type: list |
| 523 |
sample: > |
| 524 |
This output will always be in the same format as the |
| 525 |
module argspec. |
| 526 |
""" |
| 527 |
|
| 528 |
|
| 529 |
from ansible.module_utils.basic import AnsibleModule |
| 530 |
|
| 531 |
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.argspec.bfd_templates.bfd_templates import ( |
| 532 |
Bfd_templatesArgs, |
| 533 |
) |
| 534 |
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.config.bfd_templates.bfd_templates import ( |
| 535 |
Bfd_templates, |
| 536 |
) |
| 537 |
|
| 538 |
|
| 539 |
def main(): |
| 540 |
""" |
| 541 |
Main entry point for module execution |
| 542 |
|
| 543 |
:returns: the result form module invocation |
| 544 |
""" |
| 545 |
module = AnsibleModule( |
| 546 |
argument_spec=Bfd_templatesArgs.argument_spec, |
| 547 |
mutually_exclusive=[["config", "running_config"]], |
| 548 |
required_if=[ |
| 549 |
["state", "merged", ["config"]], |
| 550 |
["state", "replaced", ["config"]], |
| 551 |
["state", "overridden", ["config"]], |
| 552 |
["state", "rendered", ["config"]], |
| 553 |
["state", "parsed", ["running_config"]], |
| 554 |
], |
| 555 |
supports_check_mode=True, |
| 556 |
) |
| 557 |
|
| 558 |
result = Bfd_templates(module).execute_module() |
| 559 |
module.exit_json(**result) |
| 560 |
|
| 561 |
|
| 562 |
if __name__ == "__main__": |
| 563 |
main() |