有没有办法覆盖 local-exec
中使用的默认 Terraform 解释器?供应商?
我知道您可以通过 interpreter
设置解释器参数,但我试图避免在每个单独的资源上指定它。
我真正想做的是覆盖“基于系统操作系统选择的合理默认值”,以使脚本跨平台。具体来说,我想通过环境或命令行变量更改默认值,以便我可以在 Windows 上使用 Cygwin Bash,用于最初为 Linux 制作的脚本。
有这样的能力吗?
https://www.terraform.io/docs/provisioners/local-exec.html
最佳答案
今天是不可能的。这是您可以在任何环境中使用的跨平台示例代码(托管在我的 gist 上)。
首先是检测操作系统的片段:
locals {
# Directories start with "C:..." on Windows; All other OSs use "/" for root.
is_windows = substr(pathexpand("~"), 0, 1) == "/" ? false : true
}
然后根据使用的操作系统选择解释器和命令:
resource "null_resource" "cli_command" {
provisioner "local-exec" {
# Ensure windows always uses PowerShell, linux/mac use their default shell.
interpreter = local.is_windows ? ["PowerShell", "-Command"] : []
# TODO: Replace the below with the Windows and Linux command variants
command = local.is_windows ? "sleep 60" : "sleep 60"
}
triggers = {
# TODO: Replace this psuedocode with one or more triggers that indicate (when changed)
# that the command should be re-executed.
"test_a" = resource.my_resource.sample
}
}
最后,一个没有额外注释和触发器的简化 View :
resource "null_resource" "cli_command" {
provisioner "local-exec" {
interpreter = local.is_windows ? ["PowerShell", "-Command"] : []
command = "sleep 60"
}
}
关于terraform - 为 local-exec 设置 Terraform 默认解释器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52628749/