cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
4274
Views
17
Helpful
5
Replies

DNA Jinja2 templating split string

glsparks
Level 1
Level 1

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

 

5 Replies 5

rasmus.elmholt
Level 7
Level 7

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.

 

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.

 

glsparks
Level 1
Level 1

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 }}

glsparks
Level 1
Level 1

Success!  Needed the \\ escape sequence.

{% set ip = '192.168.100.1' %}
{% set list1 = ip.split('\\.') %}
{% set SiteSubnet = list1[2] %}
{{ SiteSubnet }}

Ahh, perfect. Thank you...

Review Cisco Networking for a $25 gift card