terraform module for building/cloning vsphere vms based on existing vm-templates
TERRAFORM MODULE CALL
change the values for the variables according to your vsphere environment and existing vm templates.
module "manager-dev" {
source = "github.com/stuttgart-things/vsphere-vm?ref=v1.7.5-2.7.0"
vm_count = 1
vsphere_vm_name = "manager-dev"
vm_memory = 12288
vm_disk_size = "128"
vm_num_cpus = 12
firmware = "bios"
vsphere_vm_folder_path = "stuttgart-things/dev"
vsphere_datacenter = "/NetApp-HCI-Datacenter"
vsphere_datastore = "/NetApp-HCI-Datacenter/datastore/DatastoreCluster/NetApp-HCI-Datastore-02"
vsphere_resource_pool = "Resources"
vsphere_network = "/NetApp-HCI-Datacenter/network/tiab-prod"
vsphere_vm_template = "/NetApp-HCI-Datacenter/vm/stuttgart-things/vm-templates/ubuntu23"
vm_ssh_user = var.vm_ssh_user
vm_ssh_password = var.vm_ssh_password
bootstrap = ["echo STUTTGART-THINGS"]
annotation = "VSPHERE-VM BUILD w/ TERRAFORM FOR STUTTGART-THINGS"
vsphere_user = var.vsphere_user
vsphere_password = var.vsphere_password
vsphere_server = var.vsphere_server
}
variable "vsphere_server" {
default = false
description = "vsphere server"
}
variable "vsphere_user" {
default = false
description = "password of vsphere user"
}
variable "vsphere_password" {
default = false
description = "password of vsphere user"
}
variable "vm_ssh_user" {
default = "sthings"
description = "username of ssh user for vm"
}
variable "vm_ssh_password" {
default = false
description = "password of ssh user for vm"
}
EXECUTE TERRAFORM / CREATE VM
terraform init
terraform plan
terraform apply --auto-approve \
-var "vsphere_server=<FQDN>" \
-var "vm_ssh_user=<USER>" \
-var "vm_ssh_password=<PASSWORD>" \
-var "vsphere_user=<VSPHERE_USER>" \
-var "vsphere_password=<VSPHERE_PASSWORD>"
DESTROY VM(S)
terraform destroy --auto-approve
CREATE TFVARS AS SECRET
# CREATE terraform.tfvars
cat <<EOF > terraform.tfvars
vsphere_user = "<USER>"
vsphere_password = "<PASSWORD>"
vm_ssh_user = "<SSH_USER>"
vm_ssh_password = "<SSH_PASSWORD>"
EOF
# CREATE SECRET
kubectl create secret generic vsphere-tfvars --from-file=terraform.tfvars
DEFINE (INLINE) WORKSPACE
apiVersion: tf.upbound.io/v1beta1
kind: Workspace
metadata:
name: vsphere-vm-labda-1
annotations:
crossplane.io/external-name: vsphere-vm-labda-1
spec:
forProvider:
source: Inline
module: |
module "labda-vm" {
source = "github.com/stuttgart-things/vsphere-vm"
vm_count = 1
vsphere_vm_name = "michigan3"
vm_memory = 6144
vm_disk_size = "64"
vm_num_cpus = 6
firmware = "bios"
vsphere_vm_folder_path = "stuttgart-things/testing"
vsphere_datacenter = "/NetApp-HCI-Datacenter"
vsphere_datastore = "/NetApp-HCI-Datacenter/datastore/DatastoreCluster/NetApp-HCI-Datastore-02"
vsphere_resource_pool = "Resources"
vsphere_network = "/NetApp-HCI-Datacenter/network/tiab-prod"
vsphere_vm_template = "/NetApp-HCI-Datacenter/vm/stuttgart-things/vm-templates/ubuntu23"
vm_ssh_user = var.vm_ssh_user
vm_ssh_password = var.vm_ssh_password
bootstrap = ["echo STUTTGART-THINGS"]
annotation = "VSPHERE-VM BUILD w/ TERRAFORM CROSSPLANE PROVIDER FOR STUTTGART-THINGS"
}
provider "vsphere" {
user = var.vsphere_user
password = var.vsphere_password
vsphere_server = var.vsphere_server
allow_unverified_ssl = true
}
variable "vsphere_server" {
type = string
default = false
description = "vsphere server"
}
variable "vsphere_user" {
type = string
default = false
description = "password of vsphere user"
}
variable "vsphere_password" {
type = string
default = false
description = "password of vsphere user"
}
variable "vm_ssh_user" {
type = string
default = false
description = "username of ssh user for vm"
}
variable "vm_ssh_password" {
type = string
default = false
description = "password of ssh user for vm"
}
varFiles:
- source: SecretKey
secretKeyRef:
namespace: default
name: vsphere-tfvars
key: terraform.tfvars
writeConnectionSecretToRef:
namespace: default
name: terraform-workspace-vsphere-vm-labda-1
CREATE WORKSPACE
kubectl apply -f <WORKSPACE-DEFINITION>.yaml
DELETE WORKSPACE
kubectl delete workspace vsphere-vm-labda-1
Patrick Hermann, stuttgart-things 12/2019
Licensed under the Apache License, Version 2.0 (the "License").
You may obtain a copy of the License at apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" basis, without WARRANTIES or conditions of any kind, either express or implied.
See the License for the specific language governing permissions and limitations under the License.