本文介绍了我可以将 terraform 输出设置为 env 变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此 terraform 生成了一堆我感兴趣的输出.如何将这些值通过管道传输到环境变量或其他内容,以便我的脚本能够在有人运行 terraform apply 后获取它们?

So terraform generates a bunch of the output I'm interested in. How can pipe those values to environment variables or something such that my script will be able to pick them up after someone runs terraform apply?

推荐答案

Terraform不能直接修改调用它的shell的环境--一般情况下,程序启动时只能修改自己的环境或者设置新的环境一个子程序本身——所以这里的任何解决方案都将涉及在 terraform apply 返回后在调用 shell 中采取额外的操作.

Terraform can't directly modify the environment of the shell that called it --generally, a program can only modify its own environment or set a new environment when starting a child program itself -- so any solution here will involve taking additional action in the calling shell once terraform apply returns.

一种方法是将 terraform output -json 的输出通过管道传输到一个程序中,该程序可以解析 JSON,提取相关值,然后将它们作为一个或多个 shell 打印出来语句来设置环境变量.然后,您可以将该程序的结果作为 shell 脚本运行,例如在 bash 中使用 source 命令,将这些环境变量值合并到您当前的 shell 中.

One way you could do this is to pipe the output from terraform output -json into a program that can parse the JSON, extract the relevant values, and then print them out as one or more shell statements to set environment variables. You could then run the result of that program as a shell script, for example using the source command in bash, to incorporate those environment variable values into your current shell.

您可以使用您熟悉的任何编程语言编写此转换程序.对于简单的情况,您可能想使用 jq 编写一个简单的 shell 脚本,依靠其 @sh 转义模式来保证有效的 shell 引用值:

You could write this transforming program in any programming language you are comfortable with. For simple cases you might like to write a simple shell script using jq, relying on its @sh escaping mode to guarantee valid shell quoting of the values:

terraform output -json | jq -r '@sh "export EXAMPLE1=\(.example1.value)\nexport EXAMPLE2=\(.example2.value)"'

我使用以下测试 Terraform 配置尝试了此操作:

I tried this with the following test Terraform configuration:

output "example1" {
  value = "hello"
}

output "example2" {
  value = <<EOT
Hello world
This is a multi-line string with 'quotes' in "it".
EOT
}

应用该配置后,上面的命令产生以下输出:

After applying that configuration, the command above produced the following output:

export EXAMPLE1='hello'
export EXAMPLE2='Hello world
This is a multi-line string with '\''quotes'\'' in "it".
'

我将输出重定向到文件 env.sh 然后将其加载到我的 shell 中以确认变量可用:

I redirected the output to a file env.sh and then loaded it into my shell to confirm that the variables were usable:

$ terraform output -json | jq -r '@sh "export EXAMPLE1=\(.example1.value)\nexport EXAMPLE2=\(.example2.value)"' >env.sh
$ source env.sh
$ echo $EXAMPLE1
hello
$ echo "$EXAMPLE2"
Hello world
This is a multi-line string with 'quotes' in "it".

(请注意,对于 EXAMPLE2,我将变量插值放在引号中,否则 shell 会将每个空格分隔的单词理解为一个单独的参数,而不是将整个值视为一个单独的参数所有空白字符完好无损的字符串.)

(Note that for EXAMPLE2 I put the variable interpolation in quotes, because otherwise the shell will understand each of the space-separated words as a separate argument, rather than considering the whole value as a single string with all of the whitespace characters intact.)

这篇关于我可以将 terraform 输出设置为 env 变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 22:59