-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-generator.py
50 lines (44 loc) · 1.56 KB
/
config-generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import argparse
import yaml
from yaml import CLoader as Loader, CDumper as Dumper
from jinja2 import Environment, FileSystemLoader
def main(template_file, source_file):
#Load data from YAML file into Python dictionary
device_variables = yaml.load(open(source_file), Loader=Loader)
#Load Jinja2 template
env = Environment(loader = FileSystemLoader('./'), trim_blocks=True, lstrip_blocks=True)
template = env.get_template(template_file)
#Render template using data and print the output
config = template.render(device_variables)
hostname = device_variables['hostname']
if (write_config_to_file(config, hostname)):
print(f"Configuration generated successfully. File is at ./{hostname}.txt")
else:
print("Error generating configuration.")
def write_config_to_file(config, filename):
# config should be a string format
try:
f = open(f"{filename}.txt", "w")
f.write(config)
return True
except:
return False
finally:
f.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generate device configurations from templates.",
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument("-s", "--source", type=str, help="Path to file containing source data (.yaml).")
parser.add_argument(
"-t",
"--template",
type=str,
help="Path to file containing Jinja template (.j2).",
)
args = parser.parse_args()
main(
template_file=args.template,
source_file=args.source
)