Terraform 中的变量插值 [英] Variable Interpolation in Terraform

查看:22
本文介绍了Terraform 中的变量插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 terraform 中的变量插值时遇到问题.这是我的 terraform 配置的样子.即内置函数内的变量

I am having trouble in variable interpolation in terraform. Here is what my terraform configuration looks like. i.e variable inside builtin function

variable "key" {}

    ssh_keys {
        path     = "/home/${var.provider["user"]}/.ssh/authorized_keys"
        key_data = "${file(${var.key})}" 
    }

命令:terraform apply -var 'key=~/.ssh/id_rsa.pub'

它不是从命令行参数或环境变量中读取key"的值.但是,当我硬核 .tf 文件中的值时,它可以工作.如下所示.

It's not reading the value of "key" from command line argument or from env variable. However when i hardcore the value in .tf file, it works. Like below.

key_data = "${file("~/.ssh/id_rsa.pub")}"

推荐答案

${ ... } 语法仅在将表达式嵌入到带引号的字符串中时使用.在这种情况下,您的 var.key 变量只是作为参数传递给已经在 ${ ... } 序列中的函数,您可以只引用变量名直接这样:

The ${ ... } syntax is only used when embedding an expression into a quoted string. In this case, where your var.key variable is just being passed as an argument to a function already within a ${ ... } sequence, you can just reference the variable name directly like this:

key_data = "${file(var.key)}" 

嵌套 ${ ... } 序列有时用于将内插字符串传递给函数.在这种情况下,首先会有一组嵌套的引号返回到字符串上下文.例如:

Nested ${ ... } sequences are sometimes used to pass an interpolated string to a function. In that case there would first be a nested set of quotes to return to string context. For example:

key_data = "${file("${path.module}/${var.key_filename}")}" 

在这种更复杂的情况下,首先计算最里面的字符串表达式以使用 / 将两个变量连接在一起,然后将整个字符串传递给 file 函数, 结果最终返回为 key_data 的值.

In this more complicated case, the innermost string expression is first evaluated to join together the two variables with a /, then that whole string is passed to the file function, with the result finally returned as the value of key_data.

这篇关于Terraform 中的变量插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆