如何在模板数据文件中使用$ {something}的文字字符串

时间:2018-08-28 00:06:11

标签: terraform

我有一个模板文件,该文件正在创建流利的文件并插入各种变量。我现在尝试包括this plugin,它期望在配置文件中找到自己的变量。问题在于Terraform在模板${variable}中定义了一个变量,并且该插件希望在文件中以文字${variable}

的形式找到其变量。

我如何告诉terraform不对文件中的${}进行插值,而实际上要传递整个字符串?

文件摘要:

<filter tomcat.logs>
  @type record_transformer
  <record>
    customer ${customer}
    environment ${environment}
    application ${application}
  </record>
</filter>

上面的${}是我为模板定义的所有变量。我现在需要添加这样的部分。

  <record>
    hostname      ${tagset_name}
    instance_id   ${instance_id}
    instance_type ${instance_type}
    az            ${availability_zone}
    private_ip    ${private_ip}
    vpc_id        ${vpc_id}
    ami_id        ${image_id}
    account_id    ${account_id}
  </record>

所有这些都是 not 变量,但实际上它是如何在呈现的模板中显示的。我尝试将它们像$${account_id}那样进行交换,但是最终只是在文件中呈现了account_id。

data "template_file" "app" {
  template = "${file("templates/${var.application}.tpl")}"

  vars {
    customer               = "${var.customer}"
    environment            = "${var.environment}"
    application            = "${var.application}"
  }
}

这里是正在发生的事情的细分。

In the user data I have "instance_type $${instance_type}"  
The launch    configuration that is created for the instances, shows "instance_type    ${instance_type}"  
The actual file that is present on AWS shows    "instance_type"

1 个答案:

答案 0 :(得分:0)

最后弄清楚了。标记为重复的问题的答案对此实例不正确。

template.tpl包含

cat <<EOT > /root/test.file
db.type=${db_type}
instance_type \$${instance_type}
EOT

结果

Error: Error refreshing state: 1 error(s) occurred:

* module.autoscaling_connect.data.template_file.app: 1 error(s) occurred:

* module.autoscaling_connect.data.template_file.app: data.template_file.app: failed to render : 27:16: unknown variable accessed: bogus_value

template.tpl包含

cat <<EOT > /root/test.file
db.type=${db_type}
instance_type \$${instance_type}
EOT

生成包含以下内容的启动配置

cat <<EOT > /root/test.file
db.type=mysql
instance_type \${instance_type}
EOT

我们在包含实例的实例上创建的文件中的结果

db.type=mysql
instance_type ${instance_type}

简而言之,要在从terraform模板文件创建的文件中以${something}结尾,必须在.tpl文件中使用\$${something}