Skip to content

Commit

Permalink
use template disk config (#11)
Browse files Browse the repository at this point in the history
* use template disk config

* terraform fmt

* add time
  • Loading branch information
DonBower authored Oct 7, 2023
1 parent 8b4c527 commit b5f3657
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Release History

## 1.2.1

> Release Date: 2023-10-07
:bug: **Bugfix**:

- Correct Orginal Disk Assignment to use template values

:sweat_drops: **Chore**:

- Add some VM Notes (annotation).

## 1.2.0

> Release Date: 2023-09-30
Expand Down
8 changes: 7 additions & 1 deletion locals.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
locals {
build_with = "HashiCorp Terraform 1.4.6"
build_date = formatdate("YYYY-MM-DD", timestamp())
build_time = formatdate("hh:mm ZZZ", timestamp())
version_text = file("${path.module}/version.txt")
vsphereResourceName = var.vsphereResourceName != null ? format("%s%s%s", data.vsphere_compute_cluster.cluster.name, "/Resources/", var.vsphereResourceName) : format("%s%s", data.vsphere_compute_cluster.cluster.name, "/Resources")
thisVM = {
vmHostName = var.vmHostName != null ? var.vmHostName : var.vsphereName
vmFolder = var.vsphereFolderName != null ? var.vsphereFolderName : "/${var.vsphereDatacenterName}/vm/test-vms"
vmTemplateID = var.vsphereContentLibrary != null ? data.vsphere_content_library_item.template[0].id : data.vsphere_virtual_machine.template[0].id
vmResourcePoolId = data.vsphere_resource_pool.default.id
vmDatastoreId = data.vsphere_datastore.datastore.id
vmTemplateID = var.vsphereContentLibrary != null ? data.vsphere_content_library_item.template[0].id : data.vsphere_virtual_machine.template[0].id
vmAnnotation = "Version: ${local.version_text}\nBuilt on: ${local.build_date} ${local.build_date}\nBuilt with: ${local.build_with}\nBuilt from: ${var.vsphereTemplateName}"

network = {
staticMAC = var.vmMacAddress != null ? true : false
macAddress = var.vmMacAddress # "00:50:56:00:02:14" 00:50:56:00 is prefix, 02:14 is ip 192.168.2.20
Expand Down
16 changes: 10 additions & 6 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
resource "vsphere_virtual_machine" "thisUbuntu" {
name = var.vsphereName
annotation = local.thisVM.vmAnnotation
resource_pool_id = local.thisVM.vmResourcePoolId
datastore_id = local.thisVM.vmDatastoreId
num_cpus = var.vmCPUs
Expand Down Expand Up @@ -28,12 +29,15 @@ resource "vsphere_virtual_machine" "thisUbuntu" {
network_id = local.thisVM.network.vsphereNetwork
}

disk {
label = local.thisVM.disk.label
size = local.thisVM.disk.size
thin_provisioned = local.thisVM.disk.thinProvision
eagerly_scrub = false
unit_number = local.thisVM.disk.unitNumber
dynamic "disk" {
for_each = data.vsphere_virtual_machine.template[0].disks
content {
label = disk.value.label
size = disk.value.size
thin_provisioned = disk.value.thin_provisioned
eagerly_scrub = disk.value.eagerly_scrub
unit_number = disk.value.unit_number
}
}

dynamic "disk" {
Expand Down
4 changes: 2 additions & 2 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ output "thisDatastore" {

output "thisTemplate" {
sensitive = false
value = data.vsphere_content_library_item.template
}
value = data.vsphere_virtual_machine.template
}
10 changes: 5 additions & 5 deletions test/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module "thisUbuntu" {
}
vmCPUs = 4
vmRAM = 16384
vmDiskSize = 16
vsphereName = "ubuntu-2204"
vmHostName = "ubuntu-2204"
vmDiskSize = 64
vsphereName = "ubuntu-2204-test"
vmHostName = "ubuntu-2204-test"
vmHostDomain = var.vmHostDomain
vmRole = "Test"
vmAdditionalDisks = {
Expand All @@ -23,7 +23,7 @@ module "thisUbuntu" {
vsphereFolderName = "test-vms"
vsphereDatastoreName = var.vsphereDatastoreName
vsphereContentLibrary = null # var.vsphereContentLibrary
vsphereTemplateName = "ubuntu-22.04-latest"
vsphereTemplateName = "ubuntu-22.04-0.2.2-rc"
vsphereResourceName = null
}

Expand All @@ -49,4 +49,4 @@ output "thisUbuntuFolder" {
output "thisTemplate" {
sensitive = false
value = module.thisUbuntu.thisTemplate
}
}
1 change: 1 addition & 0 deletions version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.2.1

0 comments on commit b5f3657

Please sign in to comment.