![](/img/trans.png)
[英]Unable to attach Public IP to Azure windows VM (created using for_each) in Terraform
[英]Azure VM Creation using For_Each loop via Terraform
我正在嘗試通過 terrform 使用 for_each 創建多個 Azure VM,我能夠創建 2 個 NIC 卡,但是在 zurerm_windows_virtual_machine 塊中定義 NIC id 時,兩個 VM 都選擇相同的 NIC 卡(最后一個,索引 1),結果只有 VM被創建和其他失敗。 (network_interface_ids = azurerm_network_interface.az_nic[*].id) 的邏輯是什么,第一個虛擬機將選擇第一個網卡,第二個這樣做。
#--------------為 Windows 虛擬機創建網絡接口--------------
resource "azurerm_network_interface" "az_nic" {
count = length(var.vm_names)
name = "${var.vm_names[count.index]}_nic"
location = var.location
resource_group_name = data.azurerm_resource_group.Resource_group.name
ip_configuration {
name = var.vm_names[count.index]
subnet_id = data.azurerm_subnet.subnet.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_windows_virtual_machine" "myvm" {
for_each = toset(var.vm_names)
name = each.value
resource_group_name = data.azurerm_resource_group.Resource_group.name
location = var.location
size = "Standard_D2s_v3"
admin_username = "abc"
admin_password = "uejehrikch123"
network_interface_ids = azurerm_network_interface.az_nic[*].id
source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2016-Datacenter"
version = "latest"
}
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
您可以在resource "azurerm_windows_virtual_machine"
添加count
參數,而不是混合count
和for_each
。
假設你有
variable "vm_names" {
default = ["vm1", "vm2"]
}
然后你可以像這樣更改資源.tf
文件:
resource "azurerm_windows_virtual_machine" "myvm" {
count = length(var.vm_names)
name = element(var.vm_names,count.index)
resource_group_name = data.azurerm_resource_group.Resource_group.name
location = var.location
size = "Standard_D2s_v3"
admin_username = "abc"
admin_password = "uejehrikch123"
network_interface_ids = [element(azurerm_network_interface.az_nic.*.id, count.index)]
source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2016-Datacenter"
version = "latest"
}
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.