Last active
February 1, 2021 19:32
-
-
Save GaryFlynn/307fea7d87a958c32a1074616a9e9298 to your computer and use it in GitHub Desktop.
terraform-vsphere-deploy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Basic configuration without variables | |
# Define authentification configuration | |
provider "vsphere" { | |
# If you use a domain set your login like this "Domain\\User" | |
user = "administrator@vsphere.local" | |
password = "VMware1!" | |
vsphere_server = "vcenter-1.homelab.local" | |
# If you have a self-signed cert | |
allow_unverified_ssl = true | |
} | |
#### RETRIEVE DATA INFORMATION ON VCENTER #### | |
data "vsphere_datacenter" "dc" { | |
name = "vSAN Datacenter" | |
} | |
# If you don't have any resource pools, put "Resources" after cluster name | |
data "vsphere_resource_pool" "pool" { | |
name = "vSAN Cluster/Resources" | |
datacenter_id = data.vsphere_datacenter.dc.id | |
} | |
# Retrieve datastore information on vsphere | |
data "vsphere_datastore" "datastore" { | |
name = "vsanDatastore" | |
datacenter_id = data.vsphere_datacenter.dc.id | |
} | |
# Retrieve network information on vsphere | |
data "vsphere_network" "network" { | |
name = "VM_Network-DPortGroup" | |
datacenter_id = data.vsphere_datacenter.dc.id | |
} | |
# Retrieve template information on vsphere | |
data "vsphere_virtual_machine" "template" { | |
name = "centos-8" | |
datacenter_id = data.vsphere_datacenter.dc.id | |
} | |
#### VM CREATION #### | |
# Set vm parameters | |
resource "vsphere_virtual_machine" "demo" { | |
name = "vm-one" | |
num_cpus = 2 | |
memory = 4096 | |
datastore_id = data.vsphere_datastore.datastore.id | |
resource_pool_id = data.vsphere_resource_pool.pool.id | |
guest_id = data.vsphere_virtual_machine.template.guest_id | |
scsi_type = data.vsphere_virtual_machine.template.scsi_type | |
# Set network parameters | |
network_interface { | |
network_id = data.vsphere_network.network.id | |
} | |
# Use a predefined vmware template as main disk | |
disk { | |
label = "vm-one.vmdk" | |
size = "30" | |
} | |
clone { | |
template_uuid = data.vsphere_virtual_machine.template.id | |
customize { | |
linux_options { | |
host_name = "vm-one" | |
domain = "vm-one.homelab.local" | |
} | |
network_interface { | |
ipv4_address = "192.168.0.240" | |
ipv4_netmask = 24 | |
dns_server_list = ["192.168.0.120", "192.168.0.121"] | |
} | |
ipv4_gateway = "192.168.0.1" | |
} | |
} | |
# Execute script on remote vm after this creation | |
provisioner "remote-exec" { | |
script = "scripts/example-script.sh" | |
connection { | |
type = "ssh" | |
user = "root" | |
password = "VMware1!" | |
host = vsphere_virtual_machine.demo.default_ip_address | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment