First we need to ensure we have the terraform vSphere provider installed.
- Install Terraform: Ensure that Terraform is installed on your machine. You can download it from the official Terraform website and follow the installation instructions for your operating system.
- Initialize Your Terraform Configuration: Create a directory for your Terraform configuration files. Inside this directory, create a `main.tf` file (or any `.tf` file) where you will define your provider and other configurations.
- Define the vSphere Provider in Your Configuration: Add the following block to your Terraform configuration file to specify the use of the vSphere provider:
terraform {
required_providers {
vsphere = {
source = "hashicorp/vsphere"
version = "> 2.0"
}
}
}
provider "vsphere" {
user = "your-username"
password = "your-password"
vsphere_server = "your-vsphere-server"
# If you have a self-signed cert
allow_unverified_ssl = true
}
Replace `”your-username”`, `”your-password”`, and `”your-vsphere-server”` with your actual vSphere credentials and server address.
Initialize the Terraform Working Directory: Run the following command in your terminal from the directory where your Terraform configuration file is located:
terraform init
This command will download the vSphere provider and initialize your Terraform working directory. It sets up the necessary provider plugins for Terraform to interact with vSphere.
Verify the Installation: After running `terraform init`, you should see output indicating that the vSphere provider has been successfully installed. You can now proceed to create Terraform configurations to manage your vSphere resources.
Deploy a VM within vSphere
In this example, we are deploying a VM running fedora (Red Hat Enterprise Linux Community version).
Terraform Module Structure
- variables.tf: Define the variables for the module.
- main.tf: Contains the main configuration for the VM.
- outputs.tf: Define the outputs for the module.
variables.tf
variable "vsphere_user" {
description = "vSphere username"
}
variable "vsphere_password" {
description = "vSphere password"
}
variable "vsphere_server" {
description = "vSphere server"
}
variable "datacenter" {
description = "vSphere datacenter"
}
variable "cluster" {
description = "vSphere cluster"
}
variable "network" {
description = "vSphere network"
}
variable "datastore" {
description = "vSphere datastore"
}
variable "template" {
description = "VM template to clone"
}
main.tf
provider "vsphere" {
user = var.vsphere_user
password = var.vsphere_password
vsphere_server = var.vsphere_server
# If you have a self-signed cert
allow_unverified_ssl = true
}
data "vsphere_datacenter" "dc" {
name = var.datacenter
}
data "vsphere_compute_cluster" "cluster" {
name = var.cluster
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_network" "network" {
name = var.network
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_datastore" "datastore" {
name = var.datastore
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_virtual_machine" "template" {
name = var.template
datacenter_id = data.vsphere_datacenter.dc.id
}
resource "vsphere_virtual_machine" "fedora_vm" {
name = "FedoraVM"
resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
datastore_id = data.vsphere_datastore.datastore.id
num_cpus = 4
memory = 16384
guest_id = data.vsphere_virtual_machine.template.guest_id
network_interface {
network_id = data.vsphere_network.network.id
adapter_type = data.vsphere_virtual_machine.template.network_interface_types0
}
disk {
label = "disk0"
size = 200
eagerly_scrub = false
thin_provisioned = true
}
clone {
template_uuid = data.vsphere_virtual_machine.template.id
customize {
linux_options {
host_name = "fedora-vm"
domain = "local"
}
network_interface {
ipv4_address = "192.168.1.100" # Replace with your desired IP
ipv4_netmask = 24
}
ipv4_gateway = "192.168.1.1" # Replace with your gateway
}
}
}
outputs.tf
output "vm_id" {
description = "The ID of the VM"
value = vsphere_virtual_machine.fedora_vm.id
}
output "vm_ip" {
description = "The IP address of the VM"
value = vsphere_virtual_machine.fedora_vm.default_ip_address
}
- Replace the variables with your vSphere environment details, such as `datacenter`, `cluster`, `network`, `datastore`, and `template`.
- Customize the IP address and gateway to match your network settings.