You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ansible的extra.conf.j2中导入main.conf.j2模板文件

在Jinja2模板中导入其他模板的方法

完全可以在extra.conf.j2中导入main.conf.j2,用Jinja2内置的{% include %}指令就能实现,这样只需维护main.conf.j2一个文件,变更会自动同步到所有引用它的模板。

修改后的示例:

main.conf.j2(无需改动)

memory = {{memory.input}}
cpu = {{cpu.input}}
domain = {{domain.input}}

extra.conf.j2(添加导入指令)

number.of.server = {{num_of_servers.input}}
{% include 'main.conf.j2' %}

注意事项:

  • 确保main.conf.j2的路径在Jinja2的模板搜索路径范围内,如果模板不在同一目录,需要填写相对路径或绝对路径,比如{% include 'templates/main.conf.j2' %}
  • 导入的模板会继承当前模板的变量上下文,memory.input这类变量在extra.conf.j2中能正常渲染,无需额外传递

内容的提问来源于stack exchange,提问作者dzbeda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 00:40:44