| 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_lldp_global |
| 20 |
""" |
| 21 |
from __future__ import absolute_import, division, print_function |
| 22 |
|
| 23 |
|
| 24 |
__metaclass__ = type |
| 25 |
|
| 26 |
DOCUMENTATION = """ |
| 27 |
module: ios_lldp_global |
| 28 |
short_description: Resource module to configure LLDP. |
| 29 |
description: |
| 30 |
This module configures and manages the Link Layer Discovery Protocol(LLDP) |
| 31 |
attributes on IOS platforms. |
| 32 |
version_added: 1.0.0 |
| 33 |
author: Sumit Jaiswal (@justjais) |
| 34 |
notes: |
| 35 |
- Tested against Cisco IOSXE Version 17.3 on CML. |
| 36 |
- This module works with connection C(network_cli). |
| 37 |
See U(https://docs.ansible.com/ansible/latest/network/user_guide/platform_ios.html) |
| 38 |
options: |
| 39 |
config: |
| 40 |
description: A dictionary of LLDP options |
| 41 |
type: dict |
| 42 |
suboptions: |
| 43 |
holdtime: |
| 44 |
description: |
| 45 |
- LLDP holdtime (in sec) to be sent in packets. |
| 46 |
- Refer to vendor documentation for valid values. |
| 47 |
type: int |
| 48 |
reinit: |
| 49 |
description: |
| 50 |
- Specify the delay (in secs) for LLDP to initialize. |
| 51 |
- Refer to vendor documentation for valid values. |
| 52 |
- NOTE, if LLDP reinit is configured with a starting value, idempotency won't |
| 53 |
be maintained as the Cisco device doesn't record the starting reinit configured |
| 54 |
value. As such, Ansible cannot verify if the respective starting reinit |
| 55 |
value is already configured or not from the device side. If you try to apply |
| 56 |
starting reinit value in every play run, Ansible will show changed as True. |
| 57 |
For any other reinit value, idempotency will be maintained since any other |
| 58 |
reinit value is recorded in the Cisco device. |
| 59 |
type: int |
| 60 |
enabled: |
| 61 |
description: |
| 62 |
- Enable LLDP |
| 63 |
type: bool |
| 64 |
timer: |
| 65 |
description: |
| 66 |
- Specify the rate at which LLDP packets are sent (in sec). |
| 67 |
- Refer to vendor documentation for valid values. |
| 68 |
type: int |
| 69 |
tlv_select: |
| 70 |
description: |
| 71 |
- Selection of LLDP TLVs i.e. type-length-value to send |
| 72 |
- NOTE, if tlv-select is configured idempotency won't be maintained as Cisco |
| 73 |
device doesn't record configured tlv-select options. As such, Ansible cannot |
| 74 |
verify if the respective tlv-select options is already configured or not |
| 75 |
from the device side. If you try to apply tlv-select option in every play |
| 76 |
run, Ansible will show changed as True. |
| 77 |
type: dict |
| 78 |
suboptions: |
| 79 |
four_wire_power_management: |
| 80 |
description: |
| 81 |
- Cisco 4-wire Power via MDI TLV |
| 82 |
type: bool |
| 83 |
mac_phy_cfg: |
| 84 |
description: |
| 85 |
- IEEE 802.3 MAC/Phy Configuration/status TLV |
| 86 |
type: bool |
| 87 |
management_address: |
| 88 |
description: |
| 89 |
- Management Address TLV |
| 90 |
type: bool |
| 91 |
port_description: |
| 92 |
description: |
| 93 |
- Port Description TLV |
| 94 |
type: bool |
| 95 |
port_vlan: |
| 96 |
description: |
| 97 |
- Port VLAN ID TLV |
| 98 |
type: bool |
| 99 |
power_management: |
| 100 |
description: |
| 101 |
- IEEE 802.3 DTE Power via MDI TLV |
| 102 |
type: bool |
| 103 |
system_capabilities: |
| 104 |
description: |
| 105 |
- System Capabilities TLV |
| 106 |
type: bool |
| 107 |
system_description: |
| 108 |
description: |
| 109 |
- System Description TLV |
| 110 |
type: bool |
| 111 |
system_name: |
| 112 |
description: |
| 113 |
- System Name TLV |
| 114 |
type: bool |
| 115 |
running_config: |
| 116 |
description: |
| 117 |
- This option is used only with state I(parsed). |
| 118 |
- The value of this option should be the output received from the IOS device |
| 119 |
by executing the command B(show running-config | section ^lldp). |
| 120 |
- The state I(parsed) reads the configuration from C(running_config) option and |
| 121 |
transforms it into Ansible structured data as per the resource module's argspec |
| 122 |
and the value is then returned in the I(parsed) key within the result. |
| 123 |
type: str |
| 124 |
state: |
| 125 |
description: |
| 126 |
- The state the configuration should be left in |
| 127 |
- The module have declaratively similar behavior for replaced and overridden state. |
| 128 |
- The states I(rendered), I(gathered) and I(parsed) does not perform any change |
| 129 |
on the device. |
| 130 |
- The state I(rendered) will transform the configuration in C(config) option to |
| 131 |
platform specific CLI commands which will be returned in the I(rendered) key |
| 132 |
within the result. For state I(rendered) active connection to remote host is |
| 133 |
not required. |
| 134 |
- The state I(gathered) will fetch the running configuration from device and transform |
| 135 |
it into structured data in the format as per the resource module argspec and |
| 136 |
the value is returned in the I(gathered) key within the result. |
| 137 |
- The state I(parsed) reads the configuration from C(running_config) option and |
| 138 |
transforms it into JSON format as per the resource module parameters and the |
| 139 |
value is returned in the I(parsed) key within the result. The value of C(running_config) |
| 140 |
option should be the same format as the output of command I(show running-config |
| 141 |
| include ip route|ipv6 route) executed on device. For state I(parsed) active |
| 142 |
connection to remote host is not required. |
| 143 |
type: str |
| 144 |
choices: |
| 145 |
- merged |
| 146 |
- replaced |
| 147 |
- overridden |
| 148 |
- deleted |
| 149 |
- rendered |
| 150 |
- gathered |
| 151 |
- parsed |
| 152 |
default: merged |
| 153 |
""" |
| 154 |
|
| 155 |
EXAMPLES = """ |
| 156 |
# Using merged |
| 157 |
|
| 158 |
# Before state: |
| 159 |
# ------------- |
| 160 |
# vios#sh running-config | section ^lldp |
| 161 |
# vios1# |
| 162 |
|
| 163 |
- name: Merge provided configuration with device configuration |
| 164 |
cisco.ios.ios_lldp_global: |
| 165 |
config: |
| 166 |
holdtime: 10 |
| 167 |
enabled: true |
| 168 |
reinit: 3 |
| 169 |
timer: 10 |
| 170 |
state: merged |
| 171 |
|
| 172 |
# After state: |
| 173 |
# ------------ |
| 174 |
# vios#sh running-config | section ^lldp |
| 175 |
# lldp timer 10 |
| 176 |
# lldp holdtime 10 |
| 177 |
# lldp reinit 3 |
| 178 |
# lldp run |
| 179 |
|
| 180 |
# Using replaced |
| 181 |
|
| 182 |
# Before state: |
| 183 |
# ------------- |
| 184 |
# vios#sh running-config | section ^lldp |
| 185 |
# lldp timer 10 |
| 186 |
# lldp holdtime 10 |
| 187 |
# lldp reinit 3 |
| 188 |
# lldp run |
| 189 |
|
| 190 |
- name: Replaces LLDP device configuration with provided configuration |
| 191 |
cisco.ios.ios_lldp_global: |
| 192 |
config: |
| 193 |
holdtime: 20 |
| 194 |
reinit: 5 |
| 195 |
state: replaced |
| 196 |
|
| 197 |
# After state: |
| 198 |
# ------------- |
| 199 |
# vios#sh running-config | section ^lldp |
| 200 |
# lldp holdtime 20 |
| 201 |
# lldp reinit 5 |
| 202 |
|
| 203 |
# Using Deleted without any config passed |
| 204 |
# "(NOTE: This will delete all of configured LLDP module attributes)" |
| 205 |
|
| 206 |
# Before state: |
| 207 |
# ------------- |
| 208 |
# vios#sh running-config | section ^lldp |
| 209 |
# lldp timer 10 |
| 210 |
# lldp holdtime 10 |
| 211 |
# lldp reinit 3 |
| 212 |
# lldp run |
| 213 |
|
| 214 |
- name: Delete LLDP attributes |
| 215 |
cisco.ios.ios_lldp_global: |
| 216 |
state: deleted |
| 217 |
|
| 218 |
# After state: |
| 219 |
# ------------- |
| 220 |
# vios#sh running-config | section ^lldp |
| 221 |
# vios1# |
| 222 |
|
| 223 |
# Using Gathered |
| 224 |
|
| 225 |
# Before state: |
| 226 |
# ------------- |
| 227 |
# |
| 228 |
# vios#sh running-config | section ^lldp |
| 229 |
# lldp timer 10 |
| 230 |
# lldp holdtime 10 |
| 231 |
# lldp reinit 3 |
| 232 |
# lldp run |
| 233 |
|
| 234 |
- name: Gather listed interfaces with provided configurations |
| 235 |
cisco.ios.ios_lldp_global: |
| 236 |
config: |
| 237 |
state: gathered |
| 238 |
|
| 239 |
# Module Execution Result: |
| 240 |
# ------------------------ |
| 241 |
# |
| 242 |
# "gathered": { |
| 243 |
# "enabled": true, |
| 244 |
# "holdtime": 10, |
| 245 |
# "reinit": 3, |
| 246 |
# "timer": 10 |
| 247 |
# } |
| 248 |
|
| 249 |
# After state: |
| 250 |
# ------------ |
| 251 |
# |
| 252 |
# vios#sh running-config | section ^lldp |
| 253 |
# lldp timer 10 |
| 254 |
# lldp holdtime 10 |
| 255 |
# lldp reinit 3 |
| 256 |
# lldp run |
| 257 |
|
| 258 |
# Using Rendered |
| 259 |
- name: Render the commands for provided configuration |
| 260 |
cisco.ios.ios_lldp_global: |
| 261 |
config: |
| 262 |
holdtime: 10 |
| 263 |
enabled: true |
| 264 |
reinit: 3 |
| 265 |
timer: 10 |
| 266 |
state: rendered |
| 267 |
|
| 268 |
# Module Execution Result: |
| 269 |
# ------------------------ |
| 270 |
# |
| 271 |
# "rendered": [ |
| 272 |
# "lldp holdtime 10", |
| 273 |
# "lldp run", |
| 274 |
# "lldp timer 10", |
| 275 |
# "lldp reinit 3" |
| 276 |
# ] |
| 277 |
|
| 278 |
# Using Parsed |
| 279 |
|
| 280 |
# File: parsed.cfg |
| 281 |
# ---------------- |
| 282 |
# |
| 283 |
# lldp timer 10 |
| 284 |
# lldp holdtime 10 |
| 285 |
# lldp reinit 3 |
| 286 |
# lldp run |
| 287 |
|
| 288 |
- name: Parse the commands for provided configuration |
| 289 |
cisco.ios.ios_lldp_global: |
| 290 |
running_config: "{{ lookup('file', 'parsed.cfg') }}" |
| 291 |
state: parsed |
| 292 |
|
| 293 |
# Module Execution Result: |
| 294 |
# ------------------------ |
| 295 |
# |
| 296 |
# "parsed": { |
| 297 |
# "enabled": true, |
| 298 |
# "holdtime": 10, |
| 299 |
# "reinit": 3, |
| 300 |
# "timer": 10 |
| 301 |
# } |
| 302 |
""" |
| 303 |
|
| 304 |
RETURN = """ |
| 305 |
before: |
| 306 |
description: The configuration as structured data prior to module invocation. |
| 307 |
returned: always |
| 308 |
type: dict |
| 309 |
sample: The configuration returned will always be in the same format of the parameters above. |
| 310 |
after: |
| 311 |
description: The configuration as structured data after module completion. |
| 312 |
returned: when changed |
| 313 |
type: dict |
| 314 |
sample: The configuration returned will always be in the same format of the parameters above. |
| 315 |
commands: |
| 316 |
description: The set of commands pushed to the remote device |
| 317 |
returned: always |
| 318 |
type: list |
| 319 |
sample: ['lldp holdtime 10', 'lldp run', 'lldp timer 10'] |
| 320 |
""" |
| 321 |
|
| 322 |
from ansible.module_utils.basic import AnsibleModule |
| 323 |
|
| 324 |
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.argspec.lldp_global.lldp_global import ( |
| 325 |
Lldp_globalArgs, |
| 326 |
) |
| 327 |
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.config.lldp_global.lldp_global import ( |
| 328 |
Lldp_global, |
| 329 |
) |
| 330 |
|
| 331 |
|
| 332 |
def main(): |
| 333 |
""" |
| 334 |
Main entry point for module execution |
| 335 |
|
| 336 |
:returns: the result form module invocation |
| 337 |
""" |
| 338 |
required_if = [ |
| 339 |
("state", "merged", ("config",)), |
| 340 |
("state", "replaced", ("config",)), |
| 341 |
("state", "overridden", ("config",)), |
| 342 |
("state", "rendered", ("config",)), |
| 343 |
("state", "parsed", ("running_config",)), |
| 344 |
] |
| 345 |
mutually_exclusive = [("config", "running_config")] |
| 346 |
|
| 347 |
module = AnsibleModule( |
| 348 |
argument_spec=Lldp_globalArgs.argument_spec, |
| 349 |
required_if=required_if, |
| 350 |
mutually_exclusive=mutually_exclusive, |
| 351 |
supports_check_mode=True, |
| 352 |
) |
| 353 |
result = Lldp_global(module).execute_module() |
| 354 |
module.exit_json(**result) |
| 355 |
|
| 356 |
|
| 357 |
if __name__ == "__main__": |
| 358 |
main() |