Как назначить случайный subnet_id в azurerm_network_interface? - PullRequest
1 голос
/ 23 января 2020
### variable
variable "vm-subnets" { 
  type = list(string) 
  default = ["7.0.1.0/24","7.0.2.0/24","7.0.3.0/24"] 
}

### subnet
resource "azurerm_subnet" "task_subnet" {
  name                 = "subnet-${format("%02d",count.index)}"
  resource_group_name  = azurerm_resource_group.task.name
  virtual_network_name = azurerm_virtual_network.task_vnet.name
  network_security_group_id = azurerm_network_security_group.task_nsg.id
  address_prefix       = var.vm-subnets[count.index]
  count                 = length(var.vm-subnets)
}

### NIC
resource "azurerm_network_interface" "vm_nic" {
  name                = "nic--${format("%02d",count.index)}"
  location            = var.region
  resource_group_name = azurerm_resource_group.task.name
  count               = var.vm-count

  ip_configuration {
    name                          = "${var.resource_prefix}-${format("%02d",count.index)}-ip"
    subnet_id                     = azurerm_subnet.task_subnet.*.id[count.index]
    private_ip_address_allocation = "dynamic"
    public_ip_address_id          = azurerm_public_ip.task_public_ip.*.id[count.index]
  }<br>
}

Мне нужно 7 ВМ в 3 su bnet для таких как su bnet -A = 2 В, su bnet -B = 2 В, su bnet - C = 3 В или случайным образом `ОШИБКА: Ошибка: недопустимый индекс

в строке 11 vm-network.tf, в ресурсе" azurerm_network_interface "" vm_ni c ": 11: subnet_id = azurerm_su bnet .task_su bnet. *. id [count.index] | ---------------- | azurerm_su bnet .task_su bnet кортеж с 3 элементами | count.index равно 4

Данный ключ не идентифицирует элемент в этом значении коллекции.

Ошибка: недопустимый индекс

в строке 11 vm-network.tf, в ресурс "azurerm_network_interface" "vm_ni c": 11: subnet_id = azurerm_su bnet .task_su bnet. *. id [count.index] | ---------------- | azurerm_su bnet .task_su bnet кортеж с 3 элементами | индекс индекса 3


The given key does not identify an element in this collection value.



What modification can be done to resolve it and how can I assign different/random subnet on each vm rather then count loop.

I also try to do it using random_shuffle and set-product function but not get the desired output .. please Help 

1 Ответ

0 голосов
/ 25 января 2020

После 2 дней логической борьбы я наконец смог найти решение или вопросы, которые я создаю: ИСПОЛЬЗУЯ функцию элемента https://www.terraform.io/docs/configuration/functions/element.html

NI C

resource "azurerm_network_interface" "vm_nic" {
  name                = "nic--${format("%02d",count.index)}"
  location            = var.region
  resource_group_name = azurerm_resource_group.task.name
  count               = var.vm-count
  ip_configuration {
    name                          = "${var.resource_prefix}-${format("%02d",count.index)}-ip"
    subnet_id                     =  element(azurerm_subnet.task_subnet.*.id, count.index)
    private_ip_address_allocation = "dynamic"
    public_ip_address_id          = azurerm_public_ip.task_public_ip.*.id[count.index]
  }
} 
subnet_id =  element(azurerm_subnet.task_subnet.*.id, count.index)

с использованием функции элемента ["7.0.1.0/24","7.0.2.0/24","7.0.3.0/24"] в vm count.index = 3 вернуться к su bnet index = 0, чтобы узнать, как он работает, и проверить его на vm.count = 5, 7, 10. ✌?

...