07-29-2021 08:02 AM
Hello,
i'm working on the PnP-Function with DNA-Center and Jinja Template. Works good so far.
Here is my Question:
I want to add multiple VLANs (the exact number of VLAN is not always the same) via a Jinja Template.
So i can't do:
vlan {{VLANID}}
name {{VLAN_NAME}}
multiple times because i don't know how many VLANs the user want's to add.
How can i do this?
Thanks in advance.
Kind regards.
07-29-2021 08:50 AM - edited 07-29-2021 09:19 AM
You will need something like this in your template:
{% for vlan in vlan_list_of_dicts %}
vlan {{ vlan['id'] }}
name {{ vlan['name'] }}
{% endfor %}
This assumes you are passing a list of dictionaries "vlan_list_of_dicts" with keys of "id" and "name" when you render your template.
Using the for loop you can iterate through the list of little vlan info dictionaries and generate the configuration you need.
If you have 5 vlans you will have 5 elements in your list and each element will be a dictionary.
something like
[
{'id': 10, 'name': 'User Vlan 10'},
{'id': 20, 'name': 'User Vlan 20'},
{'id': 30, 'name': 'Security Cameras Vlan 30'}
]
Here is a demo repo on DevNet Exchange that might help. Its about generating configs using Nornir but that is also done with Jinja2 so it might help.
https://developer.cisco.com/codeexchange/github/repo/cldeluna/nornir-config
07-29-2021 12:41 PM - edited 07-29-2021 12:56 PM
That's a good point. Thank you very much.
But in this case i also have to predefine the VLANs i want to create before i do the provisioning.
I want it in a way, where the user enters the needed VLANs in the GUI in DNA-Center, when he do the Provisioning.
Maybe something like that:
Variable "VLANIDs": 10,20,30
Variable "VLAN_NAMES": Vlan 10, Vlan 20, Security Cameras Vlan 30
Or like:
Variable "VLANs": 10, Vlan10; 20, Vlan 20; 30, Security Cameras Vlan 30
Is it possible to create the dictionary in your example based on the input above?
Any Idea?
Thank you!
07-29-2021 02:05 PM
Are the names of the vlan really variables? or will vlan 30 always have the same name? If vlan 30 is always "Security Cameras Vlan 30" for example, then I would just hardcode that; user doesn't really care. They could just pick the vlan numbers they want, as a multi-select, then the jinja template can build the rest of the logic with hardcoded vlan names for each vlan number.
07-29-2021 12:49 PM - edited 07-29-2021 12:51 PM
Another solution would be, if i could define a variable in the template where i can enter multiple lines.
So i can enter a command block like:
vlan 10 name Vlan10 ! vlan 20 name Vlan20 ! vlan 30 name Security Cameras Vlan 30
Is that possible or are getting my ideas to weird now? (:
Kind regards.
07-29-2021 02:47 PM
So you can define static values in the template using the set command.
{% set vlan_id = ["10", "20", "30"] %}
{% set vlan_name = ["Vlan10", "Vlan20", "Security Vlan 30"] %}
I've never tried the set command with a list of dictionaries but that should work too.
I'm afraid im still trying to get my hands on DNA center for testing and learning so I'm not going to be of any help to you on the translation from DNAC to your template.
Having said all of that I may not fully understand what you are trying to do because on the one hand you want user input values but on the other hand it seems like some of it is static.
07-29-2021 11:05 PM
Sorry, for my unclear explanation.
I don't want the vlan configuration static in the template.
The idea was, that the user put the whole vlan configuration in a variable via the GUI.
So in the template is something like:
{{VLAN_CONFIG}}
And in the GUI he gets prompted a textbox where he can put the vlan configuration commands.
So it is not necessary, that the user change the template if he needs more or less vlans the next time.
07-30-2021 09:03 AM
Yes that makes perfect sense. I tried to mock something up using the DevNet Always On DNAC Sandbox but it really limits what you can do although you might want to check out some of the examples there.
Looks like to do what you want you would need to set up an Input Form or have the user use the "set" command within the template the way I defined the vlan_cfg variable in the example below.
I would caution you on having the user enter the entire configuration. The more a user can enter the more you need to validate the input. If you just ask them for vlan number and name then you have control over the rest of the syntax. Also, you may wind up having to play around with whitespace so something to consider.
Python 3.8.2 (v3.8.2:7b3ab5921f, Feb 24 2020, 17:52:18) [Clang 6.0 (clang-600.0.57)] on darwin from jinja2 import Template t = Template("!Vlan Configuration\n{{vlan_cfg}}") vlan_cfg = """ vlan 10 name Vlan 10 vlan 20 name Vlan 20 vlan 30 name Security Cameras Vlan30 """ vlan_cfg '\n vlan 10\n name Vlan 10\n vlan 20\n name Vlan 20\n vlan 30\n name Security Cameras Vlan30\n ' t.render(vlan_cfg = vlan_cfg) '!Vlan Configuration\n\n vlan 10\n name Vlan 10\n vlan 20\n name Vlan 20\n vlan 30\n name Security Cameras Vlan30\n '
Check out this section
Quickly Deploy Configuration Templates with Cisco DNA Center Platform and Template Programmer
In this DevNet learning lab:
https://developer.cisco.com/learning/tracks/dnacenter-programmability/dnac-rest-apis/intro-dnac-template-prg/complete
08-29-2021 01:52 PM
Hi,
I have done it this way:
{% set vlans = ['10;admin', '20;elev', '50;print', '99;mgmt'] %} {% for vlan in vlans %} {% set vlanInfo = vlan.split(';') %} vlan {{ vlanInfo[0] }} name {{ vlanInfo[1] }} {% endfor %}
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