Skip to content

Instantly share code, notes, and snippets.

@apalii
Created May 17, 2016 08:48
Show Gist options
  • Save apalii/b78d406479df99855d8e983eeb1b967c to your computer and use it in GitHub Desktop.
Save apalii/b78d406479df99855d8e983eeb1b967c to your computer and use it in GitHub Desktop.
jinja2 config generator example
# ll
-rwxr--r-- 1 root root 305 May 17 04:45 gen_conf.py
drwxr-xr-x 2 root root 4096 May 17 03:24 templates
-rw-r--r-- 1 root root 6 May 17 03:15 vars.csv
# cat templates/test.conf
line 1
line 2
{% for item in items %}line {{ item }}
{% endfor %}
# cat vars.csv
3
4
5
# cat gen_conf.py
from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('templates'))
with open('vars.csv') as vars:
var_list = []
for var in vars:
var_list.append(var.strip())
template = env.get_template('test.conf')
print template.render(items=var_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment