hashicorp / terraform-provider-vsphere

Terraform Provider for VMware vSphere
https://registry.terraform.io/providers/hashicorp/vsphere/
Mozilla Public License 2.0
619 stars 452 forks source link

Add support for using two different SCSI buses in a `vsphere_virtual_machine` resource #1479

Open zhoutian77 opened 3 years ago

zhoutian77 commented 3 years ago

I want to create a virtual machine with two SCSI buses of different types image But I don't know how to write tf files, can you help me? My current configuration looks like this:

` .....other..configration..... variable "scsi_bus_sharing" { description = "scsi_bus_sharing mode, acceptable values physicalSharing,virtualSharing,noSharing." type = string default = "physicalSharing" }

variable "scsi_type" { description = "scsi_controller type, acceptable values lsilogic,pvscsi." type = string default = "" }

variable "data_disk" { description = "Storage data disk parameter, example" type = map(map(string)) default = { "rac1-rac2-sharedisk1-02"={ "data_disk_scsi_controller"= 1, "size_gb"= 10 }, "rac1-rac2-sharedisk2-02"={ "data_disk_scsi_controller"= 1, "size_gb"= 10 } } }

variable "disk_label" { description = "Storage data disk labels." type = list(any) default = [] }

variable "disk_size_gb" {
  description = "List of disk sizes to override template disk size."
  type        = list(any)
  default     = null

} resource "vsphere_virtual_machine" "vm1" { depends_on = [vsphere_virtual_disk.disk,] count = "1" name = "rac01-01" resource_pool_id = data.vsphere_resource_pool.pool.id datastore_id = var.disk_datastore != "" ? data.vsphere_datastore.disk_datastore[0].id : null wait_for_guest_net_timeout = 0 wait_for_guest_ip_timeout = 0

cpu_hot_add_enabled = true
num_cpus = 2
memory_hot_add_enabled = true
memory   = 4096
guest_id = data.vsphere_virtual_machine.template.guest_id

// scsi_bus_sharing       = var.scsi_bus_sharing
// scsi_type              = var.scsi_type != "" ? var.scsi_type : data.vsphere_virtual_machine.template.scsi_type
scsi_controller_count = max(
  max(0,flatten([[
    for item in values(var.data_disk) : [
      for elem, val in item :
      elem == "data_disk_scsi_controller" ? val : 0
    ]]])...) + 1,
  ceil((max(0,flatten([[
    for item in values(var.data_disk) : [
      for elem, val in item :
      elem == "unit_number" ? val : 0
    ]]])...) + 1) / 15),
  var.scsi_controller)

  dynamic "network_interface" {
      for_each = keys(var.network) 
      //#data.vsphere_network.network[*].id #other option
      content {
        network_id   = data.vsphere_network.network[network_interface.key].id
        adapter_type = var.network_type != null ? var.network_type[network_interface.key] : data.vsphere_virtual_machine.template.network_interface_types[0]
      }
    }

  //add
  // Disks defined in the original template
  dynamic "disk" {
      for_each = data.vsphere_virtual_machine.template.disks
      iterator = template_disks
      content {
      label             = length(var.disk_label) > 0 ? var.disk_label[template_disks.key] : "disk${template_disks.key}"
      size              = var.disk_size_gb != null ? var.disk_size_gb[template_disks.key] : data.vsphere_virtual_machine.template.disks[template_disks.key].size
      //unit_number       = var.scsi_controller != null ? var.scsi_controller * 15 + template_disks.key : template_disks.key
      unit_number       = template_disks.key
      thin_provisioned  = data.vsphere_virtual_machine.template.disks[template_disks.key].thin_provisioned
      eagerly_scrub     = data.vsphere_virtual_machine.template.disks[template_disks.key].eagerly_scrub
      datastore_id      = var.disk_datastore != "" ? data.vsphere_datastore.disk_datastore[0].id : null
      }
  }
  // Additional disks defined by Terraform config
  dynamic "disk" {
      for_each = var.data_disk
      iterator = terraform_disks
      content {
          label       = terraform_disks.key
          attach      = true
          datastore_id= var.disk_datastore != "" ? data.vsphere_datastore.disk_datastore[0].id : null
          path        = vsphere_virtual_disk.disk[terraform_disks.key].vmdk_path
          unit_number = (
              lookup(
              terraform_disks.value, 
              "unit_number", 
              -1
              ) < 0 ? (
              lookup(
                  terraform_disks.value, 
                  "data_disk_scsi_controller", 
                  0
              ) > 0 ? (
                  (terraform_disks.value.data_disk_scsi_controller * 15) +
                  index(keys(var.data_disk), terraform_disks.key) + 
                  (var.scsi_controller == tonumber(terraform_disks.value["data_disk_scsi_controller"]) ? local.template_disk_count : 0)
              ) : (
                  index(keys(var.data_disk), terraform_disks.key) + local.template_disk_count
              )
              ) : (
              tonumber(terraform_disks.value["unit_number"])
              )
          )
          disk_sharing      = "sharingMultiWriter"

          //   storage_policy_id = lookup(terraform_disks.value, "storage_policy_id", null)
          //   io_reservation    = lookup(terraform_disks.value, "io_reservation", null)
          //   io_share_level    = lookup(terraform_disks.value, "io_share_level", "normal")
          //   io_share_count    = lookup(terraform_disks.value, "io_share_level", null) == "custom" ? lookup(terraform_disks.value, "io_share_count") : null
          // 
      }
}
  //annotation
  //   disk {
  //     label = "disk0"
  //     size  = data.vsphere_virtual_machine.template.disks.0.size
  //   }

  //   disk{
  //     label = "disk1"
  //    size  = 200
  //    unit_number = 1
  //   }  

clone {
  #等待克隆过程完成的超时时间,缺省为30分钟
  timeout = 240
  template_uuid = data.vsphere_virtual_machine.template.id

  customize {
    #自定义配置等待超时时间,缺省为10分钟
    timeout = 120
    linux_options {
      host_name = "rac1"
      domain    = ""
    }

  //   network_interface {
  //     ipv4_address = "@{global$.ip}"
  //     ipv4_netmask = "@{global$.netmask}"
  //   }
  dynamic "network_interface" {
      for_each = keys(var.network)
      content {
        ipv4_address = split("/", var.network[keys(var.network)[network_interface.key]][count.index])[0]
        ipv4_netmask = var.network[keys(var.network)[network_interface.key]][count.index] == "" ? null : (
                         length(split("/", var.network[keys(var.network)[network_interface.key]][count.index])) == 2 ? (
                           split("/", var.network[keys(var.network)[network_interface.key]][count.index])[1]
                         ) : (
                           length(var.ipv4submask) == 1 ? var.ipv4submask[0] : var.ipv4submask[network_interface.key]
                         )
                       )
      }
    }

    ipv4_gateway = "@{global$.gateway}"
  }
}

}

.....other..configration..... `

The 'scsi_bus_sharing' argument seems to specify the shared type of all controllers directly. Can I set different share types on different controllers?

thank you

ljluestc commented 1 year ago
dynamic "disk" {
  for_each = var.data_disk
  iterator = terraform_disks
  content {
    label              = terraform_disks.key
    attach             = true
    datastore_id       = var.disk_datastore != "" ? data.vsphere_datastore.disk_datastore[0].id : null
    path               = vsphere_virtual_disk.disk[terraform_disks.key].vmdk_path
    unit_number        = (
      lookup(
        terraform_disks.value,
        "unit_number",
        -1
      ) < 0 ? (
        lookup(
          terraform_disks.value,
          "data_disk_scsi_controller",
          0
        ) > 0 ? (
          (terraform_disks.value.data_disk_scsi_controller * 15) +
          index(keys(var.data_disk), terraform_disks.key) +
          (var.scsi_controller == tonumber(terraform_disks.value["data_disk_scsi_controller"]) ? local.template_disk_count : 0)
        ) : (
          index(keys(var.data_disk), terraform_disks.key) + local.template_disk_count
        )
      ) : (
        tonumber(terraform_disks.value["unit_number"])
      )
    )
    disk_sharing       = lookup(terraform_disks.value, "scsi_bus_sharing", "physicalSharing")  # Use scsi_bus_sharing value or default to "physicalSharing"

    # ...
  }
}