第一步:生成目录结构
config_file
|_templates
| |_nginx.conf.j2
|_nginx.conf
第二步:写一个用于解析模板的函数
from jinja2 import PackageLoader,Environment
def j2(package_name,package_path,template_name,file_name,*args,**kwargs):
env = Environment(loader=PackageLoader(package_name,package_path))
template = env.get_template(template_name)
t=template.render(*args,**kwargs)
with open(file_name,'w') as f:
f.write(t)
第三步:执行函数
j2('config_file','templates','nginx.conf.j2','nginx.conf',name='123')
执行完毕会生成解析过的nginx.conf 配置文件。
标签:file,template,j2,name,nginx,conf,解析,模板,函数 From: https://www.cnblogs.com/wangend/p/17071028.html