【问题标题】:Terraform > Unescaped interpolationsTerraform > 未转义的插值
【发布时间】:2018-07-26 02:27:20
【问题描述】:

这是什么意思:

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

【问题讨论】:

    标签: terraform hcl


    【解决方案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 的模板变量(这反过来又解析为 aws_instance.consul 资源的 private_ip 输出。

    这只是内联模板的问题,您不需要对基于文件的模板执行此操作。例如这样就可以了:

    int.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}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-03
      • 2017-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-20
      • 2022-01-12
      • 1970-01-01
      相关资源
      最近更新 更多