Custom jinja filter for render config #13273
andershagman
started this conversation in
Ideas
Replies: 2 comments 4 replies
-
I've only used Jinja2 within Ansible so I don't know which behaviors are built-in and which are Ansible-specific, but AFAIK you can call python string methods on jinja strings[1] eg {{ foo.split('-')[0] }} and Ansible has a jinja filter for this specific case[2] to output IOS-style VLAN lists which you may be able to use as a reference if the licensing for ansible.netcommon allows it.
[1] https://jinja.palletsprojects.com/en/3.1.x/templates/#python-methods
[2] https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_filters.html#network-vlan-filters
—
Mark Tinberg ***@***.***>
Division of Information Technology-Network Services
University of Wisconsin-Madison
…________________________________
From: andershagman ***@***.***>
Sent: Wednesday, July 26, 2023 9:51 AM
To: netbox-community/netbox ***@***.***>
Cc: Subscribed ***@***.***>
Subject: [netbox-community/netbox] Custom jinja filter for render config (Discussion #13273)
Have started to use the render config system and it works as it should.
BUT, jinja is not always the best languish for the job.
It doesn´t handle global variables and lack normal string feature like split.
My problem was to convert a list of VLANs to Cisco style.
From this:
interface GigabitEthernet1/0/1
switchport trunk allowed vlan 1393,1394,1395,1396,1397,1398,1399,1409,3393,3394,3395,3396,3397,3398,3399,3409
To this:
interface GigabitEthernet1/0/1
switchport trunk allowed vlan 1393-1399, 1409, 3393-3399, 3409
I couldn´t find any solution for this in jinja, so I made a jinja filter in python and added it to the configuration.py file
def list_to_ranges(lst):
sorted_lst = sorted(lst)
ranges = []
start = sorted_lst[0]
end = sorted_lst[0]
for num in sorted_lst[1:]:
if num == end + 1:
end = num
else:
if start == end:
ranges.append(str(start))
else:
ranges.append(f"{start}-{end}")
start = num
end = num
if start == end:
ranges.append(str(start))
else:
ranges.append(f"{start}-{end}")
return ', '.join(ranges)
JINJA2_FILTERS = {
'list_to_ranges': list_to_ranges,
}
Is there a better way to add filters or should we have a more scaleable way to to this?
—
Reply to this email directly, view it on GitHub<#13273>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAS7UMZZPZ2J4UOAPUIPFA3XSEVFVANCNFSM6AAAAAA2YW2ZFQ>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
4 replies
-
So a solution like this could do?
def cf_filter1(s):
result = s
return result
def cf_filter2(s):
result = s
return result
def cf_filter3(s):
result = s
return result
def import_filters():
cf_filter = {}
func_list = globals()
for func in func_list:
if "cf_" in func:
cf_filter[func] = f'filters.{func}'
return cf_filter
if __name__ == "__main__":
print(import_filters()) And the from local import filters
JINJA2_FILTERS = filters.import_filters() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Have started to use the render config system and it works as it should.
BUT, jinja is not always the best languish for the job.
It doesn´t handle global variables and lack normal string feature like split.
My problem was to convert a list of VLANs to Cisco style.
From this:
To this:
I couldn´t find any solution for this in jinja, so I made a jinja filter in python and added it to the configuration.py file
Is there a better way to add filters or should we have a more scaleable way to do this?
Beta Was this translation helpful? Give feedback.
All reactions