【问题标题】:Terraform, (AzureRM) Can you have if, else, else (conditional situations) for resource deployments?Terraform,(AzureRM)您可以为资源部署提供 if、else、else(条件情况)吗?
【发布时间】:2021-04-30 16:56:01
【问题描述】:

我使用 Azure DevOps 和 Terraform 在我的订阅中部署多个环境,并且我在我的所有环境中使用相同的 main.tf、variables.tf 和 tfvars(总共 3 个环境)。在 tf 文件和 tfvars 文件中,我在变量组(特定于 Azure DevOps)中传递 DevOps 变量以识别不同的变量和值。例如,我想用相同的资源(每个订阅 1 个)构建 3 个不同的子网:

Main.tf:

resource "azurerm_subnet" ""example" { 
  for_each             = var.is_env_one ? var.subnet_range_one : var.subnet_range_two : var.subnet_range_three
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name

  name                 = each.value["name"]
  address_prefixes     = each.value["address_prefixes"]
}

变量.tf:

variable "is_env_one" {
  description = "Boolean on if this is environment 1 or not"
  default = true 
}
variable "is_env_two" {
  description = "Boolean on if this is environment 2 or not"
  default = true 
}
variable "is_env_three" {
  description = "Boolean on if this is environment 3 or not"
  default = true 
}
variable "subnet_range_one" {
  description = "tfvars file will dictate # of subnets and values"
}
variable "subnet_range_two" {
  description = "tfvars file will dictate # of subnets and values"
}
variable "subnet_range_three" {
  description = "tfvars file will dictate # of subnets and values"
}

tfvars 信息(范围配置 1 的示例):

subnet_range_one = {
  subnet_1 = {
    name             = "{[_subnet-devops-name_]}" #Azure DevOps Variable that dictates the value
    address_prefixes = "{[_subnet-devops-address-prefix_]}" #Azure DevOps Variable that dictates the value
  }
}

我有没有办法编写代码来区分使用 DevOps 变量的环境? Aka,我可以使用条件格式说选择 a、b 或 c 三个选项吗)?

【问题讨论】:

    标签: azure if-statement conditional-statements terraform


    【解决方案1】:

    我认为最简单且最具可扩展性的方法(如果您想稍后添加新环境)是为其使用一个变量。

    如下所示(仅作为示例):

    variable "env_subnets" {
    
      default = {
    
          env1 = [
              {
                  name = name11
                  address_prefixes = prefix11
              },
              {
                  name = name12
                  address_prefixes = prefix12
              }          
          ],
          env2 = [
              {
                  name = name21
                  address_prefixes = prefix21
              },
              {
                  name = name22
                  address_prefixes = prefix22
              }          
          ],
          env3 = [
              {
                  name = name31
                  address_prefixes = prefix31
              },
              {
                  name = name32
                  address_prefixes = prefix32
              }          
          ]
      }
    }
    
    
    resource "azurerm_subnet" "example" { 
    
      for_each             = {for idx, val in var.env_subnets["env1"]: idx => val}
    
      resource_group_name  = azurerm_resource_group.example.name
      virtual_network_name = azurerm_virtual_network.example.name
    
      name                 = each.value["name"]
      address_prefixes     = each.value["address_prefixes"]
    }
    

    在上面,您只需在var.env_subnets["env1"] 中使用不同的密钥来为不同的环境创建子网。

    【讨论】:

    • 你能解释一下 for_each 在你的代码中是如何工作的吗? {for idx, val in var.env_subnets["env1"]: idx => val} 不确定我是否完全遵循示例中的逻辑。
    • @aseb 这是必需的,因为for_each 不能用于地图列表。因此 for 循环将其更改为地图。这是使用 for_each 时经常看到的常见模式。顺便说一句,如果答案有帮助,我们将不胜感激。
    猜你喜欢
    • 2019-07-01
    • 2021-09-25
    • 2019-04-25
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 2021-03-07
    • 1970-01-01
    • 2010-11-10
    相关资源
    最近更新 更多