地形多个for_each资源

时间:2020-07-31 03:22:03

标签: terraform vmware vsphere

我想在多个虚拟机中制作多个磁盘

我当前的问题是添加磁盘时会创建新的VM。

如何修改for_each或for?

我的脚本如下(main.tf,variable.tf,resource.tfvars

当我创建2个虚拟机时,我想制作2个磁盘,但是,目前在4个虚拟机中创建了一个磁盘。

如何为每个VM创建不同的磁盘?

我做错了吗?

请告诉我是否还有其他方法。

vm.tf

#data source
data "vsphere_datacenter" "dc" {
  name = var.dc
}

data "vsphere_datastore" "datastore" {
  name = var.datastore
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

data "vsphere_compute_cluster" "cluster" {
  name = var.cluster
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

data "vsphere_network" "network1" {
  name = var.network1
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

data "vsphere_network" "network2" {
  name = var.network2
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

data "vsphere_virtual_machine" "template" {
  name = var.template
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

resource vsphere_virtual_machine "allvms" {
for_each = var.vms
  resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
  datastore_id     = data.vsphere_datastore.datastore.id
  guest_id = data.vsphere_virtual_machine.template.guest_id
  
  name = each.value.vmname
  folder = each.value.vfolder
  memory = each.value.vmemory
  num_cpus = each.value.vcpu
  annotation = each.value.vannotation
  firmware = each.value.vfirmware
  scsi_type = data.vsphere_virtual_machine.template.scsi_type
  
  network_interface {
    network_id   = data.vsphere_network.network1.id
    adapter_type = "vmxnet3"
  }
  
  network_interface {
    network_id   = data.vsphere_network.network2.id
    adapter_type = "vmxnet3"
  }
  
  disk {
    label            = "disk0"
    size             = data.vsphere_virtual_machine.template.disks.0.size
    eagerly_scrub    = data.vsphere_virtual_machine.template.disks.0.eagerly_scrub
    thin_provisioned = data.vsphere_virtual_machine.template.disks.0.thin_provisioned
  }
  
dynamic "disk" {
    for_each = flatten([ for k, v in var.vms : [
                 for i in v.vdisk : {
                   vdisklabel = i.vdisklabel
                   vdisksize  = i.vdisksize
                   vdisknumber = i.vdisknumber
    }]])
    content {
      label =   disk.value.vdisklabel
      size  =   disk.value.vdisksize
      unit_number = disk.value.vdisknumber
        }
}

  clone {
    template_uuid = data.vsphere_virtual_machine.template.id
    customize {
      linux_options {
        host_name = each.value.vmname
        domain = "test.internal"
      }
      network_interface {
        ipv4_address = each.value.vip1
        ipv4_netmask = each.value.vnetmask1
      }
      network_interface {
        ipv4_address = each.value.vip2
        ipv4_netmask = each.value.vnetmask2
      }
      ipv4_gateway = each.value.vgateway
            } 
        }
    }

variable.tf

variable "dc" {
}

variable "datastore" {
}

variable "cluster" {
}

variable "network1" {
}

variable "network2" {
}

variable "template" {
}

variable "vms" {
    type = map(object({
        vmname = string
        vfolder = string
        vcpu = number
        vmemory = number
        vfirmware = string
        vannotation = string
        vip1 = string
        vnetmask1 = number
        vip2 = string
        vnetmask2 = number
        vgateway =  string
        vdisk = list(object({
            vdisklabel = string
            vdisksize = number 
            vdisknumber =  number
        }))
    }))
}

main.tf

module "resource" {
source = "../Terraform"
dc  =   "test-datacenter"
datastore   = "hdd-08"
cluster = "4th_Cluster"
network1 = "192.168.200.xxx"
network2 = "192.168.201.xxx"
template = "centos-78-tmp"

vms = {
    test1 = {
        vmname = "test-01"
        vfolder = "0.test"
        vcpu = "4"
        vmemory = "4096"
        vfirmware = "bios"
        vannotation = "testvm"
        vip1 = "192.168.200.100"
        vnetmask1 = "24"
        vip2 = "192.168.201.101"
        vnetmask2 = "24"
        vgateway = "192.168.200.1"
        vdisk = [
                {
                vdisklabel = "disk1" 
                vdisksize = "100" 
                vdisknumber = "1"
                },
                {
                vdisklabel = "disk2" 
                vdisksize = "10" 
                vdisknumber = "2"
                }
                ]
    }
   test2 = {
        vmname = "test-02"
        vfolder = "0.test"
        vcpu = "4"
        vmemory = "4096"
        vfirmware = "bios"
        vannotation = "testvm"
        vip1 = "192.168.200.102"
        vnetmask1 = "24"
        vip2 = "192.168.201.102"
        vnetmask2 = "24"
        vgateway = "192.168.200.1"
        vdisk = [
                {
                vdisklabel = "disk1" 
                vdisksize = "100" 
                vdisknumber = "1"
                },
                {
                vdisklabel = "disk2" 
                vdisksize = "10" 
                vdisknumber = "2"
                }
                ]
        }
    }
}

0 个答案:

没有答案