Create Virtual Machine with Terraform in VMware
We explain how to generate virtual machines with terraform with provider VMware.
Today we are going to start with practice. We are going to use vSphere 7, to launch virtual machines on it, generated through a terraform tf file.
We connect to our vCenter and we have to be clear about all the data that we are going to use. Like the name of the Datacenter, the cluster, the pool,...
From the networks:
The folders where we want to leave the virtual machines:
Once we have them, we will go to the Terraform server and generate a tf file:
nano elblogdenegu.tf
My example is the following, I create a Debian virtual machine from an ISO:
provider "vsphere" {
user = "administrator@vsphere.local"
password = "Password1234-"
vsphere_server = "192.168.2.54"
# If you have a self-signed cert
allow_unverified_ssl = true
}
data "vsphere_datacenter" "dc" {
name = "ELBLOGDENEGU"
}
data "vsphere_datastore" "datastore" {
name = "NAS01"
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_resource_pool" "pool" {
name = "TERRAFORM"
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_network" "network" {
name = "VM Network"
datacenter_id = data.vsphere_datacenter.dc.id
}
resource "vsphere_virtual_machine" "vm" {
name = "MVPRUEBATERRAFORM"
resource_pool_id = data.vsphere_resource_pool.pool.id
datastore_id = data.vsphere_datastore.datastore.id
num_cpus = 2
memory = 1024
guest_id = "other3xLinux64Guest"
network_interface {
network_id = data.vsphere_network.network.id
}
disk {
label = "disk0"
size = 20
}
cdrom {
datastore_id = data.vsphere_datastore.datastore.id
path = "ISOS/debian-10.3.0-amd64-netinst.iso"
}
}
We start the environment:
terraform init
And we check that we have no errors and what the file is going to do:
terraform plan
We apply:
terraform apply
And we check the result:
Post a Comment