06-14-2024 12:35 AM
Hi
I have a number of config templates under `templates/` directory.
I would like to separate these templates into different sub-directories, such as `templates/so-xxx/yyy.xml`.
But I don't know how to refer to these templates from python.
I understand the function `template.apply()` takes template name (not template path) as the 1st argument.
This function don't recognize sub-directory.
Is it possible to use `template.apply()` when I put template files in sub-directory under `template/`?
And if it's possible, how to write python code?
I use NSO 5.4.4.1.
Thanks,
Atsushi
Solved! Go to Solution.
06-26-2024 04:16 AM
Hi Atsushi @anemo-608 ,
In the projects I've being involved, as part of the design we determine a template name convention to facilitate the navigation, in general we use <vendor>-<feature>-free_string.xml, but since you already have a service developed, change it will cost you at least time to promote the change, run QA.... it's completely understandable you trying to avoid those changes.
I played around @james-henderson idea, and created a variance. You can extend the package Makefile to navigate the template tree and create symbolic links to the XML file.
In this way you don't need to change the templates name, but the drawback is you need to make sure you will not reuse file names across the tree.
The full Makefile I modified is in attach, in case you like to take a look. There I implemented also the symbolic links clean up.
BR,
Kratz
06-18-2024 01:21 PM - edited 06-18-2024 01:26 PM
I do not think the feature you are looking for exists currently, but you could easily do this synthetically by creating a Make target for templates and copying the ones which are in directories into a flat structure. For example:
~/temp 16:18:39
$ mkdir -p templates-src/bogo
~/temp 16:18:50
$ touch templates-src/bogo/some-template.xml
~/temp 16:19:02
$ make templates
rm -rf templates
mkdir templates
# iterate through each of the templates in templates-src, copying each one to a flat structure with the path separator replaced by an dash
# So template templates-src/bogo/some-template.xml would become templates/bogo-some-template.xml
~/temp 16:19:04
$ ls templates
bogo-some-template.xml
~/temp 16:19:09
$ cat Makefile
templates: $(wildcard templates-src/**/*)
rm -rf templates
mkdir templates
# iterate through each of the templates in templates-src, copying each one to a flat structure with the path separator replaced by an underscore
# So template templates-src/bogo/some_template.xml would become templates/bogo-some-template.xml
@for template in $(wildcard templates-src/**/*); do \
if [ -f $$template ]; then \
newname=`echo $$template | sed 's|templates-src/||; s|/|-|g'`; \
cp $$template templates/$$newname; \
fi \
done
~/temp 16:19:21
$
06-21-2024 06:52 AM
Thank you for sharing makefile!
It can be a solution of my problem.
But, ideally, I'd like to use the template file without any changes to the template file name itself.
and just put the files into respective sub-directories.
There might be no way to realize that, indeed.
I will leave this discussion open a little longer.
Thanks
06-22-2024 06:50 AM
Hello,
Based on doc , it doesn't seems to be possible without some modification on your side.
The file structure of a package usually contains a templates folder and that is where the template belongs. When loading packages, NSO will scan this folder and process any .xml files it finds as templates.
You can also use symlink that points your files.
If you need this features , you need to request it
06-25-2024 05:47 PM
Hi,
Thank you for providing info!
Yes, unfortunately I couldn't find the way to split template into subdirectories in doc neither.
If someone was the same situation and solved it, I'd like to know the solution.
> If you need this features , you need to request it
Thank you for your advice. I'll consider it.
Thanks,
Atsushi
06-26-2024 04:16 AM
Hi Atsushi @anemo-608 ,
In the projects I've being involved, as part of the design we determine a template name convention to facilitate the navigation, in general we use <vendor>-<feature>-free_string.xml, but since you already have a service developed, change it will cost you at least time to promote the change, run QA.... it's completely understandable you trying to avoid those changes.
I played around @james-henderson idea, and created a variance. You can extend the package Makefile to navigate the template tree and create symbolic links to the XML file.
In this way you don't need to change the templates name, but the drawback is you need to make sure you will not reuse file names across the tree.
The full Makefile I modified is in attach, in case you like to take a look. There I implemented also the symbolic links clean up.
BR,
Kratz
06-26-2024 07:47 AM
One other point I might add is that NSO templates are not namespaced to the package they are in. Which means that you really should prepend the package name to each template, and if you are doing any sort of synthetic template copying/symlinking you should ensure you do this so your templates do ot conflict with some other packages templates which have the same name.
For example if your package is "l3vpn", each template should start with "l3vpn-" after any processing. This is more important if your package is for distribution, but good practice in all cases.
When I originally wrote a copying script I was doing it to work around an issue with the inability to use variables for something like "GigabitEthernet", (a node name) where all interface types need the same (complicated) configuration. In order to avoid duplicating it, I used python to template the template, allowing you to have a simple key value list which would be iterated over to create several templates based on one template. Then the code would iterate and apply them all. Something like:
l3vpn-interface-GigabitEthernet.xml
l3vpn-interface-TenGigEthernet.xml
These could also be collapsed into a single template. Just sharing to give ideas about how templates do not need to be a static file, they can be constructed at build time to become more flexible.
07-01-2024 01:26 AM
Hi, @Daniel Kratz
Thank you for your consideration and understanding!
As you said, Naming convention should have been determined correctly...
The method you shared meets my needs:
- no need to change python code and template name
- template files are structured by sub directories
I'll try to do that.
Hi, @james-henderson
> One other point I might add is that NSO templates are not namespaced to the package they are in. Which means that you really should prepend the package name to each template, and if you are doing any sort of synthetic template copying/symlinking you should ensure you do this so your templates do ot conflict with some other packages templates which have the same name.
Thank you for your helpful advice!
I didn't know that.
Your idea (dynamically-built template) is also helpful for me.
I'll use it for future reference.
Thank you,
Atsushi
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