Skip to content

Commit 2ca02d0

Browse files
author
Arman Keyoumarsi
committed
Merge branch 'sestegra-linux_win_merge' into rc/3.0.0
2 parents 14d0536 + 6937d4e commit 2ca02d0

File tree

2 files changed

+33
-158
lines changed

2 files changed

+33
-158
lines changed

main.tf

Lines changed: 25 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ locals {
5454
template_disk_count = length(data.vsphere_virtual_machine.template.disks)
5555
}
5656

57-
// Cloning a Linux VM from a given template. Note: This is the default option!!
58-
resource "vsphere_virtual_machine" "Linux" {
59-
count = var.is_windows_image ? 0 : var.instances
57+
// Cloning a Linux or Windows VM from a given template.
58+
resource "vsphere_virtual_machine" "vm" {
59+
count = var.instances
6060
depends_on = [var.vm_depends_on]
6161
name = var.instances == 1 ? var.vmname : format("${var.vmname}${var.vmnameformat}", count.index + 1)
6262

@@ -138,137 +138,32 @@ resource "vsphere_virtual_machine" "Linux" {
138138
timeout = var.timeout
139139

140140
customize {
141-
linux_options {
142-
host_name = var.instances == 1 ? var.vmname : format("${var.vmname}${var.vmnameformat}", count.index + 1)
143-
domain = var.vmdomain
144-
hw_clock_utc = var.hw_clock_utc
145-
}
146-
147-
dynamic "network_interface" {
148-
for_each = keys(var.network)
141+
dynamic "linux_options" {
142+
for_each = var.is_windows_image ? [] : [1]
149143
content {
150-
ipv4_address = var.network[keys(var.network)[network_interface.key]][count.index]
151-
ipv4_netmask = "%{if length(var.ipv4submask) == 1}${var.ipv4submask[0]}%{else}${var.ipv4submask[network_interface.key]}%{endif}"
144+
host_name = "%{if var.vmnameliteral != ""}${var.vmnameliteral}%{else}${var.vmname}${count.index + 1}${var.vmnamesuffix}%{endif}"
145+
domain = var.vmdomain
146+
hw_clock_utc = var.hw_clock_utc
152147
}
153148
}
154-
dns_server_list = var.vmdns
155-
dns_suffix_list = var.dns_suffix_list
156-
ipv4_gateway = var.vmgateway
157-
}
158-
}
159-
160-
// Advanced options
161-
hv_mode = var.hv_mode
162-
ept_rvi_mode = var.ept_rvi_mode
163-
nested_hv_enabled = var.nested_hv_enabled
164-
enable_logging = var.enable_logging
165-
cpu_performance_counters_enabled = var.cpu_performance_counters_enabled
166-
swap_placement_policy = var.swap_placement_policy
167-
latency_sensitivity = var.latency_sensitivity
168-
169-
shutdown_wait_timeout = var.shutdown_wait_timeout
170-
force_power_off = var.force_power_off
171-
}
172-
173-
resource "vsphere_virtual_machine" "Windows" {
174-
count = var.is_windows_image ? var.instances : 0
175-
depends_on = [var.vm_depends_on]
176-
name = var.instances == 1 ? var.vmname : format("${var.vmname}${var.vmnameformat}", count.index + 1)
177-
178-
resource_pool_id = data.vsphere_resource_pool.pool.id
179-
folder = var.vmfolder
180-
tags = var.tag_ids != null ? var.tag_ids : data.vsphere_tag.tag[*].id
181-
custom_attributes = var.custom_attributes
182-
annotation = var.annotation
183-
extra_config = var.extra_config
184-
firmware = var.firmware
185-
efi_secure_boot_enabled = var.efi_secure_boot
186-
enable_disk_uuid = var.enable_disk_uuid
187-
storage_policy_id = var.storage_policy_id
188-
189-
datastore_cluster_id = var.datastore_cluster != "" ? data.vsphere_datastore_cluster.datastore_cluster[0].id : null
190-
datastore_id = var.datastore != "" ? data.vsphere_datastore.datastore[0].id : null
191-
192-
num_cpus = var.cpu_number
193-
num_cores_per_socket = var.num_cores_per_socket
194-
cpu_hot_add_enabled = var.cpu_hot_add_enabled
195-
cpu_hot_remove_enabled = var.cpu_hot_remove_enabled
196-
cpu_reservation = var.cpu_reservation
197-
memory_reservation = var.memory_reservation
198-
memory = var.ram_size
199-
memory_hot_add_enabled = var.memory_hot_add_enabled
200-
guest_id = data.vsphere_virtual_machine.template.guest_id
201-
scsi_bus_sharing = var.scsi_bus_sharing
202-
scsi_type = var.scsi_type != "" ? var.scsi_type : data.vsphere_virtual_machine.template.scsi_type
203-
scsi_controller_count = max(max(0, flatten([
204-
for item in values(var.data_disk) : [
205-
for elem, val in item :
206-
elem == "data_disk_scsi_controller" ? val : 0
207-
]
208-
])...) + 1, var.scsi_controller)
209-
wait_for_guest_net_routable = var.wait_for_guest_net_routable
210-
wait_for_guest_ip_timeout = var.wait_for_guest_ip_timeout
211-
wait_for_guest_net_timeout = var.wait_for_guest_net_timeout
212-
213-
ignored_guest_ips = var.ignored_guest_ips
214-
215-
dynamic "network_interface" {
216-
for_each = keys(var.network)
217-
content {
218-
network_id = data.vsphere_network.network[network_interface.key].id
219-
adapter_type = var.network_type != null ? var.network_type[network_interface.key] : data.vsphere_virtual_machine.template.network_interface_types[0]
220-
}
221-
}
222-
223-
// Disks defined in the original template
224-
dynamic "disk" {
225-
for_each = data.vsphere_virtual_machine.template.disks
226-
iterator = template_disks
227-
content {
228-
label = length(var.disk_label) > 0 ? var.disk_label[template_disks.key] : "disk${template_disks.key}"
229-
size = var.disk_size_gb != null ? var.disk_size_gb[template_disks.key] : data.vsphere_virtual_machine.template.disks[template_disks.key].size
230-
unit_number = var.scsi_controller != null ? var.scsi_controller * 15 + template_disks.key : template_disks.key
231-
thin_provisioned = data.vsphere_virtual_machine.template.disks[template_disks.key].thin_provisioned
232-
eagerly_scrub = data.vsphere_virtual_machine.template.disks[template_disks.key].eagerly_scrub
233-
datastore_id = var.disk_datastore != "" ? data.vsphere_datastore.disk_datastore[0].id : null
234-
storage_policy_id = length(var.template_storage_policy_id) > 0 ? var.template_storage_policy_id[template_disks.key] : null
235-
}
236-
}
237149

238-
// Additional disks defined by Terraform config
239-
dynamic "disk" {
240-
for_each = var.data_disk
241-
iterator = terraform_disks
242-
content {
243-
label = terraform_disks.key
244-
size = lookup(terraform_disks.value, "size_gb", null)
245-
unit_number = lookup(terraform_disks.value, "data_disk_scsi_controller", 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
246-
thin_provisioned = lookup(terraform_disks.value, "thin_provisioned", "true")
247-
eagerly_scrub = lookup(terraform_disks.value, "eagerly_scrub", "false")
248-
datastore_id = lookup(terraform_disks.value, "datastore_id", null)
249-
storage_policy_id = lookup(terraform_disks.value, "storage_policy_id", null)
250-
}
251-
}
252-
clone {
253-
template_uuid = data.vsphere_virtual_machine.template.id
254-
linked_clone = var.linked_clone
255-
timeout = var.timeout
256-
257-
customize {
258-
windows_options {
259-
computer_name = var.instances == 1 ? var.vmname : format("${var.vmname}${var.vmnameformat}", count.index + 1)
260-
admin_password = var.local_adminpass
261-
workgroup = var.workgroup
262-
join_domain = var.windomain
263-
domain_admin_user = var.domain_admin_user
264-
domain_admin_password = var.domain_admin_password
265-
organization_name = var.orgname
266-
run_once_command_list = var.run_once
267-
auto_logon = var.auto_logon
268-
auto_logon_count = var.auto_logon_count
269-
time_zone = var.time_zone
270-
product_key = var.productkey
271-
full_name = var.full_name
150+
dynamic "windows_options" {
151+
for_each = var.is_windows_image ? [1] : []
152+
content {
153+
computer_name = var.instances == 1 ? var.vmname : format("${var.vmname}${var.vmnameformat}", count.index + 1)
154+
admin_password = var.local_adminpass
155+
workgroup = var.workgroup
156+
join_domain = var.windomain
157+
domain_admin_user = var.domain_admin_user
158+
domain_admin_password = var.domain_admin_password
159+
organization_name = var.orgname
160+
run_once_command_list = var.run_once
161+
auto_logon = var.auto_logon
162+
auto_logon_count = var.auto_logon_count
163+
time_zone = var.time_zone
164+
product_key = var.productkey
165+
full_name = var.full_name
166+
}
272167
}
273168

274169
dynamic "network_interface" {

output.tf

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,22 @@ output "ResPool_ID" {
88
value = data.vsphere_resource_pool.pool.id
99
}
1010

11-
output "Windows-VM" {
11+
output "VM" {
1212
description = "VM Names"
13-
value = vsphere_virtual_machine.Windows.*.name
13+
value = vsphere_virtual_machine.vm.*.name
1414
}
1515

16-
output "Windows-ip" {
16+
output "ip" {
1717
description = "default ip address of the deployed VM"
18-
value = vsphere_virtual_machine.Windows.*.default_ip_address
18+
value = vsphere_virtual_machine.vm.*.default_ip_address
1919
}
2020

21-
output "Windows-guest-ip" {
21+
output "guest-ip" {
2222
description = "all the registered ip address of the VM"
23-
value = vsphere_virtual_machine.Windows.*.guest_ip_addresses
23+
value = vsphere_virtual_machine.vm.*.guest_ip_addresses
2424
}
2525

26-
output "Windows-uuid" {
26+
output "uuid" {
2727
description = "UUID of the VM in vSphere"
28-
value = vsphere_virtual_machine.Windows.*.uuid
29-
}
30-
31-
output "Linux-VM" {
32-
description = "VM Names"
33-
value = vsphere_virtual_machine.Linux.*.name
34-
}
35-
36-
output "Linux-ip" {
37-
description = "default ip address of the deployed VM"
38-
value = vsphere_virtual_machine.Linux.*.default_ip_address
39-
}
40-
41-
output "Linux-guest-ip" {
42-
description = "all the registered ip address of the VM"
43-
value = vsphere_virtual_machine.Linux.*.guest_ip_addresses
44-
}
45-
46-
output "Linux-uuid" {
47-
description = "UUID of the VM in vSphere"
48-
value = vsphere_virtual_machine.Linux.*.uuid
28+
value = vsphere_virtual_machine.vm.*.uuid
4929
}

0 commit comments

Comments
 (0)