Fabric和Jinja模板上传

5 投票
3 回答
4195 浏览
提问于 2025-04-16 22:49

出于某种原因,Jinja说它找不到我在Fabric任务中指定的模板:

httpd_local = "/path/to/dir/conf/" # with httpd.conf located here
httpd_remote = "/etc/httpd/conf/httpd.conf"

with lcd(httpd_local):
    upload_template(filename='/path/to/dir/conf/httpd.conf', destination=httpd_remote, context=context[hostname], use_jinja=True)

但是每次我运行的时候,都会得到

jinja2.exceptions.TemplateNotFound: /path/to/dir/conf/httpd.conf

这个模板确实在那儿啊。到底发生了什么事呢?

3 个回答

0

这可能实际上是个错误。

可以看看这个链接: https://github.com/fabric/fabric/issues/621

2

来自upload_template的文档说明:

另外,如果将 use_jinja 设置为真(True),并且你有Jinja2这个模板库可用,那么就会用Jinja来渲染模板。默认情况下,模板会从调用这个功能的用户当前的工作目录加载,或者如果提供了 template_dir,就会从这个指定的目录加载。

这让人有点困惑。

5

为了更清楚地说明oselivanov的回答,这里是一个格式正确的例子:

httpd_local = "/path/to/dir/conf/" # with httpd.conf located here
httpd_remote = "/etc/httpd/conf/httpd.conf"

with lcd(httpd_local):
    upload_template(filename='httpd.conf', destination=httpd_remote, template_dir='/path/to/dir/conf', context=context[hostname], use_jinja=True)

撰写回答