为 local-exec 设置 Terraform 默认解释器

wrs*_*der 5 terraform

有没有办法覆盖local-exec配置器中使用的默认 Terraform 解释器?

我知道您可以通过interpreter参数设置解释器,但我试图避免在每个单独的资源上指定它。

我真正想做的是覆盖“基于系统操作系统选择的合理默认值”,以使脚本跨平台。具体来说,我想通过环境或命令行变量更改默认值,以便我可以在 Windows 上使用 Cygwin Bash,用于最初为 Linux 制作的脚本。

存在这样的能力吗?

https://www.terraform.io/docs/provisioners/local-exec.html

aar*_*ers 7

今天这是不可能的。这是跨平台示例代码,您可以在任何环境中使用它(托管在我的gist上)。

首先是检测操作系统的片段:

locals {
  # Directories start with "C:..." on Windows; All other OSs use "/" for root.
  is_windows = substr(pathexpand("~"), 0, 1) == "/" ? false : true
}
Run Code Online (Sandbox Code Playgroud)

然后根据使用的操作系统选择解释器和命令:

locals {
  # Directories start with "C:..." on Windows; All other OSs use "/" for root.
  is_windows = substr(pathexpand("~"), 0, 1) == "/" ? false : true
}
Run Code Online (Sandbox Code Playgroud)

最后,一个没有额外注释和触发器的简化视图:

resource "null_resource" "cli_command" {
  provisioner "local-exec" {
    interpreter = local.is_windows ? ["PowerShell", "-Command"] : []
    command     = "sleep 60"
  }
}
Run Code Online (Sandbox Code Playgroud)