Hi all,
I have been following you for a long time and you have helped me many times… thank you very much!
So… i have little bit problem… and i don’t find out the solution
I need to deploy multiple VMs on Vsphere with multiple disks on multiple datastore…
I have compiled this module with this tfvars file, all is ok but i cannot get datastore.id by data.tf…
when i run terraform plan, my result is it ok but the datastore id is not resolved by terraform but only take the “clear” data on tfvars… (will be not deploy inside it)… below the bold datastore.id
terraform plan --var site=US --var-file=prod.tfvars -out=prod.plan
# module.vm.vsphere_virtual_machine.vm["server1"] will be created
+ resource "vsphere_virtual_machine" "vm" {
+ annotation = "VM1 PRod"
+ boot_retry_delay = 10000
+ change_version = (known after apply)
+ cpu_hot_add_enabled = true
+ cpu_limit = -1
+ cpu_share_count = (known after apply)
+ cpu_share_level = "normal"
+ datastore_id = "datastore-1179"
+ default_ip_address = (known after apply)
+ ept_rvi_mode = "automatic"
+ extra_config_reboot_required = true
+ firmware = "efi"
+ force_power_off = true
+ guest_id = "sles15_64Guest"
+ guest_ip_addresses = (known after apply)
+ hardware_version = (known after apply)
+ host_system_id = "host-1175"
+ hv_mode = "hvAuto"
+ id = (known after apply)
+ ide_controller_count = 2
+ imported = (known after apply)
+ latency_sensitivity = "normal"
+ memory = 2048
+ memory_hot_add_enabled = true
+ memory_limit = -1
+ memory_share_count = (known after apply)
+ memory_share_level = "normal"
+ migrate_wait_timeout = 30
+ moid = (known after apply)
+ name = "server1"
+ num_cores_per_socket = 1
+ num_cpus = 8
+ power_state = (known after apply)
+ poweron_timeout = 300
+ reboot_required = (known after apply)
+ resource_pool_id = "resgroup-1177"
+ run_tools_scripts_after_power_on = true
+ run_tools_scripts_after_resume = true
+ run_tools_scripts_before_guest_shutdown = true
+ run_tools_scripts_before_guest_standby = true
+ sata_controller_count = 0
+ scsi_bus_sharing = "noSharing"
+ scsi_controller_count = 4
+ scsi_type = "pvscsi"
+ shutdown_wait_timeout = 3
+ storage_policy_id = (known after apply)
+ swap_placement_policy = "inherit"
+ sync_time_with_host = true
+ tools_upgrade_policy = "manual"
+ uuid = (known after apply)
+ vapp_transport = (known after apply)
+ vmware_tools_status = (known after apply)
+ vmx_path = (known after apply)
+ wait_for_guest_ip_timeout = 0
+ wait_for_guest_net_routable = true
+ wait_for_guest_net_timeout = 0
+ clone {
+ template_uuid = "423deb75-fdf1-7553-b080-bd617bb1281d"
+ timeout = 30
+ customize {
+ dns_server_list = [
+ "10.237.216.7",
+ "10.237.208.5",
+ "10.237.111.8",
]
+ ipv4_gateway = "10.237.113.1"
+ timeout = 10
+ linux_options {
+ domain = "adgr.net"
+ host_name = "server1"
+ hw_clock_utc = true
}
+ network_interface {
+ ipv4_address = "10.237.113.186"
+ ipv4_netmask = 24
}
}
}
+ disk {
+ attach = false
+ controller_type = "scsi"
+ datastore_id = "<computed>"
+ device_address = (known after apply)
+ disk_mode = "persistent"
+ disk_sharing = "sharingNone"
+ eagerly_scrub = false
+ io_limit = -1
+ io_reservation = 0
+ io_share_count = 0
+ io_share_level = "normal"
+ keep_on_remove = false
+ key = 0
+ label = "disk0"
+ path = (known after apply)
+ size = 80
+ storage_policy_id = (known after apply)
+ thin_provisioned = true
+ unit_number = 0
+ uuid = (known after apply)
+ write_through = false
}
+ disk {
+ attach = false
+ controller_type = "scsi"
+ datastore_id = "datastore1"
+ device_address = (known after apply)
+ disk_mode = "persistent"
+ disk_sharing = "sharingNone"
+ eagerly_scrub = false
+ io_limit = -1
+ io_reservation = 0
+ io_share_count = 0
+ io_share_level = "normal"
+ keep_on_remove = false
+ key = 0
+ label = "0"
+ path = (known after apply)
+ size = 20
+ storage_policy_id = (known after apply)
+ thin_provisioned = true
+ unit_number = 10
+ uuid = (known after apply)
+ write_through = false
}
+ disk {
+ attach = false
+ controller_type = "scsi"
+ datastore_id = "LocalStorage103"
+ device_address = (known after apply)
+ disk_mode = "persistent"
+ disk_sharing = "sharingNone"
+ eagerly_scrub = false
+ io_limit = -1
+ io_reservation = 0
+ io_share_count = 0
+ io_share_level = "normal"
+ keep_on_remove = false
+ key = 0
+ label = "1"
+ path = (known after apply)
+ size = 200
+ storage_policy_id = (known after apply)
+ thin_provisioned = true
+ unit_number = 30
+ uuid = (known after apply)
+ write_through = false
}
+ network_interface {
+ adapter_type = "vmxnet3"
+ bandwidth_limit = -1
+ bandwidth_reservation = 0
+ bandwidth_share_count = (known after apply)
+ bandwidth_share_level = "normal"
+ device_address = (known after apply)
+ key = (known after apply)
+ mac_address = (known after apply)
+ network_id = "network-12"
}
}
Plan: 2 to add, 0 to change, 0 to destroy.
Below my configuration…
main.tf
module "vm" {
source = "/home/omar/terraform-vm-foreach/vm"
vsphere_user = var.vsphere_user
vsphere_password = var.vsphere_password
vsphere_server = var.vsphere_server
vsphere_unverified_ssl = var.vsphere_unverified_ssl
compute_cluster = var.compute_cluster
#resource_pool = var.resource_pool
datacenter = var.datacenter
virtual_machines = var.virtual_machines
site = var.site
change = var.change
}
prod.tfvars
vsphere_server = "192.168.10.100"
vsphere_unverified_ssl = true
datacenter = "Lab.local"
compute_cluster = "Lab.local"
change = "CH0000999"
virtual_machines = {
server1 = {
datacenter = "Lab.local"
compute_cluster = "Lab.local"
rspool = "max"
network_interface = "VM Network"
vmnotes = "VM1 PRod"
category = "dbserver"
system_cores = 8
system_memory = 2048
system_disk = 80
system_ipv4_address = "10.237.113.186"
system_ipv4_netmask = "24"
system_ipv4_gateway = "10.237.113.1"
datastore = "LocalStorage103"
datastore_a = "LocalStorage103"
datastore_b = "datastore1"
folder = "/"
system_name = "server1"
system_domain = "ad1.local"
dns_server_list = ["192.168.1.200", "192.168.1.201", "192.168.1.202"]
disks = [ {
vdisklabel = "server1-disk16"
vdisksize = "20"
vdisknumber = "10"
vdatastore = "datastore1"
},
{
vdisklabel = "server2-disk30"
vdisksize = "20"
vdisknumber = "30"
vdatastore = "datastore2"
},
]
}
}
variables.tf
variable "vsphere_user" {}
variable "vsphere_password" {}
variable "vsphere_server" {}
variable "vsphere_unverified_ssl" {}
variable "compute_cluster" {}
variable "datacenter" {}
variable "site" {}
variable "change" {}
variable "virtual_machines" {}
On VM folder (this is the module)
data.tf
locals{
vds = var.site == "US" ? "vds_001" : "vds_002"
hostesx = var.site == "US" ? "192.168.10.103" : "192.168.10.104"
vmgroup = var.site == "US" ? "RheLicenseVM" : "RheLicenseVM"
template = var.site == "US" ? "MicroOS-Temp" : "RHEL8_ZH_template"
virtm = { for_each = var.virtual_machines}
flat_sandboxes = {
for sandbox in var.virtual_machines :
sandbox.system_name => sandbox
}
network_subnets = flatten([
for network_key, network in var.virtual_machines : [
for subnet in network.disks : {
network_key = network_key
purpose = subnet.vdatastore
namedisk = subnet.vdisklabel
sizedisk = subnet.vdisksize
numberdisk = subnet.vdisknumber
}
]
])
}
data "vsphere_datacenter" "datacenter" {
name = var.datacenter
}
data "vsphere_resource_pool" "pool" {
for_each = var.virtual_machines
name = each.value.rspool
datacenter_id = data.vsphere_datacenter.datacenter.id
}
data "vsphere_virtual_machine" "template" {
name = "MicroOS-Temp"
datacenter_id = data.vsphere_datacenter.datacenter.id
}
data "vsphere_datastore" "datastore" {
for_each = var.virtual_machines
name = each.value.datastore
datacenter_id = data.vsphere_datacenter.datacenter.id
}
data "vsphere_datastore" "datastore2" {
for_each = {
#for ns in local.network_subnets: ns.purpose => ns
for index, ns in local.network_subnets : index => ns
}
name = each.value.purpose
datacenter_id = data.vsphere_datacenter.datacenter.id
}
data "vsphere_datastore" "datastore_a" {
for_each = var.virtual_machines
name = each.value.datastore_a
datacenter_id = data.vsphere_datacenter.datacenter.id
}
data "vsphere_datastore" "datastore_b" {
for_each = var.virtual_machines
name = each.value.datastore_b
datacenter_id = data.vsphere_datacenter.datacenter.id
}
data "vsphere_network" "network_interface" {
for_each = var.virtual_machines
name = each.value.network_interface
datacenter_id = data.vsphere_datacenter.datacenter.id
}
data "vsphere_host" "host" {
name = local.hostesx
datacenter_id = data.vsphere_datacenter.datacenter.id
}
instance.tf
resource "vsphere_virtual_machine" "vm" {
for_each = var.virtual_machines
name = each.key
resource_pool_id = "${data.vsphere_host.host.resource_pool_id}"
guest_id = data.vsphere_virtual_machine.template.guest_id
scsi_type = data.vsphere_virtual_machine.template.scsi_type
num_cpus = each.value.system_cores
memory = each.value.system_memory
host_system_id = data.vsphere_host.host.id
annotation = each.value.vmnotes
firmware = "efi"
sync_time_with_host = true
cpu_hot_add_enabled = true
memory_hot_add_enabled = true
datastore_id = data.vsphere_datastore.datastore[each.key].id
folder = each.value.folder
wait_for_guest_ip_timeout = 0
wait_for_guest_net_timeout = 0
scsi_controller_count = 4
#Network
network_interface {
network_id = data.vsphere_network.network_interface[each.key].id
adapter_type = data.vsphere_virtual_machine.template.network_interface_types[0]
}
disk {
label = "disk0"
size = each.value.system_disk
eagerly_scrub = false
thin_provisioned = true
#unit_number = 0
}
dynamic "disk" {
for_each = each.value.disks
content {
label = disk.key
size = disk.value.vdisksize
unit_number = disk.value.vdisknumber
datastore_id = disk.value.vdatastore
}
}
#cloning from template
clone {
template_uuid = data.vsphere_virtual_machine.template.id
customize {
linux_options {
host_name = each.value.system_name
domain = each.value.system_domain
}
network_interface {
ipv4_address = each.value.system_ipv4_address
ipv4_netmask = each.value.system_ipv4_netmask
}
ipv4_gateway = each.value.system_ipv4_gateway
dns_server_list = each.value.dns_server_list
}
}
}
resource "local_file" "change" {
content = var.change
filename = "${path.module}/change"
}
providers.tf
terraform {
required_providers {
vsphere = {
source = "hashicorp/vsphere"
}
}
required_version = ">= 1.1.0"
}
provider "vsphere" {
user = var.vsphere_user
password = var.vsphere_password
vsphere_server = var.vsphere_server
allow_unverified_ssl = var.vsphere_unverified_ssl
}
variable.tf
variable "vsphere_user" {
type = string
description = "User that connects to the vCenter."
default = "admin"
sensitive = true
}
variable "vsphere_password" {
type = string
description = "Password of the user that connects to the vCenter."
default = "admin"
sensitive = true
}
variable "vsphere_server" {
type = string
description = "vCenter URL."
default = "https://localhost"
}
variable "change" {
type = string
description = "change"
default = ""
}
variable "vsphere_unverified_ssl" {
type = bool
description = "Disable verification of vCenter server HTTPS certificate."
default = false
}
variable "compute_cluster" {
type = string
description = "Cluster where to provision the servers of the group."
default = ""
}
variable "resource_pool" {
type = string
description = "Cluster where to provision the servers of the group."
default = ""
}
variable "datacenter" {
type = string
description = "Datacenter where to provision the servers of the group."
default = ""
}
variable "vds" {
type = string
description = "select os option"
default = "vds"
}
variable "site" {}
variable "virtual_machines" {
type = map(object({
system_cores = number
system_memory = number
system_disk = number
system_ipv4_address = string
system_ipv4_netmask = string
system_ipv4_gateway = string
rspool = string
category = string
datastore = string
datastore_a = string
datastore_b = string
folder = string
compute_cluster = string
network_interface = string
vmnotes = string
system_name = string
system_domain = string
dns_server_list = list(string)
disks = list(object({
vdisklabel=string
vdisksize=number
vdisknumber=number
vdatastore=string}))
}))
I hope to help me because i don’t understand where is my problem…
Thx a lot
Omar