I have two folders – instance and volume.
instance
-main.tf
volume
-main.tf
-volume.tf
I am defining the instance folder main.tf as
data "vsphere_datacenter" "datacenter" {
name = "dc-01"
}
data "vsphere_datastore" "datastore" {
name = "datastore-01"
datacenter_id = data.vsphere_datacenter.datacenter.id
}
data "vsphere_compute_cluster" "cluster" {
name = "cluster-01"
datacenter_id = data.vsphere_datacenter.datacenter.id
}
data "vsphere_network" "network" {
name = "VM Network"
datacenter_id = data.vsphere_datacenter.datacenter.id
}
resource "vsphere_virtual_machine" "vm" {
name = "foo"
resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
datastore_id = data.vsphere_datastore.datastore.id
num_cpus = 1
memory = 1024
guest_id = "otherLinux64Guest"
network_interface {
network_id = data.vsphere_network.network.id
}
disk {
label = "disk0"
size = 20
}
}
}
with single disk. But I need two additional disks which I am defining separately in volume folder – volume.tf
module host {
source "../instance"
}
-volume.tf
data "vsphere_datacenter" "datacenter" {
name = "dc-01"
}
data "vsphere_datacenter" "datastore" {
name = "datastore-01"
}
resource "vsphere_virtual_disk" "virtual_disk1" {
size = 40
type = "thin"
vmdk_path = "/foo/foo.vmdk"
create_directories = true
datacenter = data.vsphere_datacenter.datacenter.name
datastore = data.vsphere_datastore.datastore.name
}
resource "vsphere_virtual_disk" "virtual_disk2" {
size = 40
type = "thin"
vmdk_path = "/foo/foo.vmdk"
create_directories = true
datacenter = data.vsphere_datacenter.datacenter.name
datastore = data.vsphere_datastore.datastore.name
}
Now in volumes.tf, I want to define some code through which I can attach these 2 disks to instance created in instance folder main.tf. I do not see any documentation to attach external disks to newly created server in other folder.
To attach additional disks from the volume
folder to the VM in the instance
folder, follow these steps:
1. In instance/main.tf
:
Add an output for the VM ID so it can be referenced later.
output "vm_id" {
value = vsphere_virtual_machine.vm.id
}
2. In volume/volume.tf
:
Reference the VM and attach the new disks using vsphere_virtual_disk_attachment
.
module "host" {
source = "../instance"
}
data "vsphere_virtual_machine" "vm" {
id = module.host.vm_id
}
resource "vsphere_virtual_disk_attachment" "attach_disk1" {
virtual_machine_id = data.vsphere_virtual_machine.vm.id
disk_id = vsphere_virtual_disk.virtual_disk1.vmdk_path
}
resource "vsphere_virtual_disk_attachment" "attach_disk2" {
virtual_machine_id = data.vsphere_virtual_machine.vm.id
disk_id = vsphere_virtual_disk.virtual_disk2.vmdk_path
}
This way, the disks from volume
will attach to the VM created in instance
.
1