Skip to content

NoifP/confplate

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ConfPlate is a configuration file generator. After doing some configurations over and over again I got bored and built this script. Boring tasks often lead to mistakes in configurations which you want to avoid I assume.

The script is requires Python3 and uses Jinja2 as template language.

Notes

  • Original author has archived their copy and moved onto bigger and better things.
  • This fork has been updated as follows
    • now works with python3 (and requires it)
    • --force mode works with csv input
    • added ability to filter csv data by two fields
    • ability to use regex in filters
    • jinja2 ip address filters (j2ipaddr)
    • with much less elegant code (use at your own peril)!

Design goals

  • Code should run on Linux, OSX, Windows
  • Simplicity (Just generate configs from a template. No querying of other devices via SNMP for input, yadayada...)
  • Userfriendly command line interface
  • Make it easy to create UIs on top of the main code base

Setup (in a venv)

git clone https://github.com/NoifP/confplate.git
cd confplate/confplate
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install -r ../requirements.txt

Usage Examples

Example template cat3560.txt:

hostname {{HOSTNAME|lower}}
!
ip domain name {{DOMAINNAME}}
!
interface range FastEthernet0/1 - 24
 swtichport mode access
 switchport access vlan {{ACCESS_VLAN}}
 switchport nonegotiate
 no shut
!
interface {{MGMT_IFACE}}
 ip address {{MGMT_IFACE_IP}} {{MGMT_IFACE_NM}}
!
ip default-gateway {{MGMT_DEFAULT_GW}}

This template makes use of Jinja2 filters in the HOSTNAME variable. The value for the hostname variable will always be converted to lower case.

Get a list of variables used in a template

$ ./confplate.py -l examples/cat3560.txt 
 
Variables used in template cat3560.txt

	ACCESS_VLAN
	DOMAINNAME
	HOSTNAME
	MGMT_DEFAULT_GW
	MGMT_IFACE
	MGMT_IFACE_IP
	MGMT_IFACE_NM

Interactive mode

Will prompt you for every variable used in the template

$ ./confplate.py examples/cat3560.txt
ACCESS_VLAN: 23
DOMAINNAME: lab.verbosemo.de
HOSTNAME: brewery-sw010
MGMT_IFACE: Vlan200
MGMT_IFACE_IP: 10.33.200.10
MGMT_IFACE_NM: 255.255.255.0
MGMT_DEFAULT_GW: 10.33.200.1

Variables as arguments

Set all variable values as arguments on the command line

./confplate.py examples/cat3560.txt ACCESS_VLAN=23 DOMAINNAME=lab.verbosemo.de HOSTNAME=brewery-sw010 MGMT_IFACE=Vlan200 MGMT_IFACE_IP=10.33.200.10 MGMT_IFACE_NM=255.255.255.0 MGMT_DEFAULT_GW=10.33.200.1

Bulk configuration

  • Variable values read from CSV file
  • Creates config per line in CSV file

new-subnets.csv

VLAN_ID,VLAN_NAME,IP,NM,HSRP_IP,IP_HELPER
40,Floor1,192.168.40.2,255.255.255.0,192.168.40.1,192.168.100.100
50,Floor2,192.168.50.2,255.255.255.0,192.168.50.1,192.168.100.100
60,Floor3,192.168.60.2,255.255.255.0,192.168.60.1,192.168.100.100

new-subnets.txt

vlan {{VLAN_ID}}
 name {{VLAN_NAME}}
!
interface Vlan{{VLAN_ID}}
 ip address {{IP}} {{NM}}
 ip ospf 1 area 0.0.0.0
 ip ospf passive-interface
 ip helper-address {{IP_HELPER}}

Use new-subnets.csv as input and create a config per line

$ ./confplate.py -i examples/new-subnets.csv examples/new-subnets.txt 
vlan 40
 name Floor1
!
interface Vlan40
 ip address 192.168.40.2 255.255.255.0
 ip ospf 1 area 0.0.0.0
 ip ospf passive-interface
 ip helper-address 192.168.100.100

vlan 50
 name Floor2
!
interface Vlan50
 ip address 192.168.50.2 255.255.255.0
 ip ospf 1 area 0.0.0.0
 ip ospf passive-interface
 ip helper-address 192.168.100.100

vlan 60
 name Floor3
!
interface Vlan60
 ip address 192.168.60.2 255.255.255.0
 ip ospf 1 area 0.0.0.0
 ip ospf passive-interface
 ip helper-address 192.168.100.100

Generate a CSV header from an existing template. This should make generating a CSV header in your favourite spreadsheet application manually unnecessary

$ ./confplate.py --generate-csv-header examples/cat3560.txt
ACCESS_VLAN,DOMAINNAME,HOSTNAME,MGMT_DEFAULT_GW,MGMT_IFACE,MGMT_IFACE_IP,MGMT_IFACE_NM

$ ./confplate.py --generate-csv-header examples/cat3560.txt > /tmp/cat3560.csv

$ ./confplate.py -g -F ";" examples/cat3560.txt 
ACCESS_VLAN;DOMAINNAME;HOSTNAME;MGMT_DEFAULT_GW;MGMT_IFACE;MGMT_IFACE_IP;MGMT_IFACE_NM

CSV Filters

Filter CSV data based on a value in one of the fields. Field must match specified value. Example filters csv to only render output for Floor2.

$ ./confplate.py -i examples/new-subnets.csv --ff VLAN_NAME --fv Floor2 examples/new-subnets.txt 
vlan 50
 name Floor2
!
interface Vlan50
 ip address 192.168.50.2 255.255.255.0
 ip ospf 1 area 0.0.0.0
 ip ospf passive-interface
 ip helper-address 192.168.100.100

IP Address Filters

Convert network masks, CIDRs and other fun stuff thanks to j2ipaddr

$ ./confplate-ipfilters.py ../examples/ip-filters.j2
ipaddress: 10.11.12.13/24
ipaddress should be a CIDR IP address or an IP address with a subnet mask (and no spaces).
e.g. 192.168.1.5/24 or 192.168.1.5 255.255.255.0

IP Address: 10.11.12.13
Network Address: 10.11.12.0
Broadcast Address: 10.11.12.255
Network Mask: 255.255.255.0
Prefix Length: 24
Wildcard Mask: 0.0.0.255
Network Size: 256
First Usable IP Address: 10.11.12.1
Last Usable IP Address: 10.11.12.254

Feedback, bug reports and patches are welcome.

Links

About

Config file generator based on Jinja2 templates

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%