【问题标题】:include a terraform template in another template在另一个模板中包含一个 terraform 模板
【发布时间】:2021-06-23 05:28:35
【问题描述】:

我有很多 terraform 脚本使用的模板文件,所有模板文件都有一些共同的部分,即:

file a.tmpl:

env=prod
var=a
-------------------
file b.tmpl:

env=prod
var=b

我想将通用部分导出到单独的文件中,这样就不必在每个文件中重复,例如:

file base.tmpl:
env=prod
-------------------
file a.tmpl:

%{ include "base.tmpl" }
var=a
-------------------
file b.tmpl:

%{ include "base.tmpl" }
var=b

但该功能不存在

(它与django 模板功能非常相似,如下所述:https://stackoverflow.com/a/10985987/245024

有没有办法以某种方式进行包含?


我可以通过像这样连接文件来解决问题:

data "template_file" "vars_a" {
  template = "${format("%s \n %s", 
    file("${path.module}/base.tmpl"), 
    file("${path.module}/a.tmpl")
   )}"
}

但这比直接在文件中包含基本模板更具限制性。

【问题讨论】:

    标签: terraform terraform-template-file


    【解决方案1】:

    我认为你可以使用templatefile:

    a.tmpl

    ${file("base.tmpl")}
    var=a
    

    base.tmpl

    var_ddd=ffff
    var_sss=adfs
    

    ma​​in.tf

    data "template_file" "vars_a" {
      template = templatefile("a.tmpl", {})
    }
    
    output "test" {
      value = data.template_file.vars_a.template
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      • 2018-02-02
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多