Using Django's Template system for network configs
I wrote hatch a few years ago to make cookie cutter network config templates. One thing I've wanted to implement for a while is a DSL for use in hatch. I've experimented with jscc and more recently with peg.js. While writing grammar for peg.js I said to myself "it would be great if I could used Django's template system and filters without re-inventing the wheel."
So I started experimenting and found out its pretty easy to do. A little more tinkering and I could possibly replace hatch's mechanics with Django's builtin template, tag, and filter code.
#!/usr/bin/python from django.conf import settings from django import template settings.configure(DEBUG=True, TEMPLATE_DEBUG=True) #https://github.com/django/django/blob/master/django/template/__init__.py s = u'conf t\n ip address {{ip_address}} {{subnet_mask}} \n end \n write' t = template.Template(s) c = template.Context({'ip_address': '192.168.1.1','subnet_mask':'255.255.255.0'}) rendered = t.render(c) print rendered print "----------------------------------" s = u'conf t\n ip address 10.{{site_code}}.1.1 {{subnet_mask}} \n end \n write' t = template.Template(s) c = template.Context({'site_code': '66','subnet_mask':'255.255.255.0'}) rendered = t.render(c) print rendered print "----------------------------------" s = u'conf t\n {% if site_code == 66 %}snmp-server location Sydney{% endif%}' t = template.Template(s) c = template.Context({'site_code': 66}) rendered = t.render(c) print rendered
akonkol@dev:~/Code/messy$ ./temp_hack.py conf t ip address 192.168.1.1 255.255.255.0 end write ---------------------------------- conf t ip address 10.66.1.1 255.255.255.0 end write ---------------------------------- conf t snmp-server location Sydney
0 Comments
Log in with Twitter, Google, Facebook, LinkedIn to leave a comment.