代码之家  ›  专栏  ›  技术社区  ›  M A.

terraform允许我们重写变量吗

  •  0
  • M A.  · 技术社区  · 7 年前

    terraform是否提供了覆盖变量值的功能?假设我声明了下面给出的两个变量。

    variable "foo" {}
    variable "bar" { default = "false"}
    

    foo 是强制性的 bar resoruce 在我可以重新分配或覆盖 resource 观点。我知道我可以用 terraform-modules .

    我试过这个用法 null_resource 但没有得到预期的结果。它仍然返回默认值。

    resource "null_resource" "this" {
      provisioner "local-exec" {
        command = "echo ${var.env} > ${var.newvar}"
      }
    }
    

    curl 在里面 命令属性 . 我需要用翻译吗。如果是的话,它的价值是什么?

    interpreter = ["shell","?"] 我应该传递什么值集来执行 卷曲 local-exec 供应者。

    function check_efs() {
    curl -ls https://elasticfilesystem.us-east-1.amazonsaws.com
    if [ $? -eq 0 ]; then
            output=1
    else:
            output=0
    }
    
    function produce_output() {
    value=$(output)
    
    jq -n \
        --arg is_efs_exist "$value" \
        '{"is_efs_exist":$is_efs_exist}'
    }
    
    check_efs
    produce_output
    
    1 回复  |  直到 7 年前
        1
  •  4
  •   KJH    7 年前

    有几种方法可以解决这个问题。我不知道你的具体使用情况,所以你的里程数可能会有所不同。

    1. 可以导出具有要使用的值的环境变量。例如,您可以设置 bar var到新值 export TF_VAR_bar=newvalue 然后跑 terraform 在那次会议上。或者将它们组合在同一行上: TF_VAR_bar=newvalue terraform apply

      裁判: https://www.terraform.io/docs/configuration/environment-variables.html

    2. 使用覆盖文件。例如。: override.tf 可能包含 variable "bar" { default = "newvalue"} 或任何其他TF代码。它是最后装的。

      裁判: https://www.terraform.io/docs/configuration/override.html

    3. 酒吧 你喜欢的。如果您一直在重复使用相同的代码,并且希望使用不同的参数为某些资源集提供不同的实例,那么这一点尤其有用。

      裁判: https://www.terraform.io/docs/configuration/modules.html

    嗯!