Jinja template: Network device configuration

how-to-create-a-jinja-template-network-configuration

Creating a Jinja template is a very efficient way to deal with repetitive configuration of network devices. It is generally good practice to create templates for your network configuration anyway as most of the time network deployment requires change of IP addressing etc and the rest of the configuration file mimics that of a previous deployment.

One way to achieve this is with the use of Jinja templates. Originally designed to be used with Python, Jinja templates are very handy when used in conjunction with ansible. You can refer to my post on how to setup ansible here.

Refer to a snippet from a cisco jinja temple file:

In the above exhibit, you can see that wherever variables were meant to change, we created item values in a jinja template. All values inside {{}} get replaced with the reference value. This way, you can modify and create a new valid configuration file for a new deployment of any network device within minutes.

Just like the configuration snippet above, if the whole configuration file of a device is modified using a jinja template with the correct variables referenced everywhere, you can imagine how simple configuration generation would become.
You can either use jinja templates with Python or Ansible. Even though I perform extensive scripting to automate network tasks using Perl and Python, when it comes to bulk config generation, I am a big fan of Ansible. I prefer to define playbooks for all types of network use cases and then run them when needed.

With ansible, you can define all variables in a yaml file and create a playbook to call this yaml file. The playbook than replaces the referenced jinja template with all variable values found in the yaml file and generates an output file that mimics the configuration your wanted. It’s really that simple!

To refer to my guide of using Ansible in conjunction with jinja templates, refer to my post here.

Got questions? Leave a comment! Let’s chat.

Rafay Rasool is a Network Specialist with over 8 years of experience designing, configuring and implementing core network solutions based predominantly but not limited to Juniper Routers, Switches and Firewalls along with other vendors such as Cisco, Huawei, Siemens, Aerohive, Ringmaster, Pulse etc for Internet Service Provider and Enterprise Networks.

Rafay is an avid supporter of network automation and likes to code and automate networking solutions.