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

田博易
2023-12-01

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 instructionswhich 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.


 类似资料: