cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
483
Views
5
Helpful
7
Replies

Is it possible to place template files into separate sub-directories?

anemo-608
Level 1
Level 1

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

1 Accepted Solution

Accepted Solutions

Daniel Kratz
Cisco Employee
Cisco Employee

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.

<..>
# symbolyc links directory
SYMLINK_DIR = ../templates
XML_FILES := $(shell find ../templates -type f -name "*.xml")
<..>
# Target to create symbolic links for XML files
create_links: $(SYMLINK_DIR) $(XML_FILES)
@for file in $(XML_FILES); do \
ln -s $$file $(SYMLINK_DIR)/$$(basename $$file); \
done
<..>

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



 

View solution in original post

7 Replies 7

james-henderson
Level 1
Level 1

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
$ 

 

 

anemo-608
Level 1
Level 1

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

Nabsch
Spotlight
Spotlight

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

anemo-608
Level 1
Level 1

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

Daniel Kratz
Cisco Employee
Cisco Employee

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.

<..>
# symbolyc links directory
SYMLINK_DIR = ../templates
XML_FILES := $(shell find ../templates -type f -name "*.xml")
<..>
# Target to create symbolic links for XML files
create_links: $(SYMLINK_DIR) $(XML_FILES)
@for file in $(XML_FILES); do \
ln -s $$file $(SYMLINK_DIR)/$$(basename $$file); \
done
<..>

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



 

james-henderson
Level 1
Level 1

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.

anemo-608
Level 1
Level 1

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