HashiCorp Terraform 0.12 新特性抢鲜看:模版语法

AUG 09 2018    MARTIN ATKINS

This is the seventh post of the series highlighting new features in Terraform 0.12.

As part of the lead up to the release of Terraform 0.12, we are publishing a series of feature preview blog posts. The post this week is on the addition of the template syntax.

Terraform has always supported basic string interpolation using the ${...}syntax. Terraform 0.12 extends this syntax to support loops and conditionals that work directly within any value in a Terraform configuration.

Template Syntax

We've recognized that more complex templating is a common requirement of Terraform configurations. Terraform 0.12 introduces a new template syntax that is usable for any value.

The string interpolation syntax ${ ... } has been part of Terraform since its initial release in 2015. This continues to work in Terraform 0.12, but is now extended to include support for conditionals and for expressions. These new features are most useful within multi-line strings:

# Configuration for Terraform 0.12

locals {
  lb_config = <<EOT
%{ for instance in opc_compute_instance.example ~}
server ${instance.label} ${instance.ip_address}:8080
%{ endfor }
EOT
}

After evaluating the above, the expression local.lb_config would evaluate to a string like the following:

server example0 192.168.2.12:8080
server example1 192.168.2.65:8080
server example2 192.168.2.23:8080

These new capabilities can be used for any value in Terraform. Because Terraform automatically converts values to their correct type, templates can even be used for numbers, booleans, and more.

These new capabilities are also available in the template_file data source after upgrading to the Template provider v2.0.0 release or newer. The template provider has many additional use cases and will continue to be supported, but we expect that most users will be able to use the new template syntax directly from Terraform 0.12 onwards.

If we had the freedom to start fresh with our template syntax we would ideally have used an existing template syntax, but since compatibility with the existing interpolations was a requirement we instead defined this new syntax in the spirit of our existing interpolation syntax while taking cues from the behaviors of other template languages. The template_file data source may still get support for other template engines in subsequent releases.

Upgrade Guide

This feature introduces no breaking changes. Existing string interpolation remains unchanged and the existing template provider and resources continue to work as they do in current Terraform versions.

Next

This was part 7 of the blog post series previewing Terraform 0.12.

The new template syntax will be released in Terraform 0.12, coming later this summer. To learn more about how to upgrade to Terraform 0.12, read the upgrade instructions which will be continuously updated as we get closer to releasing Terraform 0.12. If you have any feedback or concerns about these changes, please communicate with the Terraform team via the public mailing list.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值