commit a199aa4c93a1a6448a1d00c49c80766d0e78f7f2 Author: Jonathan Dale Date: Mon Nov 11 16:42:24 2024 -0500 Initial Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e22d43a --- /dev/null +++ b/.gitignore @@ -0,0 +1,142 @@ +# Project Specific +output/* +input/* +templates/* + +# testing files +test-* + +# VS and VSCode workspace +*.code-workspace +.vs/ + + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ \ No newline at end of file diff --git a/Config_builder.py b/Config_builder.py new file mode 100644 index 0000000..f3be933 --- /dev/null +++ b/Config_builder.py @@ -0,0 +1,76 @@ +from jinja2 import Environment, FileSystemLoader +import pprint +import os + +from yaml import safe_load +try: + from yaml import CLoader as Loader +except ImportError: + from yaml import Loader + +def main(template_filepath,yaml_filepath,output_filepath): + + with open(yaml_filepath, 'r') as file: + devices = safe_load(file) + + template_path = os.path.dirname(os.path.abspath(template_filepath)) + template_filename = os.path.basename(template_filepath) + + env = Environment(loader=FileSystemLoader(template_path)) + template = env.get_template(template_filename) + + #Path("./output").mkdir(parents=True, exist_ok=True) + + ### Specify output file - current write to script working directory + #filename = "./output/bdr_output.config" + + ### open output file mode="w" for create/truncate + #output = open(filename, mode="w", encoding="utf-8") + output = open(output_filepath, mode="w", encoding="utf-8") + + + for host, keys in devices.items(): + device = "{" + for key, value in keys.items(): + device += "'{}':'{}', ".format(key,value) + device += "'Hostname':'{}' }}".format(host) + #pprint.pprint(device) + + device_dict = eval(device) + #pprint.pprint(device_dict) + content = template.render( device_dict ) + + print('{}\n'.format(content)) + output.write('{}\n'.format(content)) + + return 0 + +if __name__ == "__main__": + + import argparse + + parser = argparse.ArgumentParser(description='Arguments for ' + 'Config_builder.py') + parser.add_argument('-template', + help='Template config with JINJA markup', + required=True + ) + + parser.add_argument('-yaml', + help='YAML file with devices and config variables', + required=True + ) + + parser.add_argument('-output', + help='Output File', + required=True + ) + + custom_args = parser.parse_known_args()[0] + + template = custom_args.template + yaml = custom_args.yaml + output = custom_args.output + + main(template,yaml,output) + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..8f0f6e0 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# Playing with JINJA2 + +This applet is intended to generate a JSON output snip for use with Grafana. +The applet is statically configured to run 'count' interations on a JSON template with JINJA markup and output +to a statically defined file in the scripts CWD. + +Next steps will be to modify the markup template to include the python +instructions for iterating and the name of the output file instead of having the render script +statically defining those values. diff --git a/example_input.yaml b/example_input.yaml new file mode 100644 index 0000000..3b51725 --- /dev/null +++ b/example_input.yaml @@ -0,0 +1,4 @@ +lab-rt0003-bdr: + Lo0_IPv4: 10.10.10.10 + Lo300_IPv4: 10.10.20.20 + Collector: 10.10.30.10 diff --git a/example_template.config b/example_template.config new file mode 100644 index 0000000..0c189a8 --- /dev/null +++ b/example_template.config @@ -0,0 +1,24 @@ +!!!!!!!! +!! {{Hostname}} +!!!!!!!! +! +vrf definition iONE_MGT + rd {{Lo0_IPv4}}:300 + route-target export 22351:300 + route-target import 22351:300 + ! + address-family ipv4 + exit-address-family +! +interface Loopback300 + description {{Hostname}}_lo300 - NetFlow telemetry source + vrf forwarding iONE_MGT + ip address {{Lo300_IPv4}} 255.255.255.255 +! +router bgp 22351 + address-family ipv4 vrf iONE_MGT + network {{Lo300_IPv4}} mask 255.255.255.255 + exit-address-family +! +ip nat log translations flow-export v9 udp destination {{Collector}} 2056 vrf iONE_MGT source Loopback300 +! diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7d3622b --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +Jinja2==3.1.4 +MarkupSafe==3.0.2 +PyYAML==6.0.2