The best place for network developers

Jinja2 – Configuration Template

Configuration Template with Jinja2 - how to generate configuration with Pyhon - Part I

For all of us entering same commands / text into CLI becomes boring after some time. Do you remember your fascination when you created a VLAN 10, connected two switches together and they pinged each other? Yes, probably for most of us were very enthusiastic that time. But after some time copying same command, creating same VLANs can become a little bit boring. What can we do to make it faster, better and less prone to mistakes?

The answer is quite simple – the answer is – use some programmatic point of concept. You can use Python and Jinja2 template base to generate configuration to create automatic configuration templates that  can be just copied and pasted to your network device.

This is one of the key benefits of using network automation – consistency – this is an ability to make changes in our production network environment, changes which are predictably and are always the same on every single device.

First of all. What is Jinja2?

Jinja2 is one of the most used template engines for Python. It is inspired by Django’s templating system but extends it with an expressive language that gives template authors a more powerful set of tools.

http://jinja.pocoo.org/

Let’s begin with basic example of template to configure a switch interface:

interface FastEthernet0/1
 description Printer
 switchport access vlan 10
 switchport mode access
 

The point that we can do is to convert the configuration into a template that can be applied into Cisco Switch. So, what are variables in this configuration:

  • Interface number
  • Description
  • Vlan number

So let we write our Interface configuration template as jinja2 template:

  1. interface FastEthernet 0/{{ interface.number }}
  2. description {{ interface.description }}
  3. switchport access vlan {{ interface.vlan }}
  4. switchport mode access
  5. no sh

Now we have a possibility to set different description / vlan on each interface.

Syntax interface.<name> means that we have dictionary and in the dictionary named “interface” we define keys (number / description / vlan) and for all keys we define some value (e.g. vlan = 10)

As always, there is multiple ways to achieve same result, so instead of creating dictionary we can always create a class interface and in the class define values for each method in the class.

Let we go back to our Jinja2 we have ready to go template for one interface but in our Cisco switch we have 24 access ports, so we have to create a loop for these interfaces as a result a template will be generated for all defined interfaces in our python script.

  1. {% for interface in interfaces %}
  2. interface FastEthernet 0/{{ interface.number }}
  3. description {{ interface.description }}
  4. switchport access vlan {{ interface.vlan }}
  5. switchport mode access
  6. no sh
  7. !
  8. {% endfor %}

When we have our template ready we can go back to Python script, firstly we have to install Jinja2 library:

Pip install jinja2
  1. from jinja2 import Environment, FileSystemLoader

At the beginning of a script, from jinja2 library we import:

  • Environment – this create environment for our template,
  • FileSystemLoader – allows us to use external file(s) with template(s)
  1. env = Environment(loader=FileSystemLoader('.'))

Specifying a dot (.) we declare that out template exists in the same directory as our Python script. If you need to specify any other path, you can define it here instead of dot.

  1. template = env.get_template('interface_template.j2')

Variable template is a template which indicates our environment (env) to collect a template named interface_template.j2 .

  1. interfaces = []
  2. for i in range(1, 12):
  3. interface = {'number': str(i),
  4. 'description': 'Users',
  5. 'vlan': '10'}
  6. interfaces.append(interface)
  7.  
  8. for i in range(12, 17):
  9. interface = {'number': str(i),
  10. 'description': 'Printers',
  11. 'vlan': '20'}
  12. interfaces.append(interface)
  13.  
  14. for i in range(17, 25):
  15. interface = {'number': str(i),
  16. 'description': 'Servers',
  17. 'vlan': '50'}
  18. interfaces.append(interface)
  19.  
  20. output = template.render(interfaces=interfaces)
  21. #Print the output
  22. print(output)
  23.  

Next, we can go to main code of our script. In Line 1 we define a list interfaces. This list is provided as a variable to  our template in line 20.

We create 3 loops in lines 2, 8, 14, loops are responsible for creating number, description and vlan for each port. Ever loop creates a dictionary which name is interface and in each dictionary we have keys: number, description and vlan, and values for each keys.

Please remember to always use the same name for keys that you used names in jinja2 template!

Please remember that by default python “for i in range (1, 12)” define variable i in range 1 – 11, the last value (12) is not in the range!

As a last step in line 20 we create a variable and in line 22 we print this output. If you want, you also can render the output to any text file or provide the text from output to directly on your device with Netmiko library.

When we run the script we can see out output:

interface FastEthernet 0/1
 description Users
 switchport access vlan 10
 switchport mode access
 no sh
!
 
interface FastEthernet 0/2
 description Users
 switchport access vlan 10
 switchport mode access
 no sh
!
 
interface FastEthernet 0/3
 description Users
 switchport access vlan 10
 switchport mode access
 no sh
!
 
interface FastEthernet 0/4
 description Users
 switchport access vlan 10
 switchport mode access
 no sh
!
 
interface FastEthernet 0/5
 description Users
 switchport access vlan 10
 switchport mode access
 no sh
!
 
interface FastEthernet 0/6
 description Users
 switchport access vlan 10
 switchport mode access
 no sh
!
 
interface FastEthernet 0/7
 description Users
 switchport access vlan 10
 switchport mode access
 no sh
!
 
interface FastEthernet 0/8
 description Users
 switchport access vlan 10
 switchport mode access
 no sh
!
 
interface FastEthernet 0/9
 description Users
 switchport access vlan 10
 switchport mode access
 no sh
!
 
interface FastEthernet 0/10
 description Users
 switchport access vlan 10
 switchport mode access
 no sh
!
 
interface FastEthernet 0/11
 description Users
 switchport access vlan 10
 switchport mode access
 no sh
!
 
interface FastEthernet 0/12
 description Printers
 switchport access vlan 20
 switchport mode access
 no sh
!
 
interface FastEthernet 0/13
 description Printers
 switchport access vlan 20
 switchport mode access
 no sh
!
 
interface FastEthernet 0/14
 description Printers
 switchport access vlan 20
 switchport mode access
 no sh
!
 
interface FastEthernet 0/15
 description Printers
 switchport access vlan 20
 switchport mode access
 no sh
!
 
interface FastEthernet 0/16
 description Printers
 switchport access vlan 20
 switchport mode access
 no sh
!
 
interface FastEthernet 0/17
 description Servers
 switchport access vlan 50
 switchport mode access
 no sh
!
 
interface FastEthernet 0/18
 description Servers
 switchport access vlan 50
 switchport mode access
 no sh
!
 
interface FastEthernet 0/19
 description Servers
 switchport access vlan 50
 switchport mode access
 no sh
!
 
interface FastEthernet 0/20
 description Servers
 switchport access vlan 50
 switchport mode access
 no sh
!
 
interface FastEthernet 0/21
 description Servers
 switchport access vlan 50
 switchport mode access
 no sh
!
 
interface FastEthernet 0/22
 description Servers
 switchport access vlan 50
 switchport mode access
 no sh
!
 
interface FastEthernet 0/23
 description Servers
 switchport access vlan 50
 switchport mode access
 no sh
!
 
interface FastEthernet 0/24
 description Servers
 switchport access vlan 50
 switchport mode access
 no sh
!

And as always, you can find full scripts and jinja template on our GitHub: https://github.com/DevNetSpace/Articles

Access Point – automatic report

Do you remember when last time local IT / your colleague / HelpDesk / anyone asked you for gathering a report about some devices which are connected in your network? Do you remember all the boring stuff that you had to do? Login into 5, 10, 15… 50 devices and gathering information like IP, port number, description, device name, mac address?

Building lab environment

Every time I have to automate specific kind of a job I write down and answer project specific questions, which help me to prepare algorithm and general idea, how a new solution will work.

Of course, this list can be easily extended during algorithm preparation. A good approach is to prepare small working solution and extend it, every time you need it, by adding new functionality. Building small peaces and extracting specific functions will help in troubleshooting, reduce downtime during writing a code and of course, give a better planning and estimation of time when whole project will be finished.

Automatic interface configuration

For all of us entering same commands / text into CLI becomes boring after some time. Do you remember your fascination when you created a VLAN 10, connected two switches together and they pinged each other? Yes, probably for most of us were very enthusiastic that time. But after some time copying same command, creating same VLANs can become a little bit boring. What can we do to make it faster, better and less prone to mistakes?