I'm currently working on a Python script to pull in data from a csv file to generate cisco ASR config utilizing a Jinja2 template. All of that is working fine; however, I need to modify one row so that the generated output will be in the correct ASR configuration syntax. I have hundreds of these to do and would love to simply be able to generate the ASR config properly for a simple copy and paste into devices. Could someone please guide me on the easiest way to do this?
My Python script:
import os
import jinja2
import csv
import re
# csv fileused
csv_file = "BFL1_AR1.csv"
with open(csv_file) as f:
read_csv = csv.DictReader(f)
for list_elements in read_csv:
generated_list = list_elements['TEST']
generated_list = generated_list.split()
list_elements['TEST'] = generated_list
pattern = re.findall('4323:(\d{1,9})\:\DD', )
print (pattern)
template_file = 'asr_generate_config.j2'
with open(template_file) as f:
list_template = f.read()
template = jinja2.Template(list_template)
print()
print('-' * 80)
print(template.render(list_elements))
print('-' * 80)
print()
Generated output:
--------------------------------------------------------------------------------
l2vpn bridge group 4323:63210:BD
l2vpn bridge group 4323:63210:BD bridge-domain 4323:63210:BD
l2vpn bridge group 4323:63210:BD bridge-domain 4323:63210:BD mac withdraw state-down
l2vpn bridge group 4323:63210:BD bridge-domain 4323:63210:BD mtu 9216
l2vpn bridge group 4323:63210:BD bridge-domain 4323:63210:BD interface be10.149
l2vpn bridge group 4323:63210:BD bridge-domain 4323:63210:BD interface be50.149
l2vpn bridge group 4323:63210:BD bridge-domain 4323:63210:BD vfi 4323:63210:BD
l2vpn bridge group 4323:63210:BD bridge-domain 4323:63210:BD vfi 4323:63210:BD vpn-id 1024571
l2vpn bridge group 4323:63210:BD bridge-domain 4323:63210:BD vfi 4323:63210:BD autodiscovery bgp
l2vpn bridge group 4323:63210:BD bridge-domain 4323:63210:BD vfi 4323:63210:BD autodiscovery bgp rd auto
l2vpn bridge group 4323:63210:BD bridge-domain 4323:63210:BD vfi 4323:63210:BD autodiscovery bgp route-target 4323:63210
interface be10.149 l2transport
interface be10.149 l2transport description CA/KXFN/201755/LVLC
interface be10.149 l2transport encapsulation dot1q 149
interface be10.149 l2transport rewrite ingress tag pop 1 symmetric
interface be10.149 l2transport ethernet-services access-group L2-FRAMES ingress
interface be50.149 l2transport
interface be50.149 l2transport description CA/KXFN/201755/LVLC
interface be50.149 l2transport encapsulation dot1q 149
interface be50.149 l2transport rewrite ingress tag pop 1 symmetric
interface be50.149 l2transport ethernet-services access-group L2-FRAMES ingress
--------------------------------------------------------------------------------
What I need to change:
Every instance that has this data format: 4323:63210:BD --> 4323_63210
The bridge group ID will stay the same for every customer. I need to change the first semicolon to an underscore and remove the last semicolon and B & D letters. I have been trying to do this with regex & re.findall but haven't had much luck.