terraform 计划输出重定向到文件不捕获错误消息

terraform plan output redirection to file does not capture error messages

我之前使用 unix 输出重定向成功地将 terraform plan 输出捕获到一个文件中:

tf plan -no-color > plan.txt

当我的plan(或apply)出现错误时,尽管我在终端输出中看到错误消息,但输出文本文件为空。

如何在出现错误的情况下捕获输出?

如果您想重定向所有内容,您可以执行以下操作:

tf plan -no-color > plan.txt 2>&1

Terraform 有一个内置变量 TF_LOG_PATH 可将包括错误在内的所有输出重定向到特定文件。您不需要重定向输出。

示例:

export TF_LOG_PATH=/mydirectory/mylogfile.log
terraform plan

来源:https://www.terraform.io/docs/internals/debugging.html

我在参考资料here.

时发现是因为unix中的stderr(标准错误)和stdout(标准输出)输出

2> 可用于重定向标准错误。
&> 可用于重定向 stdout 和 std err。
1>> 可用于重定向标准输出。

最终我使用这个命令重定向到 2 个文件(我更喜欢):

tf plan -no-color 2> err.txt 1> out.txt

更多信息

我的初始命令,

tf plan -no-color > plan.txt

只会将标准输出重定向到文件。

因此,当计划出错时,没有标准输出,空输出覆盖并清空了我的 plan.txt 文件,错误转到标准错误。