Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lldp: Handling attributes that are defined multiple times #9657

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bugfixes:
- lldp - fix crash caused by certain lldpctl output where an attribute is defined as branch and leaf
minor_changes:
- lldp - adds ``multivalues`` parameter to control behavior when lldpctl outputs an attribute multiple times
32 changes: 27 additions & 5 deletions plugins/modules/lldp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
support: none
diff_mode:
support: none
options: {}
options:
multivalues:
description: If lldpctl outputs an attribute multiple time represent all values as a list.
required: false
type: bool
default: false
author: "Andy Hill (@andyhky)"
notes:
- Requires C(lldpd) running and LLDP enabled on switches.
Expand Down Expand Up @@ -53,26 +58,43 @@ def gather_lldp(module):
if output:
output_dict = {}
current_dict = {}
lldp_entries = output.split("\n")
lldp_entries = output.strip().split("\n")

for entry in lldp_entries:
if entry.startswith('lldp'):
path, value = entry.strip().split("=", 1)
path = path.split(".")
path_components, final = path[:-1], path[-1]
else:
value = current_dict[final] + '\n' + entry
current_dict[final] += '\n' + entry
continue

current_dict = output_dict
for path_component in path_components:
current_dict[path_component] = current_dict.get(path_component, {})
if type(current_dict[path_component]) is not dict:
current_dict[path_component] = {'value': current_dict[path_component]}
current_dict = current_dict[path_component]
current_dict[final] = value

if final in current_dict and type(current_dict[final]) is dict:
current_dict = current_dict[final]
final = 'value'

if final not in current_dict or not module.params['multivalues']:
current_dict[final] = value
elif type(current_dict[final]) is str:
current_dict[final] = [current_dict[final], value]
elif type(current_dict[final]) is list:
current_dict[final].append(value)

return output_dict


def main():
module = AnsibleModule({})
module_args = dict(
multivalues=dict(type='bool', equired=False, default=False)
)
module = AnsibleModule(module_args)

lldp_output = gather_lldp(module)
try:
Expand Down
Loading