1

这是什么意思:

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

1 回答 1

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

这只是内联模板的问题,您不需要对基于文件的模板执行此操作。例如这会很好:

诠释.tpl

#!/bin/bash

echo ${consul_address} 

模板.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}
于 2018-02-15T11:09:00.980 回答