16

在 terraform 文档中,它展示了如何使用模板。有没有办法在控制台上记录这个渲染输出?

https://www.terraform.io/docs/configuration/interpolation.html#templates

data "template_file" "example" {
  template = "${hello} ${world}!"
  vars {
    hello = "goodnight"
    world = "moon"
  }
}

output "rendered" {
  value = "${template_file.example.rendered}"
}
4

5 回答 5

9

terraform apply然后你需要运行terraform output rendered

$ terraform apply
 template_file.example: Creating...
   rendered:   "" => "<computed>"
   template:   "" => "${hello} ${world}!"
   vars.#:     "" => "2"
   vars.hello: "" => "goodnight"
   vars.world: "" => "moon"
 template_file.example: Creation complete

 Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

 The state of your infrastructure has been saved to the path
 below. This state is required to modify and destroy your
 infrastructure, so keep it safe. To inspect the complete state
 use the `terraform show` command.

 State path: terraform.tfstate

 Outputs:

   rendered = goodnight moon!
 $ terraform output rendered
 goodnight moon!
于 2016-06-17T18:22:05.700 回答
3

仔细看,这是数据不是资源

data "template_file" "example" {
template = "${file("templates/greeting.tpl")}"
  vars {
  hello = "goodnight"
  world = "moon"
  }
}

output "rendered" {
  value = "${data.template_file.example.rendered}"
}
于 2020-02-12T09:50:41.033 回答
1

该代码可能是模块的一部分吗?如果它是模块的一部分,则不会显示。您必须将模块的输出放在正在调用模块的位置。

于 2017-11-10T08:36:19.573 回答
0

最好的答案来自达沃斯

  • terraform state list查找资源名称
  • terraform state show <resource name>

即使呈现的模板是无效的 json,这也将起作用

terraform output rendered只有在没有错误发生的情况下才有效。

于 2021-05-26T06:38:47.003 回答
0

注意:如果您将模板指定为文字字符串而不是加载文件,则内联模板必须使用双美元符号(如 $${hello})。

https://www.terraform.io/language/configuration-0-11/interpolation#templates

它是这样工作的:

data "template_file" "example" {
  template = "$${v1} $${v2}!"
  vars = {
    v1 = "hello"
    v2 = "world"
  }
}

output "rendered" {
  value = data.template_file.example.rendered

}

输出:渲染=“你好世界!”

于 2022-02-10T14:16:25.723 回答