-
Notifications
You must be signed in to change notification settings - Fork 3
/
configuration-rendering.py
42 lines (32 loc) · 1.38 KB
/
configuration-rendering.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
# usage:
#
# Update devices details:
# vi data.yml
#
# To get help:
# python ./render-telegraf-configuration.py -h
# python ./render-telegraf-configuration.py --help
#
# Generate Telegraf configuration file for Openconfig:
# python ./render-telegraf-configuration.py --output 'configs/telegraf-openconfig.conf' --template 'templates/telegraf-openconfig.j2' --yaml 'data.yml'
# python ./render-telegraf-configuration.py -o 'configs/telegraf-openconfig.conf' -t 'templates/telegraf-openconfig.j2' -y 'data.yml'
#
# Generate Telegraf configuration file for SNMP:
# python ./render-telegraf-configuration.py --output 'configs/telegraf-snmp.conf' --template 'templates/telegraf-snmp.j2' --yaml 'data.yml'
# python ./render-telegraf-configuration.py -o 'configs/telegraf-snmp.conf' -t 'templates/telegraf-snmp.j2' -y 'data.yml'
import yaml
from jinja2 import Template
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-o', '--output', help='rendered file')
parser.add_argument('-t', '--template', help='jinja template file')
parser.add_argument('-y', '--yaml', help='YAML variables file')
args = parser.parse_args()
# Start building telegraf configuration'
with open(args.yaml) as f:
data=yaml.load(f.read())
with open(args.template) as f:
template = Template(f.read(),lstrip_blocks=True, trim_blocks=True)
conf=open(args.output,'w')
conf.write(template.render(data))
conf.close()