├── README.md ├── bgp_builder.py ├── bgp_template.j2 ├── hello_template.py ├── hello_world.j2 ├── vlan.j2 └── vlan_builder.py /README.md: -------------------------------------------------------------------------------- 1 | # jinja2-template -------------------------------------------------------------------------------- /bgp_builder.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | ############################################################## 4 | # Network Automation Template Configurations 5 | # Author: Stuart Clark 6 | # 7 | # https://blogs.cisco.com/developer/network-automation-template-configurations 8 | ############################################################## 9 | 10 | from jinja2 import Environment, FileSystemLoader 11 | 12 | #This line uses the current directory 13 | file_loader = FileSystemLoader('.') 14 | # Load the enviroment 15 | env = Environment(loader=file_loader) 16 | template = env.get_template('bgp_template.j2') 17 | output = template.render(local_asn='1111', bgp_neighbor='192.168.1.1', remote_asn='2222') 18 | #Print the output 19 | print(output) -------------------------------------------------------------------------------- /bgp_template.j2: -------------------------------------------------------------------------------- 1 | router bgp {{local_asn}} 2 | neighbor {{bgp_neighbor}} remote-as {{remote_asn}} 3 | ! 4 | address-family ipv4 5 | neighbor {{bgp_neighbor}} activate 6 | exit-address-family -------------------------------------------------------------------------------- /hello_template.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | ############################################################## 4 | # Network Automation Template Configurations 5 | # Author: Stuart Clark 6 | # 7 | # https://blogs.cisco.com/developer/network-automation-template-configurations 8 | ############################################################## 9 | 10 | from jinja2 import Environment, FileSystemLoader 11 | 12 | #This line uses the current directory 13 | file_loader = FileSystemLoader('.') 14 | #This line uses the specifies which directory the temapate is in 15 | # file_loader = FileSystemLoader('my_templates') 16 | # Load the enviroment 17 | env = Environment(loader=file_loader) 18 | template = env.get_template('hello_world.j2') 19 | output = template.render() 20 | #Print the output 21 | print(output) -------------------------------------------------------------------------------- /hello_world.j2: -------------------------------------------------------------------------------- 1 | Hello World 2 | -------------------------------------------------------------------------------- /vlan.j2: -------------------------------------------------------------------------------- 1 | {% for vlan in vlans -%} 2 | {{vlan}} 3 | {% endfor -%} 4 | -------------------------------------------------------------------------------- /vlan_builder.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | ############################################################## 4 | # Network Automation Template Configurations 5 | # Author: Stuart Clark 6 | # 7 | # https://blogs.cisco.com/developer/network-automation-template-configurations 8 | ############################################################## 9 | 10 | from jinja2 import Environment, FileSystemLoader 11 | 12 | #This line uses the current directory 13 | file_loader = FileSystemLoader('.') 14 | # Load the enviroment 15 | env = Environment(loader=file_loader) 16 | template = env.get_template('vlan.j2') 17 | vlans = ['vlan10', 'vlan20', 'vlan30'] 18 | output = template.render(vlans=vlans) 19 | #Print the output 20 | print(output) --------------------------------------------------------------------------------