2016-11-11 175 views
0

我在Azure中創建多個虛擬機與Terraform過程中面臨的問題創造天藍multile虛擬機。 每次都失敗,因爲它選擇相同的網絡接口ID。那麼,如何更改我的Terraform代碼以使其使用不同的網絡接口?需要通過terraform

這裏是我的Terraform文件:

variable "node_count" {default = 2} 

resource "azurerm_network_interface" "terraform-CnetFace" { 
    name = "cacctni-${format("%02d", count.index+1)}" 
    location = "East US 2" 
    resource_group_name = "${azurerm_resource_group.terraform-test.name}" 

    ip_configuration { 
     name = "cIpconfig-${format("%02d", count.index+1)}" 
     subnet_id = "${azurerm_subnet.terraform-test.id}" 
     private_ip_address_allocation = "dynamic" 
    } 
    count = "${var.node_count}" 
} 

variable "confignode_count" {default = 2} 

resource "azurerm_virtual_machine" "terraform-test" { 
    name = "confignode-${format("%02d", count.index+1)}" 
    location = "East US 2" 
    resource_group_name = "${azurerm_resource_group.terraform-test.name}" 
    network_interface_ids = ["${azurerm_network_interface.terraform-CnetFace.id}"] 
    vm_size = "Standard_A0" 
    availability_set_id = "${azurerm_availability_set.terraform-test.id}" 

    storage_image_reference { 
     publisher = "Canonical" 
     offer = "UbuntuServer" 
     sku = "14.04.2-LTS" 
     version = "latest" 
    } 

    storage_os_disk { 
     name = "configosdisk-${format("%02d", count.index+1)}" 
     vhd_uri = "${azurerm_storage_account.terraform-test.primary_blob_endpoint}${azurerm_storage_container.terraform-test.name}/configosdisk-${format("%02d", count.index+1)}.vhd" 
     caching = "ReadWrite" 
     create_option = "FromImage" 
    } 

    storage_data_disk { 
    name   = "configdatadisk-${format("%02d", count.index+1)}" 
    vhd_uri  = "${azurerm_storage_account.terraform-test.primary_blob_endpoint}${azurerm_storage_container.terraform-test.name}/configdatadisk-${format("%02d", count.index+1)}.vhd" 
    disk_size_gb = "512" 
    create_option = "empty" 
    lun   = 0 
    } 

    os_profile { 
     computer_name = "confignode-${format("%02d", count.index+1)}" 
     admin_username = "ubuntu" 
     admin_password = "Qawzsx12345" 
    } 

    os_profile_linux_config { 
     disable_password_authentication = false 
    } 

    tags { 
     environment = "Production" 
    } 
    provisioner "local-exec" { 
     command = "sleep 30" 
    } 

    #Loop for Count 
    count = "${var.confignode_count}" 
} 

回答

2

如果你想兩個資源,你遍歷他們,那麼你需要使用「提示圖標」來檢索的迴路形成的資源列表和鏈接選擇正確的一個。這在interpolation syntax docsresources docs中有簡要的解釋。

你的情況,你可能想是這樣的:

variable "count" {default = 2} 

resource "azurerm_network_interface" "terraform-CnetFace" { 
    count = "${var.count}" 
    ... 
} 

resource "azurerm_virtual_machine" "terraform-test" { 
    count = "${var.count}" 
    ... 
    network_interface_ids = ["${element(azurerm_network_interface.terraform-CnetFace.*.id, count.index)}"] 
    ... 
} 

這暴露出輸出爲每個創建的環狀管網接口,然後通過他們從輸出抓住id並將它傳遞給適當的azurerm_virtual_machine循環。

+0

感謝ydaetskcoR。!這對我有效。 – 10305059