Python Cheetah - Specify name/value pairs for templating
I am trying to template-ize my Apache httpd configuration for deployment to different environments and I would like to use the Python language Cheetah application to do so. However, I am having difficulty with the command line cheetah
program and I believe its a combination of my misunderstanding Cheetah along with a lack of documentation.
My goal is to have a single httpd.conf template file and substitute variables from an environment specific definition file.
httpd.tmpl:
Listen $HTTP_PORT
...
#if $ENABLE_HTTPS == true
<Virtual Host *:$HTTPS_PORT>
...
</VirtualHost>
#end if
production.env:
HTTP_PORT=34120
HTTPS_PORT=34121
ENABLE_HTTPS=true
What is the command line needed to fill this Cheetah template? I've used:
cheetah f --oext conf --debug httpd
But obviously prod.env
is not read as an input file. Adding an #include
to the top of my template file:
#include "prod.env"
And none of my names are found:
Cheetah.NameMapper.NotFound: cannot find 'APACHE_PORT'
This is not the ideal situation anyway, because I want the ability to specify the name/value mapping file on the command line for each invocation of cheetah.
Thanks!
EDIT: I am aware that I can write a python script to perform the file reading and then substitute using the Cheetah API. However, I'm looking for a way to use the command line to fill my template.
SOLVED
Thanks to the documentation link provided by @pyfunc I now understand how to accomplish this. The main issue is to supply --env
on the cheetah command line which passes the current environment variables to cheetah. These environment variables must be exported first开发者_如何学运维 however.
Cheetah wraps each chunk of #include text inside a nested Template object.
Use
#include raw "prod.env"
also
#set global $HTTP_PORT="34120"
To include different env files, you will have too templatize that too.
Please look at the following for examples that should help you.
- http://packages.python.org/Cheetah/dev_guide/output.html
精彩评论