需要通过Terraform在Azure中创建多虚拟机 [英] Need to create multile vms in azure through terraform

查看:279
本文介绍了需要通过Terraform在Azure中创建多虚拟机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Azure中使用Terraform创建多个VM时遇到问题. 每次由于选择相同的网络接口ID而失败.那么如何更改我的Terraform代码以使其使用不同的网络接口?

I'm facing issues during creation of multiple VMs in Azure with Terraform. Each time it is failing because it is picking the same network-interface-id. So how can I change my Terraform code to make this use the different network interfaces?

这是我的Terraform文件:

Here is my Terraform file:

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}"
}

推荐答案

如果要在链接两个资源时尝试链接它们,则需要使用"splats"来检索在循环中创建的资源列表,然后选择正确的一个. 插值语法文档资源文档.

If you're trying to link two resources as you loop through them then you need to use "splats" to retrieve the list of resources created in a loop and select the right one. This is explained briefly in the interpolation syntax docs and the resources docs.

在您的情况下,您可能想要以下内容:

In your case you probably want something like:

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.

This exposes the outputs for each of the looped network interfaces created and then loops through them grabbing the id from the output and passing it to the appropriate azurerm_virtual_machine.

这篇关于需要通过Terraform在Azure中创建多虚拟机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆