无法在azurerm_network_interface public_ip_address_id上设置值

1l5u6lss  于 2023-10-22  发布在  其他
关注(0)|答案(2)|浏览(103)

我是Azure Terraform的新手,在azurerm_network_interface资源的这一部分上正确设置public_ip_address_id时遇到问题。我可以征求意见。谢谢你,谢谢
我正在创建8个接口,并尝试在前3个接口上绑定公共IP。我尝试使用try函数在前3个接口上返回并设置public_ip_id,并在其余接口上设置null

resource "azurerm_network_interface" "virtual_network_interfaces" {
  
  for_each = var.interfaces

   location            = var.location
   resource_group_name = var.resource_group_name 

   name = "${var.device_name}_${var.instance_type}_${var.site_name}_${each.value.name}_Interface"    
   enable_ip_forwarding = true
   enable_accelerated_networking = true
   
   ip_configuration {
      name                          = "${var.device_name}_${var.instance_type}_${var.site_name}_${each.value.name}_IP_Config"
      private_ip_address            = each.value.address_prefixes[0]
      private_ip_address_allocation = "Static"  

      # set Management interface as primary vNIC
      primary = each.value.name == "Management" ? true : false

      subnet_id = try(
         lookup(
            { for k, v in var.subnets : v.name => v.id },
               each.value.name, null
         ), null
       )

       public_ip_address_id = try(
         {
            for k1, v1 in var.public_ips :
              k1 => can(regex("${each.value.name}", v1.name)) ? v1.id : null
          },
           null)
     }
}
x7yiwoj4

x7yiwoj41#

我尝试在azurerm_network_interface public_ip_address_id上设置值,并且能够成功配置需求。
当前用于设置public_ip_address_id的逻辑是在var.public_ips上进行配置,并针对每个公共IP检查当前NIC的名称(来自var.interfaces)是否与公共IP的名称匹配。如果是,则将public_ip_address_id设置为该公网IP的ID,否则将其设置为null
这种方法的问题是,逻辑将始终返回最后匹配的公共IP的ID(如果没有找到匹配,则返回null)。这并不理想,因为如果多个公共IP与NIC的名称匹配,则只会设置最后一个。
此外,can(regex())函数在这里的使用并不理想。如果您尝试将前3个接口绑定到公共IP,建议使用更确定性的方法。
我试图实现您的要求使用我赢得了配置模块的变化,其中提到如下。

我的地形配置:

main.tf:

provider "azurerm" {
    features {}
}

resource "azurerm_resource_group" "example" {
  name     = var.resource_group_name
  location = var.location
}

resource "azurerm_virtual_network" "example" {
  name                = "vksbl-vnet"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  address_space       = ["10.0.0.0/16"]
}

resource "azurerm_subnet" "example" {
  name                 = "vksbl-subnet"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.1.0/24"]
}

resource "azurerm_public_ip" "example" {
  count               = 3
  name                = "vksbl-public-ip-${count.index}"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  allocation_method   = "Dynamic"
}

resource "azurerm_network_interface" "virtual_network_interfaces" {
  count = length(var.interfaces)

  location            = var.location
  resource_group_name = azurerm_resource_group.example.name
  
  name                = "${var.device_name}_${var.instance_type}_${var.site_name}_${var.interfaces[count.index].name}_Interface"
  enable_ip_forwarding          = true
  enable_accelerated_networking = true
   
  ip_configuration {
    name                          = "${var.device_name}_${var.instance_type}_${var.site_name}_${var.interfaces[count.index].name}_IP_Config"
    private_ip_address            = var.interfaces[count.index].address_prefixes[0]
    private_ip_address_allocation = "Static"
    primary                       = var.interfaces[count.index].name == "Management" ? true : false

    subnet_id = azurerm_subnet.example.id

    # Bind the first 3 interfaces to public IPs
    public_ip_address_id = count.index < 3 ? azurerm_public_ip.example[count.index].id : null
  }
}

variable.tf:

variable "interfaces" {
  description = "List of interfaces"
  type        = list(object({
    name            = string
    address_prefixes = list(string)
  }))
  default = [
    {
      name            = "Interface1"
      address_prefixes = ["10.0.1.4"]
    },
    {
      name            = "Interface2"
      address_prefixes = ["10.0.1.5"]
    },
    {
      name            = "Interface3"
      address_prefixes = ["10.0.1.6"]
    },
    {
      name            = "Interface4"
      address_prefixes = ["10.0.1.7"]
    },
    {
      name            = "Interface5"
      address_prefixes = ["10.0.1.8"]
    },
    {
      name            = "Interface6"
      address_prefixes = ["10.0.1.9"]
    },
    {
      name            = "Interface7"
      address_prefixes = ["10.0.1.10"]
    },
    {
      name            = "Interface8"
      address_prefixes = ["10.0.1.11"]
    }
  ]
}

variable "location" {
  description = "Azure region/location"
  default     = "East US"
}

variable "resource_group_name" {
  description = "Name of the resource group"
  default     = "demorgvk"
}

variable "device_name" {
  description = "Device name"
  default     = "my-device"
}

variable "instance_type" {
  description = "Instance type"
  default     = "t2.micro"
}

variable "site_name" {
  description = "Site name"
  default     = "my-site"
}

输出:

此配置将创建一个虚拟网络、一个子网、3个公共IP和8个网络接口。这些接口中的前3个将绑定到公共IP,而其余的将没有公共IP。

xbp102n0

xbp102n02#

我怀疑try代码块返回map对象,为了证明这个理论,我做了以下更改。看起来try块返回了一个map对象。任何修改/增强此代码块的建议。谢谢!2谢谢!

public_ip_address_id = tostring( try(
     {
         for k1, v1 in var.public_ips :
            k1 => can(regex("${each.value.name}", v1.name)) ?
            v1.id : tostring("")
      },
      tostring("")))

相关问题