下面是我为 Azure VM 提供的模板。
据我所知,在 Google 云中,我们可以选择设置创建多台机器的数量。
如何使用单个模板创建多台机器,以便根据变量值创建大量机器。
Azure Windows 服务器 VM 的示例模板。
github网址:关联
我想让这个 repo 永久公开,所以不在这里发布直接文件。
答案1
实现此目的的一种方法是将属性声明为变量,并将它们用作资源定义中 for_each 的参数。
答案2
这是一个粗略的工作示例,说明如何使用元参数“count” azurerm_windows_virtual_machine
:
provider "azurerm" {
features {}
}
resource "random_string" "username" { length = 8 }
resource "random_password" "password" { length = 24 }
resource "azurerm_resource_group" "rg" {
name = "count-test-win"
location = "northeurope"
}
# Set the count of virtual machines you want
variable "vm_count" {
default = 4
}
resource "azurerm_virtual_network" "test" {
name = "test-network"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}
resource "azurerm_subnet" "test" {
name = "internal"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefixes = ["10.0.2.0/24"]
}
resource "azurerm_network_interface" "nic" {
count = var.vm_count
name = "nic-${count.index}"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.test.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_windows_virtual_machine" "vm" {
count = var.vm_count
name = "win-vm-${count.index}"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
size = "Standard_F2"
admin_username = random_string.username.result
admin_password = random_password.password.result
network_interface_ids = [azurerm_network_interface.nic[count.index].id]
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2016-Datacenter"
version = "latest"
}
}
不过我建议您考虑编写自己的模块或考虑使用虚拟机规模集并确定它是否更适合您的用例https://docs.microsoft.com/en-us/azure/virtual-machine-scale-sets/overview
Windows 版本的相关 azurerm 模块文档可以在以下位置找到:https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/windows_virtual_machine_scale_set