问题描述
TF版本:0.13.4
TF version: 0.13.4
提供程序版本:2.40.0
Provider version: 2.40.0
我不确定执行此操作的最佳/最有效方法,但是我想实现的目标是为每个VM附加2个磁盘(目前),但是我需要能够配置每个VM和磁盘按要求.将来我可能需要更改磁盘和VM的数量.我试图让for_each遍历大小,并根据其中的值创建多少磁盘,以便我可以拥有多个大小不同的磁盘.
I'm not sure on the best/most efficient way to do this, but what I'm trying to achieve is to attach 2 disks (for now) to each VM, but I need to be able to configure each VM and disk as per requirements. I may need to vary the number of disks and VM's in the future. I'm trying to get the for_each to loop over the size and based on how many values are in there, create that amount of disks so that I can have multiple disks with different sizes.
有没有一种方法可以不必手动创建多个托管磁盘资源?
Is there a way to do this without having to create multiple managed disk resources manually?
我当前的代码创建了数量不等的VM,但每个VM仅附加一个磁盘.for_each迭代的变量是在每个环境的tfvars文件中设置的:
My current code creates varying number of VM's but only attaches one disk per VM. The variables that the for_each iterates over are set in a tfvars file per environment:
desktop_servers = {
"Server_1" = {
name = 1,
zone = 1,
lun = 1,
size = 32
}
"Server_2" = {
name = 2,
zone = 2,
lun = 2,
size = 32
}
"Server_3" = {
name = 3,
zone = 3,
lun = 3,
size = 32
}
}
etl_servers = {
"Server_1" = {
name = 1,
zone = 1,
lun = 1,
size = 32
}
"Server_2" = {
name = 2,
zone = 2,
lun = 2,
size = 32
}
}
module.tf中的虚拟机资源
virtual machine resource within the module.tf
# Azure Virtual Machine
resource "azurerm_windows_virtual_machine" "virtual_machine" {
for_each = var.servers
name = "vm-${var.environment}-${var.vm_identifier}${each.value.name}"
location = var.location
resource_group_name = var.resource_group
zone = each.value.zone
size = var.vm_size
network_interface_ids = [azurerm_network_interface.network_interface[each.key].id]
computer_name = "${var.vm_identifier}${each.value.name}"
admin_username = xxxx
admin_password = xxxx
provision_vm_agent = "true"
source_image_id = data.azurerm_shared_image.dwp_shared_image.id
boot_diagnostics {
storage_account_uri = data.azurerm_storage_account.dwp_diag_storage_account.primary_blob_endpoint
}
os_disk {
name = "vm-${var.environment}-${var.directorate}-${var.business_unit}-${var.vm_identifier}-os${each.value.name}"
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
}
depends_on = [azurerm_network_interface.network_interface]
}
module.tf中的托管磁盘资源:
managed disk resource within module.tf :
# #Managed disks per vm
resource "azurerm_managed_disk" "managed_disk" {
for_each = var.servers
name = "disk-${var.environment}-${var.vm_identifier}${each.value.name}"
location = var.location
resource_group_name = var.resource_group
storage_account_type = "Premium_LRS"
create_option = "Empty"
disk_size_gb = each.value.size
zones = [azurerm_windows_virtual_machine.virtual_machine[each.key].zone]
}
resource "azurerm_virtual_machine_data_disk_attachment" "disk_attachment" {
for_each = var.servers
managed_disk_id = azurerm_managed_disk.managed_disk[each.key].id
virtual_machine_id = azurerm_windows_virtual_machine.virtual_machine[each.key].id
lun = each.value.lun
caching = "ReadWrite"
}
模块中的
variable.tf:
variable.tf within the module:
variable "servers" {
description = "Variable for defining each instance"
}
main.tf中用于ETL服务器的模块示例.:
Example of a module for the ETL servers in the main.tf. :
module "etl_vm" {
source = "../modules/compute/windows_vm"
location = var.location
resource_group = azurerm_resource_group.rg_sbox_etl.name
directorate = var.directorate
business_unit = var.business_unit
environment = var.environment
network_rg_identifier = var.network_rg_identifier
subnet_name = "sub-${var.environment}-${var.directorate}-${var.business_unit}-be01"
diag_storage_account_name = var.diag_storage_account_name
log_analytics_workspace_name = var.log_analytics_workspace_name
backup_policy_name = var.backup_policy_name
vm_identifier = "${var.vm_identifier}${var.instance_number}-etl"
servers = var.etl_servers
vm_size = var.etl_vm_size
enable_management_locks = true
image_name = "WIN2016-CISL2"
gallery_subscription_id = var.sub_id
gallery_resourcegroup = var.rg_gallery
gallery_name = "SBOXGallery"
推荐答案
您可以使用该模块来实现它.但这有点复杂.这个想法是您使用模块来配置VM,在每个VM中,您可以配置不同的数据磁盘并将它们附加到VM.使用该变量输入不同的配置虚拟机编号.这是一个示例:
You can use the module to achieve it. But it's a little complex. The idea is that you use the module to configure the VM, in each VM, you can configure different data disks and attach them to the VM. Use the variable to input different VM number of the configuration. Here is an example:
main.tf
variable "rg_name" {
type = string
}
variable "vms" {
type = map(object({
size = string
admin_user = string
admin_password = string
disks = list(number)
}))
}
variable "location" {}
module "vms" {
for_each = var.vms
source = "./modules/vm"
resource_group_name = var.rg_name
vm_name = each.key
vm = each.value
disks = each.value["disks"]
location = var.location
}
terraform.tfvar
terraform.tfvar
rg_name = "charlesVMs"
location = "East US"
vms ={
azurevm1 = {
size = "Standard_DS1_v2"
admin_user = "azureuser"
admin_password = "azureuser@2021"
disks = [30, 30]
}
}
./modules/vm/main.tf
./modules/vm/main.tf
variable "resource_group_name" {}
variable "vm" {}
variable "disks" {}
variable "location" {}
variable "vm_name" {}
resource "azurerm_resource_group" "example" {
name = var.resource_group_name
location = var.location
}
resource "azurerm_virtual_network" "example" {
name = "example-network"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}
resource "azurerm_subnet" "example" {
name = "internal"
resource_group_name = azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.example.name
address_prefixes = ["10.0.2.0/24"]
}
resource "azurerm_network_interface" "example" {
name = "example-nic"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.example.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_linux_virtual_machine" "vm" {
name = var.vm_name
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
size = var.vm["size"]
disable_password_authentication = false
admin_username = var.vm["admin_user"]
admin_password = var.vm["admin_password"]
network_interface_ids = [
azurerm_network_interface.example.id
]
# admin_ssh_key {
# username = "adminuser"
# public_key = file("~/.ssh/id_rsa.pub")
# }
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
}
resource "azurerm_managed_disk" "example" {
count = length(var.vm["disks"])
name = "datadisk-${count.index}"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
storage_account_type = "Standard_LRS"
create_option = "Empty"
disk_size_gb = element(var.vm["disks"], count.index)
}
resource "azurerm_virtual_machine_data_disk_attachment" "example" {
count = length(var.vm["disks"])
managed_disk_id = element(azurerm_managed_disk.example.*.id, count.index)
virtual_machine_id = azurerm_linux_virtual_machine.vm.id
lun = count.index
caching = "ReadWrite"
}
您只需要为 vms
变量提供不同数量的VM配置和不同数量的磁盘.
You only need to give the varying number of VM's configuration and the varying number of disks with sizes for the vms
variable.
这篇关于使用for_each将不同数量的磁盘连接到不同数量的VM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!