Skip to content

Commit

Permalink
add missing main.tf files from examples
Browse files Browse the repository at this point in the history
  • Loading branch information
josenk committed Feb 4, 2022
1 parent 5f06604 commit 273dd55
Show file tree
Hide file tree
Showing 7 changed files with 434 additions and 0 deletions.
30 changes: 30 additions & 0 deletions examples/01 Simple Guest/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#########################################
# ESXI Provider host/login details
#########################################
#
# Use of variables here to hide/move the variables to a separate file
#
provider "esxi" {
esxi_hostname = var.esxi_hostname
esxi_hostport = var.esxi_hostport
esxi_hostssl = var.esxi_hostssl
esxi_username = var.esxi_username
esxi_password = var.esxi_password
}

#########################################
# ESXI Guest resource
#########################################
#
# This Guest VM is "bare-metal". It will be powered on by default
# by terraform, but it will not boot to any OS. It will however attempt
# to network boot.
#
resource "esxi_guest" "vmtest01" {
guest_name = "vmtest01" # Required, Specify the Guest Name
disk_store = "DS_001" # Required, Specify an existing Disk Store
network_interfaces {
virtual_network = "VM Network" # Required for each network interface, Specify the Virtual Network name.
}
}

56 changes: 56 additions & 0 deletions examples/02 Cloned Guest - Complete build/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#########################################
# ESXI Provider host/login details
#########################################
#
# Use of variables here to hide/move the variables to a separate file
#
provider "esxi" {
esxi_hostname = var.esxi_hostname
esxi_hostport = var.esxi_hostport
esxi_hostssl = var.esxi_hostssl
esxi_username = var.esxi_username
esxi_password = var.esxi_password
}

#########################################
# ESXI Guest resource
#########################################
#
# This Guest VM is a clone of an existing Guest VM named "centos7" (must exist and
# be powered off), located in the "Templates" resource pool. vmtest02 will be powered
# on by default by terraform. The virtual network "VM Network", must already exist on
# your esxi host!
#
resource "esxi_guest" "vmtest02" {
guest_name = "vmtest02"
disk_store = "DS_001"
guestos = "centos-64"

boot_disk_type = "thin"
boot_disk_size = "35"

memsize = "2048"
numvcpus = "2"
resource_pool_name = "/"
power = "on"

# clone_from_vm uses ovftool to clone an existing Guest on your esxi host. This example will clone a Guest VM named "centos7", located in the "Templates" resource pool.
# ovf_source uses ovftool to produce a clone from an ovf or vmx image. (typically produced using the ovf_tool).
# Basically clone_from_vm clones from sources on the esxi host and ovf_source clones from sources on your local hard disk or a URL.
# These two options are mutually exclusive.
clone_from_vm = "Templates/centos7"

#ovf_source = "/my_local_system_path/centos-7-min/centos-7.vmx"

network_interfaces {
virtual_network = "VM Network"
mac_address = "00:50:56:a1:b1:c2"
nic_type = "e1000"
}
network_interfaces {
virtual_network = "VM Network2"
}

guest_startup_timeout = 45
guest_shutdown_timeout = 30
}
99 changes: 99 additions & 0 deletions examples/03 Resource Pools - Additional Storage/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#########################################
# ESXI Provider host/login details
#########################################
#
# Use of variables here to hide/move the variables to a separate file
#
provider "esxi" {
esxi_hostname = var.esxi_hostname
esxi_hostport = var.esxi_hostport
esxi_hostssl = var.esxi_hostssl
esxi_username = var.esxi_username
esxi_password = var.esxi_password
}

#########################################
# Resource Pools
#########################################
resource "esxi_resource_pool" "Terraform" {
resource_pool_name = "Terraform"
cpu_min = "100"
cpu_min_expandable = "true"
cpu_max = "8000"
cpu_shares = "normal"
mem_min = "200"
mem_min_expandable = "false"
mem_max = "8192"
mem_shares = "normal"
}

resource "esxi_resource_pool" "pool2" {
resource_pool_name = "${esxi_resource_pool.Terraform.resource_pool_name}/pool2"
}

#########################################
# Virtual Disks
#########################################
resource "esxi_virtual_disk" "vdisk1" {
virtual_disk_disk_store = var.disk_store
virtual_disk_dir = "Terraform"
virtual_disk_name = "vdisk_1.vmdk"
virtual_disk_size = 10
virtual_disk_type = "zeroedthick"
}

resource "esxi_virtual_disk" "vdisk2" {
virtual_disk_disk_store = var.disk_store
virtual_disk_dir = "Terraform"
virtual_disk_size = 15
virtual_disk_type = "eagerzeroedthick"
}

#########################################
# ESXI Guest resource
#########################################
#
# This Guest VM is a clone of an existing Guest VM named "centos7" (must exist and
# be powered off), located in the "Templates" resource pool. vmtest03 will be powered
# on by default by terraform. The virtual network "VM Network", must already exist on
# your esxi host!
#
resource "esxi_guest" "vmtest03" {
guest_name = "vmtes03"
disk_store = var.disk_store
guestos = "centos-64"

boot_disk_type = "thin"
boot_disk_size = "35"

memsize = "2048"
numvcpus = "2"
resource_pool_name = esxi_resource_pool.pool2.resource_pool_name
power = "on"

# clone_from_vm uses ovftool to clone an existing Guest on your esxi host. This example will clone a Guest VM named "centos7", located in the "Templates" resource pool.
# ovf_source uses ovftool to produce a clone from an ovf or vmx image. (typically produced using the ovf_tool).
# Basically clone_from_vm clones from sources on the esxi host and ovf_source clones from sources on your local hard disk or URL.
# These two options are mutually exclusive.
clone_from_vm = "Templates/centos7"

#ovf_source = "/my_local_system_path/centos-7-min/centos-7.vmx"

network_interfaces {
virtual_network = "VM Network"
mac_address = "00:50:56:a1:b1:c3"
nic_type = "e1000"
}

guest_startup_timeout = 45
guest_shutdown_timeout = 30

virtual_disks {
virtual_disk_id = esxi_virtual_disk.vdisk1.id
slot = "0:1"
}
virtual_disks {
virtual_disk_id = esxi_virtual_disk.vdisk2.id
slot = "0:2"
}
}
80 changes: 80 additions & 0 deletions examples/04 CoreOS and Ignition/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#########################################
# ESXI Provider host/login details
#########################################
#
# Use of variables here to hide/move the variables to a separate file
#
provider "esxi" {
version = "~> 1.5"
esxi_hostname = var.esxi_hostname
esxi_hostport = var.esxi_hostport
esxi_hostssl = var.esxi_hostssl
esxi_username = var.esxi_username
esxi_password = var.esxi_password
}

#########################################
# coreos and Ignition
# See: https://coreos.com/ignition/docs/latest/
#
# - To use this example, you must download the latest ova from coreos site.
# - curl -LO https://stable.release.core-os.net/amd64-usr/current/coreos_production_vmware_ova.ova
#########################################

data "ignition_user" "cluster_user" {
name = "core"

# used to calculate a password hash below if you want password: mkpasswd --method=SHA-512 --rounds=4096
#password_hash = "$6$rounds=4096$v28up9xdAO$aLCyf/FfGU72QsOlJN60CyXH5yuyJ/f6WWeW3wyvPjHt4uDOUFbFCxchrf9FCUkUdng7bwZbonBk7aOFQ8Bcm0" # test1234
ssh_authorized_keys = [file("~/.ssh/id_rsa.pub")]
}

data "ignition_systemd_unit" "example" {
name = "example.service"
content = file("example.service")
}

data "ignition_config" "coreos" {
users = [
data.ignition_user.cluster_user.id,
]

systemd = [
data.ignition_systemd_unit.example.id,
]
}

resource "esxi_guest" "coreos" {
guest_name = "${terraform.workspace}-coreos"
disk_store = var.disk_store
guest_startup_timeout = 180

guestinfo = {
"coreos.config.data.encoding" = "base64"
"coreos.config.data" = base64encode(data.ignition_config.coreos.rendered)
}

ovf_source = "../../coreos_production_vmware_ova.ova"
power = "on"
memsize = "2048"
numvcpus = "2"
network_interfaces {
virtual_network = "VM Network" # Required for each network interface, Specify the Virtual Network name.
}

connection {
host = self.ip_address
type = "ssh"
user = "core"
private_key = file("~/.ssh/id_rsa")
timeout = "180s"
}

provisioner "remote-exec" {
inline = [
"echo success",
]
#"update_engine_client -update",
}
}

55 changes: 55 additions & 0 deletions examples/05 CloudInit and Templates/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#########################################
# ESXI Provider host/login details
#########################################
#
# Use of variables here to hide/move the variables to a separate file
#
provider "esxi" {
esxi_hostname = var.esxi_hostname
esxi_hostport = var.esxi_hostport
esxi_hostssl = var.esxi_hostssl
esxi_username = var.esxi_username
esxi_password = var.esxi_password
}

#########################################
# cloud-init for vmware!
# You must install it on your source VM before cloning it!
# See https://github.com/vmware/cloud-init-vmware-guestinfo for more details.
# and https://cloudinit.readthedocs.io/en/latest/topics/examples.html#
#
#
# yum install https://github.com/vmware/cloud-init-vmware-guestinfo/releases/download/v1.1.0/cloud-init-vmware-guestinfo-1.1.0-1.el7.noarch.rpm
# cloud-init clean
#########################################

#
# Template for initial configuration bash script
# template_file is a great way to pass variables to
# cloud-init
data "template_file" "Default" {
template = file("userdata.tpl")
vars = {
HOSTNAME = var.vm_hostname
HELLO = "Hello World!"
}
}

#########################################
# ESXI Guest resource
#########################################
resource "esxi_guest" "Default" {
guest_name = var.vm_hostname
disk_store = var.disk_store

clone_from_vm = "Templates/centos7"

network_interfaces {
virtual_network = var.virtual_network
}

guestinfo = {
"userdata.encoding" = "gzip+base64"
"userdata" = base64gzip(data.template_file.Default.rendered)
}
}
60 changes: 60 additions & 0 deletions examples/06 OVF Properties/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
provider "esxi" {
esxi_hostname = var.esxi_hostname
esxi_hostport = var.esxi_hostport
esxi_hostssl = var.esxi_hostssl
esxi_username = var.esxi_username
esxi_password = var.esxi_password
}

#
# Template for initial configuration bash script
# template_file is a great way to pass variables to
# cloud-init
data "template_file" "userdata_default" {
template = file("userdata.tpl")
vars = {
HOSTNAME = var.vm_hostname
HELLO = "Hello ESXi World!"
}
}

resource "esxi_guest" "vmtest" {
guest_name = var.vm_hostname
disk_store = var.disk_store

network_interfaces {
virtual_network = var.virtual_network
}

guestinfo = {
"userdata.encoding" = "gzip+base64"
"userdata" = base64gzip(data.template_file.userdata_default.rendered)
}

#
# Specify an ovf file to use as a source.
#
ovf_source = var.ovf_file

#
# Specify ovf_properties specific to the source ovf/ova.
# Use ovftool <filename>.ova to get details of which ovf_properties are available.
#
ovf_properties {
key = "hostname"
value = "firstboot"
}

ovf_properties {
key = "user-data"
value = base64encode(data.template_file.userdata_default.rendered)
}

#
# Default ovf_properties_timer is 90 seconds. ovf_properties are injected on first
# boot. This value should be high enough to allow the vmguest to fully boot to
# a linux prompt. The second boot is needed to configure the vmguest as
# specified. (cpus, memory, adding or expanding disks, etc...)
#
#ovf_properties_timer = 90
}
Loading

0 comments on commit 273dd55

Please sign in to comment.