首页 > 解决方案 > 在 Terraform 的输出字符串中转义引号

问题描述

我正在使用 Terraform 0.14.2 开发一个 API 项目。我想创建一个输出来打印我可以在开发 API 时使用的调试 CURL 命令。

这是我要输出的示例

curl -X POST -H 'Content-Type: application/json' -d '{"test": "yes"}' https://foo.bar.baz

我的 Terraform 输出如下所示:

output "test" {
  value = "curl -X POST -H 'Content-Type: application/json' -d '${jsonencode({test = "yes"})}' https://foo.bar.baz"
}

Terraform 对此很满意,但它会生成以下实际输出

cur -X POST -H 'Content-Type: application/json' -d '{\"test\": \"yes\"}' https://foo.bar.baz

我尝试手动编写 JSON:

output "test" {
  value = "curl -X POST -H 'Content-Type: application/json' -d '{\"test\" = \"yes\"}' https://foo.bar.baz"
}

但我得到相同的输出。字符串转义在 terraform 中的工作方式是否与其他所有语言不同?举一个更简单的例子,以这个 terraform 为例:

output "quote" {
  value = "\""
}

这个输出"\""我希望它输出"

标签: terraform

解决方案


你想要的是原始输出。您必须明确请求这样的输出:

terraform output -raw test

但是,我不确定您是否可以强制terraform apply使用原始输出,而不是转义输出。


推荐阅读