From bd89b7b010c76417e2dfe132c9fbb7982c63f434 Mon Sep 17 00:00:00 2001 From: Elsvent Hong Date: Mon, 3 Dec 2018 10:24:11 +0800 Subject: [PATCH] Using ISO pre-install template --- alternative/README.md | 20 +++++++ alternative/terraform.tf | 79 ++++++++++++++++++++++++++++ alternative/terraform.tfvars.example | 38 +++++++++++++ alternative/variables.tf | 62 ++++++++++++++++++++++ 4 files changed, 199 insertions(+) create mode 100644 alternative/README.md create mode 100644 alternative/terraform.tf create mode 100644 alternative/terraform.tfvars.example create mode 100644 alternative/variables.tf diff --git a/alternative/README.md b/alternative/README.md new file mode 100644 index 0000000..557a2bd --- /dev/null +++ b/alternative/README.md @@ -0,0 +1,20 @@ +# vSphere terraform unify os for customize ip hostname setting + +The original repository using ova package, the alternative way using ISO pre-install VM +convert VM to template for customize. + +For pre-install VM step: + +1. Just install VM by ISO in normal way. +2. Left most option default. +3. Check below critiria, ensure you setup VM template correct. + +Some point you may need to know before using vSphere plugin + +1. vmware tool is required for clone customize +2. perl is required(if you search you will see a lot of user failed because perl +3. please aware the support matrix, some essential OS may failed the guestOS check. + +Note: if you are using CentOS, sometime may failed because /etc/redhat-release not match. +There is a trick hint: for me using **Red Hat Enterprise Linux Server release 7.0 (Maipo)** +, it will work and not effect function. diff --git a/alternative/terraform.tf b/alternative/terraform.tf new file mode 100644 index 0000000..9c0b1bd --- /dev/null +++ b/alternative/terraform.tf @@ -0,0 +1,79 @@ +provider "vsphere" { + user = "${var.user}" + password = "${var.password}" + vsphere_server = "${var.vsphere_server}" + + allow_unverified_ssl = true +} + +data "vsphere_datacenter" "dc" { + name = "${var.datacenter}" +} + +data "vsphere_datastore" "datastore" { + name = "${var.datastore}" + datacenter_id = "${data.vsphere_datacenter.dc.id}" +} + +data "vsphere_resource_pool" "pool" { + name = "${var.resource_pool}" + datacenter_id = "${data.vsphere_datacenter.dc.id}" +} + +data "vsphere_network" "network" { + name = "${var.network}" + 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" "vm" { + count = "${var.vm_numbers}" + name = "${var.name}-${count.index+1}" + resource_pool_id = "${data.vsphere_resource_pool.pool.id}" + datastore_id = "${data.vsphere_datastore.datastore.id}" + + num_cpus = 4 + memory = 8192 + guest_id = "${data.vsphere_virtual_machine.template.guest_id}" + + scsi_type = "${data.vsphere_virtual_machine.template.scsi_type}" + + network_interface { + network_id = "${data.vsphere_network.network.id}" + adapter_type = "${data.vsphere_virtual_machine.template.network_interface_types[0]}" + } + + disk { + label = "disk0" + size = "${data.vsphere_virtual_machine.template.disks.0.size}" + eagerly_scrub = "${data.vsphere_virtual_machine.template.disks.0.eagerly_scrub}" + thin_provisioned = "${data.vsphere_virtual_machine.template.disks.0.thin_provisioned}" + } + + cdrom { + client_device = true + } + + clone { + template_uuid = "${data.vsphere_virtual_machine.template.id}" + customize { + linux_options { + host_name = "${var.name}${count.index+1}" + domain = "${var.virtual_machine_domain}" + } + + network_interface { + ipv4_address = "${cidrhost(var.virtual_machine_network_address, var.virtual_machine_ip_address_start + count.index)}" + ipv4_netmask = "${element(split("/", var.virtual_machine_network_address), 1)}" + } + + ipv4_gateway = "${var.virtual_machine_network_gateway}" + dns_suffix_list = ["${var.virtual_machine_domain}"] + dns_server_list = ["${var.virtual_machine_dns_servers}"] + } + } +} diff --git a/alternative/terraform.tfvars.example b/alternative/terraform.tfvars.example new file mode 100644 index 0000000..319b32c --- /dev/null +++ b/alternative/terraform.tfvars.example @@ -0,0 +1,38 @@ +# General vCenter data +user = "FILL_ME_IN" # VC User + +password = "FILL_ME_IN" # vSphere Password + +vsphere_server = "FILL_ME_IN" # VC URL (IP, hostname or FQDN) + +datacenter = "FILL_ME_IN" # vSphere datacenter + +datastore = "FILL_ME_IN" # vSphere datastore + +resource_pool = "FILL_ME_IN" # vSphere Resourcepool + +# Virtual Machine configuration +name = "FILL_ME_IN" # name of the virtual machine + +template = "FILL_ME_IN" # chosen name of the template + +network = "FILL_ME_IN" # network for the VM to reside in + +cpus = 2 # CPU cores of the VM + +memory = 1024 # Memory of the VM in Mb + +vm_numbers = 4 # VM numbers you want to create + +virtual_machine_network_address = "FILL_ME_IN" # VM cidr ex: 192.168.1.0/24 + +virtual_machine_ip_address_start = "FILL_ME_IN" # ex: 100 + +virtual_machine_domain = "FILL_ME_IN" # domain ex: example.com + +virtual_machine_network_gateway = "FILL_ME_IN" # ex: 192.168.1.254 + +virtual_machine_dns_servers = [ + "8.8.8.8", + "8.8.4.4", +] diff --git a/alternative/variables.tf b/alternative/variables.tf new file mode 100644 index 0000000..e0adda2 --- /dev/null +++ b/alternative/variables.tf @@ -0,0 +1,62 @@ +# General vCenter data +# vCenter / ESXi Username +variable "user" {} + +# vCenter / ESXi Password +variable "password" {} + +# vCenter / ESXi Endpoint +variable "vsphere_server" {} + +# vCenter / ESXi Datacenter +variable "datacenter" {} + +# vCenter / ESXi Datastore +variable "datastore" {} + +# vCenter / ESXi ResourcePool +variable "resource_pool" {} + +# Virtual Machine configuration +# VM Name +variable "name" {} + +# Name of OVA template (chosen in import process) +variable "template" {} + +# VM Network +variable "network" {} + +# VM Number of CPU's +variable "cpus" {} + +# VM Memory in MB +variable "memory" {} +# VM numbers +variable "vm_numbers" {} + +// The network address for the virtual machines, in the form of 10.0.0.0/24. +variable "virtual_machine_network_address" { + type = "string" +} + +variable "virtual_machine_network_gateway" { + type = "string" +} + +variable "virtual_machine_domain" { + type = "string" +} + +// The last octect that serves as the start of the IP addresses for the virtual +// machines. Given the default value here of 100, if the network address is +// 10.0.0.0/24, the 3 virtual machines will be assigned addresses 10.0.0.100, +// 10.0.0.101, and 10.0.0.102. +variable "virtual_machine_ip_address_start" { + type = "string" +} + +// The DNS servers for the network the virtual machines reside in. +variable "virtual_machine_dns_servers" { + type = "list" +}