| 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 |
from __future__ import absolute_import, division, print_function |
| 19 |
|
| 20 |
|
| 21 |
__metaclass__ = type |
| 22 |
|
| 23 |
|
| 24 |
DOCUMENTATION = """ |
| 25 |
module: nxos_bfd_global |
| 26 |
extends_documentation_fragment: |
| 27 |
- cisco.nxos.nxos |
| 28 |
short_description: Bidirectional Forwarding Detection (BFD) global-level configuration |
| 29 |
description: |
| 30 |
- Manages Bidirectional Forwarding Detection (BFD) global-level configuration. |
| 31 |
version_added: 1.0.0 |
| 32 |
author: |
| 33 |
- Chris Van Heuveln (@chrisvanheuveln) |
| 34 |
notes: |
| 35 |
- Tested against NXOSv 9.2(2) |
| 36 |
- Unsupported for Cisco MDS |
| 37 |
- BFD global will automatically enable 'feature bfd' if it is disabled. |
| 38 |
- BFD global does not have a 'state' parameter. All of the BFD commands are unique |
| 39 |
and are defined if 'feature bfd' is enabled. |
| 40 |
options: |
| 41 |
echo_interface: |
| 42 |
description: |
| 43 |
- Loopback interface used for echo frames. |
| 44 |
- Valid values are loopback interface name or 'deleted'. |
| 45 |
- Not supported on N5K/N6K |
| 46 |
required: false |
| 47 |
type: str |
| 48 |
echo_rx_interval: |
| 49 |
description: |
| 50 |
- BFD Echo receive interval in milliseconds. |
| 51 |
required: false |
| 52 |
type: int |
| 53 |
interval: |
| 54 |
description: |
| 55 |
- BFD interval timer values. |
| 56 |
- Value must be a dict defining values for keys (tx, min_rx, and multiplier) |
| 57 |
required: false |
| 58 |
type: dict |
| 59 |
slow_timer: |
| 60 |
description: |
| 61 |
- BFD slow rate timer in milliseconds. |
| 62 |
required: false |
| 63 |
type: int |
| 64 |
startup_timer: |
| 65 |
description: |
| 66 |
- BFD delayed startup timer in seconds. |
| 67 |
- Not supported on N5K/N6K/N7K |
| 68 |
required: false |
| 69 |
type: int |
| 70 |
ipv4_echo_rx_interval: |
| 71 |
description: |
| 72 |
- BFD IPv4 session echo receive interval in milliseconds. |
| 73 |
required: false |
| 74 |
type: int |
| 75 |
ipv4_interval: |
| 76 |
description: |
| 77 |
- BFD IPv4 interval timer values. |
| 78 |
- Value must be a dict defining values for keys (tx, min_rx, and multiplier). |
| 79 |
required: false |
| 80 |
type: dict |
| 81 |
ipv4_slow_timer: |
| 82 |
description: |
| 83 |
- BFD IPv4 slow rate timer in milliseconds. |
| 84 |
required: false |
| 85 |
type: int |
| 86 |
ipv6_echo_rx_interval: |
| 87 |
description: |
| 88 |
- BFD IPv6 session echo receive interval in milliseconds. |
| 89 |
required: false |
| 90 |
type: int |
| 91 |
ipv6_interval: |
| 92 |
description: |
| 93 |
- BFD IPv6 interval timer values. |
| 94 |
- Value must be a dict defining values for keys (tx, min_rx, and multiplier). |
| 95 |
required: false |
| 96 |
type: dict |
| 97 |
ipv6_slow_timer: |
| 98 |
description: |
| 99 |
- BFD IPv6 slow rate timer in milliseconds. |
| 100 |
required: false |
| 101 |
type: int |
| 102 |
fabricpath_interval: |
| 103 |
description: |
| 104 |
- BFD fabricpath interval timer values. |
| 105 |
- Value must be a dict defining values for keys (tx, min_rx, and multiplier). |
| 106 |
required: false |
| 107 |
type: dict |
| 108 |
fabricpath_slow_timer: |
| 109 |
description: |
| 110 |
- BFD fabricpath slow rate timer in milliseconds. |
| 111 |
required: false |
| 112 |
type: int |
| 113 |
fabricpath_vlan: |
| 114 |
description: |
| 115 |
- BFD fabricpath control vlan. |
| 116 |
required: false |
| 117 |
type: int |
| 118 |
""" |
| 119 |
EXAMPLES = """ |
| 120 |
- cisco.nxos.nxos_bfd_global: |
| 121 |
echo_interface: Ethernet1/2 |
| 122 |
echo_rx_interval: 50 |
| 123 |
interval: |
| 124 |
tx: 50 |
| 125 |
min_rx: 50 |
| 126 |
multiplier: 4 |
| 127 |
""" |
| 128 |
|
| 129 |
RETURN = """ |
| 130 |
cmds: |
| 131 |
description: commands sent to the device |
| 132 |
returned: always |
| 133 |
type: list |
| 134 |
sample: ["bfd echo-interface loopback1", "bfd slow-timer 2000"] |
| 135 |
""" |
| 136 |
|
| 137 |
|
| 138 |
import re |
| 139 |
|
| 140 |
from ansible.module_utils.basic import AnsibleModule |
| 141 |
|
| 142 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import ( |
| 143 |
NxosCmdRef, |
| 144 |
load_config, |
| 145 |
) |
| 146 |
|
| 147 |
|
| 148 |
BFD_CMD_REF = """ |
| 149 |
# The cmd_ref is a yaml formatted list of module commands. |
| 150 |
# A leading underscore denotes a non-command variable; e.g. _template. |
| 151 |
# BFD does not have convenient json data so this cmd_ref uses raw cli configs. |
| 152 |
--- |
| 153 |
_template: # _template holds common settings for all commands |
| 154 |
# Enable feature bfd if disabled |
| 155 |
feature: bfd |
| 156 |
# Common get syntax for BFD commands |
| 157 |
get_command: show run bfd all | incl '^(no )*bfd' |
| 158 |
|
| 159 |
echo_interface: |
| 160 |
kind: str |
| 161 |
getval: (no )*bfd echo-interface *(\\S+)*$ |
| 162 |
setval: 'bfd echo-interface {0}' |
| 163 |
default: ~ |
| 164 |
|
| 165 |
echo_rx_interval: |
| 166 |
_exclude: ['N5K', 'N6K'] |
| 167 |
kind: int |
| 168 |
getval: bfd echo-rx-interval (\\d+)$ |
| 169 |
setval: bfd echo-rx-interval {0} |
| 170 |
default: 50 |
| 171 |
N3K: |
| 172 |
default: 250 |
| 173 |
|
| 174 |
interval: |
| 175 |
kind: dict |
| 176 |
getval: bfd interval (?P<tx>\\d+) min_rx (?P<min_rx>\\d+) multiplier (?P<multiplier>\\d+) |
| 177 |
setval: bfd interval {tx} min_rx {min_rx} multiplier {multiplier} |
| 178 |
default: &def_interval |
| 179 |
tx: 50 |
| 180 |
min_rx: 50 |
| 181 |
multiplier: 3 |
| 182 |
N3K: |
| 183 |
default: &n3k_def_interval |
| 184 |
tx: 250 |
| 185 |
min_rx: 250 |
| 186 |
multiplier: 3 |
| 187 |
|
| 188 |
slow_timer: |
| 189 |
kind: int |
| 190 |
getval: bfd slow-timer (\\d+)$ |
| 191 |
setval: bfd slow-timer {0} |
| 192 |
default: 2000 |
| 193 |
|
| 194 |
startup_timer: |
| 195 |
_exclude: ['N5K', 'N6K', 'N7K'] |
| 196 |
kind: int |
| 197 |
getval: bfd startup-timer (\\d+)$ |
| 198 |
setval: bfd startup-timer {0} |
| 199 |
default: 5 |
| 200 |
|
| 201 |
# IPv4/IPv6 specific commands |
| 202 |
ipv4_echo_rx_interval: |
| 203 |
_exclude: ['N5K', 'N6K'] |
| 204 |
kind: int |
| 205 |
getval: bfd ipv4 echo-rx-interval (\\d+)$ |
| 206 |
setval: bfd ipv4 echo-rx-interval {0} |
| 207 |
default: 50 |
| 208 |
N3K: |
| 209 |
default: 250 |
| 210 |
|
| 211 |
ipv4_interval: |
| 212 |
_exclude: ['N5K', 'N6K'] |
| 213 |
kind: dict |
| 214 |
getval: bfd ipv4 interval (?P<tx>\\d+) min_rx (?P<min_rx>\\d+) multiplier (?P<multiplier>\\d+) |
| 215 |
setval: bfd ipv4 interval {tx} min_rx {min_rx} multiplier {multiplier} |
| 216 |
default: *def_interval |
| 217 |
N3K: |
| 218 |
default: *n3k_def_interval |
| 219 |
|
| 220 |
ipv4_slow_timer: |
| 221 |
_exclude: ['N5K', 'N6K'] |
| 222 |
kind: int |
| 223 |
getval: bfd ipv4 slow-timer (\\d+)$ |
| 224 |
setval: bfd ipv4 slow-timer {0} |
| 225 |
default: 2000 |
| 226 |
|
| 227 |
ipv6_echo_rx_interval: |
| 228 |
_exclude: ['N35', 'N5K', 'N6K'] |
| 229 |
kind: int |
| 230 |
getval: bfd ipv6 echo-rx-interval (\\d+)$ |
| 231 |
setval: bfd ipv6 echo-rx-interval {0} |
| 232 |
default: 50 |
| 233 |
N3K: |
| 234 |
default: 250 |
| 235 |
|
| 236 |
ipv6_interval: |
| 237 |
_exclude: ['N35', 'N5K', 'N6K'] |
| 238 |
kind: dict |
| 239 |
getval: bfd ipv6 interval (?P<tx>\\d+) min_rx (?P<min_rx>\\d+) multiplier (?P<multiplier>\\d+) |
| 240 |
setval: bfd ipv6 interval {tx} min_rx {min_rx} multiplier {multiplier} |
| 241 |
default: *def_interval |
| 242 |
N3K: |
| 243 |
default: *n3k_def_interval |
| 244 |
|
| 245 |
ipv6_slow_timer: |
| 246 |
_exclude: ['N35', 'N5K', 'N6K'] |
| 247 |
kind: int |
| 248 |
getval: bfd ipv6 slow-timer (\\d+)$ |
| 249 |
setval: bfd ipv6 slow-timer {0} |
| 250 |
default: 2000 |
| 251 |
|
| 252 |
# Fabricpath Commands |
| 253 |
fabricpath_interval: |
| 254 |
_exclude: ['N35', 'N3K', 'N9K'] |
| 255 |
kind: dict |
| 256 |
getval: bfd fabricpath interval (?P<tx>\\d+) min_rx (?P<min_rx>\\d+) multiplier (?P<multiplier>\\d+) |
| 257 |
setval: bfd fabricpath interval {tx} min_rx {min_rx} multiplier {multiplier} |
| 258 |
default: *def_interval |
| 259 |
|
| 260 |
fabricpath_slow_timer: |
| 261 |
_exclude: ['N35', 'N3K', 'N9K'] |
| 262 |
kind: int |
| 263 |
getval: bfd fabricpath slow-timer (\\d+)$ |
| 264 |
setval: bfd fabricpath slow-timer {0} |
| 265 |
default: 2000 |
| 266 |
|
| 267 |
fabricpath_vlan: |
| 268 |
_exclude: ['N35', 'N3K', 'N9K'] |
| 269 |
kind: int |
| 270 |
getval: bfd fabricpath vlan (\\d+)$ |
| 271 |
setval: bfd fabricpath vlan {0} |
| 272 |
default: 1 |
| 273 |
""" |
| 274 |
|
| 275 |
|
| 276 |
def reorder_cmds(cmds): |
| 277 |
""" |
| 278 |
There is a bug in some image versions where bfd echo-interface and |
| 279 |
bfd echo-rx-interval need to be applied last for them to nvgen properly. |
| 280 |
""" |
| 281 |
regex1 = re.compile(r"^bfd echo-interface") |
| 282 |
regex2 = re.compile(r"^bfd echo-rx-interval") |
| 283 |
filtered_cmds = [i for i in cmds if not regex1.match(i)] |
| 284 |
filtered_cmds = [i for i in filtered_cmds if not regex2.match(i)] |
| 285 |
echo_int_cmd = [i for i in cmds if regex1.match(i)] |
| 286 |
echo_rx_cmd = [i for i in cmds if regex2.match(i)] |
| 287 |
filtered_cmds.extend(echo_int_cmd) |
| 288 |
filtered_cmds.extend(echo_rx_cmd) |
| 289 |
|
| 290 |
return filtered_cmds |
| 291 |
|
| 292 |
|
| 293 |
def main(): |
| 294 |
argument_spec = dict( |
| 295 |
echo_interface=dict(required=False, type="str"), |
| 296 |
echo_rx_interval=dict(required=False, type="int"), |
| 297 |
interval=dict(required=False, type="dict"), |
| 298 |
slow_timer=dict(required=False, type="int"), |
| 299 |
startup_timer=dict(required=False, type="int"), |
| 300 |
ipv4_echo_rx_interval=dict(required=False, type="int"), |
| 301 |
ipv4_interval=dict(required=False, type="dict"), |
| 302 |
ipv4_slow_timer=dict(required=False, type="int"), |
| 303 |
ipv6_echo_rx_interval=dict(required=False, type="int"), |
| 304 |
ipv6_interval=dict(required=False, type="dict"), |
| 305 |
ipv6_slow_timer=dict(required=False, type="int"), |
| 306 |
fabricpath_interval=dict(required=False, type="dict"), |
| 307 |
fabricpath_slow_timer=dict(required=False, type="int"), |
| 308 |
fabricpath_vlan=dict(required=False, type="int"), |
| 309 |
) |
| 310 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 311 |
warnings = list() |
| 312 |
|
| 313 |
cmd_ref = NxosCmdRef(module, BFD_CMD_REF) |
| 314 |
cmd_ref.get_existing() |
| 315 |
cmd_ref.get_playvals() |
| 316 |
cmds = reorder_cmds(cmd_ref.get_proposed()) |
| 317 |
|
| 318 |
result = { |
| 319 |
"changed": False, |
| 320 |
"commands": cmds, |
| 321 |
"warnings": warnings, |
| 322 |
"check_mode": module.check_mode, |
| 323 |
} |
| 324 |
if cmds: |
| 325 |
result["changed"] = True |
| 326 |
if not module.check_mode: |
| 327 |
load_config(module, cmds) |
| 328 |
|
| 329 |
module.exit_json(**result) |
| 330 |
|
| 331 |
|
| 332 |
if __name__ == "__main__": |
| 333 |
main() |