在终端中打印terraform模板渲染输出?

时间:2016-06-17 18:15:08

标签: terraform

在terraform文档中,它显示了如何使用模板。有没有办法将此渲染输出记录到控制台?

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

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

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

4 个答案:

答案 0 :(得分:6)

您需要运行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!

答案 1 :(得分:2)

仔细观察,这是数据而非资源

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

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

答案 2 :(得分:1)

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

答案 3 :(得分:0)

最佳答案来自达沃斯

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

即使渲染的模板是无效的 json,这也能工作

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