12-07-2022 02:43 AM
Hi,
I've been trying to do a split string in Jinja2 template editor in DNA Center.
In velocity it works just fine but I just can't get it to work with Jinja2.
Velocity
#set ($octets =$SwitchIP.split('\.'))
#set ($SiteSubnet = $octets[2])
subnet ${SiteSubnet}
Jinja2
{% set octets = '{{ switchip }}'.split(".") %}
{% set SiteSubnet = octets[2] %}
{{ SiteSubnet }}
I've tried multiple options recommened on various forums with no luck.
I found someone with a similar issue here.
jinja2 - Configuring a Jinja template in Cisco DNAC Template editor - Stack Overflow
12-08-2022 01:20 AM
I am not sure this is supported in DNAC. Tried it the other day with IP addresses instead.
{% set hname = 'ABC-DEF-111-AS-10-11-12-13' %}
Device name is {{ hname }}
{% set list1 = hname.split('-') %}
{% for list in list1 %}
<p>{{ list }}</p>
{% endfor %}
This should be the correct way to split it, but I just get a java error instead.
12-08-2022 02:04 AM
Well using this logic with a loop does produces some output but not quite what i had in mind.
{% set ip = '192-168-1-1' %}
{% set list1 = ip.split('-') %}
{% for list in list1 %}
{{ list }}
{% endfor %}
192
168
1
1
Substituting a period as you'd have in an IP address, DNA does not seem to like. It just fails to recognise the period entirely.
{% set ip = '192.168.1.1' %}
{% set list1 = ip.split(' . ') %}
{% for list in list1 %}
{{ list }}
{% endfor %}
There doesn't appear to be a method to access a specfic element in the list either as you can with the Velocity templates. Which was i really would like to do. Grab one of the specific octets to use elsewhere.
12-08-2022 02:12 AM
A little progress
This gives me what i wanted however I just can't get it to recognise a period .
{% set ip = '192-168-99-1' %}
{% set list1 = ip.split('-') %}
{% set SiteSubnet = list1[2] %}
{{ SiteSubnet }}
99
{% set ip = '192.168.99.1' %}
{% set list1 = ip.split('.') %}
{% set SiteSubnet = list1[2] %}
{{ SiteSubnet }}
12-08-2022 02:26 AM
Success! Needed the \\ escape sequence.
{% set ip = '192.168.100.1' %}
{% set list1 = ip.split('\\.') %}
{% set SiteSubnet = list1[2] %}
{{ SiteSubnet }}
12-08-2022 03:17 AM
Ahh, perfect. Thank you...
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide