【问题标题】:Print terraform template rendered output in terminal?在终端中打印 terraform 模板呈现的输出?
【发布时间】:2016-10-19 15:57:17
【问题描述】:

在 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}"
}

【问题讨论】:

  • 如果您不需要输出变量,您也可以只使用terraform state show template_file.example,它将打印所有数据源的属性,包括rendered

标签: terraform


【解决方案1】:

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

【讨论】:

  • 注意terraform refresh就足够了;你不必terraform apply
  • 如果您刚刚运行terraform apply,则不需要执行terraform output rendered,该变量将为您输出,如您上面的答案所示Outputs: rendered = goodnight moon! 您需要运行的唯一原因terraform output 是如果您没有运行应用程序,或者如果您想查看已标记为sensitive = true的输出的值@
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      最好的答案来自达沃斯

      • terraform state list 查找资源名称
      • terraform state show &lt;resource name&gt;

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

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

      【讨论】:

        【解决方案5】:

        注意:如果您将模板指定为文字字符串而不是加载文件,则内联模板必须使用双美元符号(如 $${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
        
        }
        

        输出:rendered = "hello world!"

        【讨论】:

          猜你喜欢
          • 2020-02-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多