需要通过 terraform 在 azure 中创建多个虚拟机
Need to create multile vms in azure through terraform
我在使用 Terraform 在 Azure 中创建多个 VM 时遇到问题。
每次失败都是因为它选择了相同的网络接口 ID。那么,如何更改我的 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}"
}
如果你想 link 循环遍历两个资源,那么你需要使用“splats”来检索在循环中创建的资源列表和 select 正确的资源. interpolation syntax docs and the resources 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
。
我在使用 Terraform 在 Azure 中创建多个 VM 时遇到问题。 每次失败都是因为它选择了相同的网络接口 ID。那么,如何更改我的 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}"
}
如果你想 link 循环遍历两个资源,那么你需要使用“splats”来检索在循环中创建的资源列表和 select 正确的资源. interpolation syntax docs and the resources 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
。