Initial Commit

This commit is contained in:
Jonathan Dale
2024-11-11 16:42:24 -05:00
commit a199aa4c93
6 changed files with 258 additions and 0 deletions

142
.gitignore vendored Normal file
View File

@@ -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/

76
Config_builder.py Normal file
View File

@@ -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)

9
README.md Normal file
View File

@@ -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.

4
example_input.yaml Normal file
View File

@@ -0,0 +1,4 @@
lab-rt0003-bdr:
Lo0_IPv4: 10.10.10.10
Lo300_IPv4: 10.10.20.20
Collector: 10.10.30.10

24
example_template.config Normal file
View File

@@ -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
!

3
requirements.txt Normal file
View File

@@ -0,0 +1,3 @@
Jinja2==3.1.4
MarkupSafe==3.0.2
PyYAML==6.0.2