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

标签 terraform

有没有办法覆盖 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/

相关文章:

amazon-web-services - 如何在 Terraform 中配置 CloudWatch Lambda Insights

amazon-web-services - Terraform 在配置 S3 后端时出错

amazon-web-services - 如何在 Terraform 子文件夹中声明托管资源

Azure AKS - oms 代理和诊断设置可以一起使用吗?

aws-api-gateway - 如何通过 Terraform 设置 aws cloudwatch 保留

aws-lambda - 如何使 Terraform archive_file 资源获取对源文件的更改?

terraform 错误 - 未知的过滤器 key : MSRC_SEVERITY

terraform - 使用terraform模块时如何 "override"一些资源参数?

terraform - 如果 Terraform 的条件

amazon-web-services - eip 分配 id 的 Terraform 输出