Files
Automatisation-Reseau/TP-02/scripts/create_config.py
2025-11-13 09:45:43 +01:00

64 lines
2.0 KiB
Python

import json
from jinja2 import Template, Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader("templates"))
def load_json_data_from_file(file_path):
with open(file_path) as json_file:
data = json.load(json_file)
return data
def load_yaml_data_from_file(file_path):
with open(file_path) as yaml_file:
data = yaml.safe_load(yaml_file)
return data
def render_network_config(template_name, data):
template = env.get_template(template_name)
return template.render(data)
def save_built_config(file_name, data):
with open(file_name, "w") as f:
f.write(data)
return file_name
def create_vlan_config_cpe_marseille():
"""
Must return two values : router config and the switch config
"""
esw2_data = load_json_data_from_file(file_path='data/vlan_ESW2.json')
esw2_config = render_network_config(template_name='vlan_switch.j2', data=esw2_data)
R2_data = load_json_data_from_file(file_path='data/vlan_R02.json')
R2_config = render_network_config(template_name='vlan_router.j2', data=R2_data)
return R2_config,esw2_config
def create_vlan_config_cpe_paris():
"""
Must return two values : router config and the switch config
"""
esw3_data = load_json_data_from_file(file_path='data/vlan_ESW3.json')
esw3_config = render_network_config(template_name='vlan_switch.j2', data=esw3_data)
R3_data = load_json_data_from_file(file_path='data/vlan_R03.json')
R3_config = render_network_config(template_name='vlan_router.j2', data=R3_data)
return R3_config,esw3_config
pass
if __name__ == "__main__":
"""
process question 1 to 5:
"""
r02_config, esw2_config = create_vlan_config_cpe_marseille()
save_built_config('config/vlan_R02.conf', r02_config)
save_built_config('config/vlan_ESW2.conf', esw2_config)
r03_config, esw3_config = create_vlan_config_cpe_paris()
save_built_config('config/vlan_R03.conf', r03_config)
save_built_config('config/vlan_ESW3.conf', esw3_config)