Terraform>非转义插值

时间:2018-02-15 10:50:25

标签: terraform hcl

这是什么意思:

Note: Inline templates must escape their interpolations (as seen by the double 
$ above). Unescaped interpolations will be processed before the template.

来自https://www.terraform.io/docs/providers/template/index.html

具体的例子是:

# Template for initial configuration bash script
data "template_file" "init" {
  template = "$${consul_address}:1234"

  vars {
    consul_address = "${aws_instance.consul.private_ip}"
  }
}

1 个答案:

答案 0 :(得分:1)

在模板渲染发生之前,HCL使用${}语法进行插值,如果您只是使用:

# Template for initial configuration bash script
data "template_file" "init" {
  template = "${consul_address}:1234"

  vars {
    consul_address = "${aws_instance.consul.private_ip}"
  }
}

Terraform将尝试在输出中找到consul_address模板,而不是使用consul_address的模板变量(后者又被解析为private_ip aws_instance.consul输出资源。

这只是内联模板的一个问题,您无需为基于文件的模板执行此操作。例如,这没关系:

int.tpl

#!/bin/bash

echo ${consul_address} 

template.tf

# Template for initial configuration bash script
data "template_file" "init" {
  template = "${file("init.tpl")}"

  vars {
    consul_address = "${aws_instance.consul.private_ip}"
  }
}

当然,如果您还需要在输出模板中逐字使用${}语法,那么您需要使用以下内容进行双重转义:

#!/bin/bash

CONSUL_ADDRESS_VAR=${consul_address}
echo $${CONSUL_ADDRESS_VAR}

然后将其呈现为:

#!/bin/bash

CONSUL_ADDRESS_VAR=1.2.3.4
echo ${CONSUL_ADDRESS_VAR}