Configs Templates Take 2 (17 of 30)

This is the second part of a series of post’s on developing a Config Template tool, click this link to follow the whole series of post’s. This tutorial will go over two attributes of HTML::Template cpan module. The ability to “include” other config templates and the ability to make simple if statements.

Let’s say you have regional configs, in this example, it will be NTP servers, but it could easily be tacacs, radius, syslog, etc… You want to be able to have the user declare one option (the region,) and it correctly declare the regional configs.

In the html form, create a drop down or radio button

1
2
3
4
5
6
REGION:
<select name="region">
<option>AM</option>
<option>AP</option>
<option>EU</option>
</select><br>

In the cgi script, you have to take in the parameter, then convert it to a specfic parameter, and send that paremeter.. a little confusing and I am probably not explaining it the best, hopefully the code makes sense.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
my $REGION = param('REGION' );
my ($AM,$EU,$AP)= ():
if ($REGION eq 'AM');
$AM = 1;
}
elsif ($REGION eq 'AP' ){
$AP = 1;

}
elsif ($REGION eq 'EU' ){
$EU = 1;
}
$switch->param('AM' => $AM );
$switch->param('AP' => $AP );
$switch->param('EU' => $EU );

Now you can use HTML::Template’s simple IF option.

1
2
3
4
5
6
7
8
9
10
11
12
13
!<TMPL_IF NAME=AM>
ntp server 10.100.123.123
ntp server 10.100.123.124
ntp server 10.100.123.125</TMPL_IF>
!<TMPL_IF NAME=AP>
ntp server 10.200.123.123
ntp server 10.200.123.124
ntp server 10.200.123.125</TMPL_IF>
!<TMPL_IF NAME=EU>
ntp server 10.300.123.123
ntp server 10.300.123.124
ntp server 10.300.123.125</TMPL_IF>
!</TMPL_IF>

So this saves you from having to create templates like “switch_am.tmpl” and “switch_eu.tmpl” etc… Now you might have configure this same ntp on multiple devices, so the next feature is the include. This allows you to nest configurations. So put the above into a file called ntp.tmpl, and in switch.tmpl, you create an include to that ntp.tmpl, e.g:

1
2
3
ntp access-group peer 23
<TMPL_INCLUDE NAME="ntp.tmpl">
end

Nesting is allowed something like 10 times, otherwise you could find yourself in a nesting loop, so the creator of the module put some checks in against that.

Here is your end result:

take-2 pre

take-2 post

There you have it, two steps closer to something a that can be very useful.

You can see the configs on my github

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>