Using Templates in Your Configuration Files

There is a little-known secret in Asterisk config files that is so brilliant that we had to devote a little section to it.

Let us say that you have 20 SIP phones that are all pretty much identical in terms of how they are configured. The documented way to create them is to specify the parameters for each. Part of such a sip.conf file might look like this:

[1000]
type=friend
context=internal
host=dynamic
disallow=all
allow=ulaw
dtmfmode=rfc2833
maibox=1000
secret=AllYourSetsAreBelongToUs

[1001]
type=friend
context=internal
host=dynamic
disallow=all
allow=ulaw
dtmfmode=rfc2833
maibox=1001
secret=AllYourSetsAreBelongToUs

[1002]
type=friend
context=internal
host=dynamic
disallow=all
allow=ulaw
dtmfmode=rfc2833
maibox=1002
secret=AllYourSetsAreBelongToUs

Seems like a lot of extra typing, cutting, and pasting, yes? And what if you decide that you are going to change the context for your sets to another name. Not looking good, is it?

Enter the template. Let’s create the same SIP friends as we did above, only this time using the template construct:

[sets](!) ; <== note the exclamation point in parenthesis. That makes this a template.
type=friend
context=internal
host=dynamic
disallow=all
allow=ulaw
dtmfmode=rfc2833
secret=AllYourSetsAreBelongToUs

[1000](sets) ; <== note the template name in parenthesis. All of that templates  
             ; settings will be inhereted.
maibox=1000

[1001](sets)
maibox=1001

[1002](sets)
maibox=1002

This is one of the best kept secrets of conf file creation. In our experience, very few people use this, but for no other reason than that they don’t know about it. Well, that’s about to change. Our goal is to see everyone using these from now on; and yes, we will be checking.