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

vlan parser for dellos6 #58

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
59 changes: 59 additions & 0 deletions plugins/filter/vlan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from ansible.errors import AnsibleError, AnsibleFilterError

def vlan_parser(vlan_list):
"""
Input: Unsorted list of vlan integers
Output: Sorted string list of integers according to OS6-like vlan list rules
1. Vlans are listed in ascending order
2. Runs of 2 or more consecutive vlans are listed with a dash
"""

# Sort and remove duplicates
sorted_list = sorted(set(vlan_list))

if sorted_list[0] < 1 or sorted_list[-1] > 4094:
raise AnsibleFilterError("Valid VLAN range is 1-4094")

parse_list = []
idx = 0
while idx < len(sorted_list):
start = idx
end = start
while end < len(sorted_list) - 1:
if sorted_list[end + 1] - sorted_list[end] == 1:
end += 1
else:
break

if start == end:
# Single VLAN
parse_list.append(str(sorted_list[idx]))
else:
# Run of 2 or more VLANs VLANs
parse_list.append(
str(sorted_list[start]) + "-" + str(sorted_list[end])
)
idx = end + 1

result = [""]
for vlans in parse_list:
# Line (" switchport trunk allowed vlan ")
result.append("")
result[0] += vlans + ","

# Remove trailing orphan commas
for idx in range(0, len(result)):
result[idx] = result[idx].rstrip(",")

# Sometimes text wraps to next line, but there are no remaining VLANs
if "" in result:
result.remove("")

return result


class FilterModule(object):
"""Filters for working with output from network devices"""

def filters(self):
return { 'vlan_parser': vlan_parser }