Midas/provisioning/midasp.py

36 lines
1.2 KiB
Python
Raw Permalink Normal View History

2023-07-01 23:57:08 +01:00
import yaml
from jinja2 import Environment, FileSystemLoader
from utils.log import log
class Provisioning(object):
2023-07-01 23:57:08 +01:00
def render(self, topology):
'''
Summary:
Builds a configuration file for all network devices in the topology.
Takes:
topology: networkx Graph object detailing the network topology
'''
log('Rendering configs...', 'info')
for node in topology.G.nodes:
if topology.G.nodes[node].get('no_render') == True:
continue
node_os = topology.G.nodes[node]['os']
template_path = Environment(loader=FileSystemLoader('provisioning/templates/'), trim_blocks=True, lstrip_blocks=True)
device_vars = yaml.load(open('provisioning/vars/' + node + '.yaml'), Loader=yaml.SafeLoader)
template = template_path.get_template(node_os + '.j2')
rendered_config = template.render(device_vars)
config_path = '/srv/tftp/configs/' + node + '.conf'
with open(config_path, 'w') as configuration:
configuration.write(rendered_config)
log(f'Rendered {config_path}', 'info')
log('Config rendering complete', 'info')