使用 terraform 转义字符串中的特殊字符

Sir*_*nth 4 terraform terraform-provider-azure terraform-provider-aws terraform-provider-gcp terraform0.12+

您能帮我使用 terraform 实现以下场景吗?我需要在字符串值中的每个特殊字符前加上 // 前缀。

示例:mysplchr="test O'riel*abc"这必须更改为"test O//'riel//*abc"

谢谢

Mar*_*cin 9

不确定这里的问题是什么,但如果需要,您可以直接编写或自动更改原始字符串:

variable "mysplchr" {
     default = "test O//'riel//*abc"
}

output "test1" {
  value = var.mysplchr
}

# or to do it automatically for
# the original string
output "test2" {
  value = replace("test O'riel*abc", "/(['\\*])/", "//$1")
}
Run Code Online (Sandbox Code Playgroud)

结果是:

test1 = test O//'riel//*abc
test2 = test O//'riel//*abc
Run Code Online (Sandbox Code Playgroud)

  • 救星!为什么这不是公认的答案? (2认同)