Skip to content

Instantly share code, notes, and snippets.

@jason-azze
Last active January 9, 2018 13:05
Show Gist options
  • Save jason-azze/d9e3a8ced36c83a1e72ac5971d26cc40 to your computer and use it in GitHub Desktop.
Save jason-azze/d9e3a8ced36c83a1e72ac5971d26cc40 to your computer and use it in GitHub Desktop.
Appendices for vsphere-provider #341 file 1: called and calling modules. (Not including variables.tf, outputs.tf, or tfvars files)
# Terraform code to stand up Jenkins agents for devci Jenkins.
/* Include our vsphere module and provide it with some inputs.
Don't forget you'll need to run 'terraform init -upgrade=true' to get the latest
version of any modules */
# For now, variables that affect each of these modules will be kept in tfvars files.
module "windows-test" {
source = "git::http://stash.mycompany.com/scm/ops/tf-modules.git//vsphere-mod-win?ref=v1.1.14"
vsphere_user = "${var.vsphere_user}"
vsphere_password = "${var.vsphere_password}"
vsphere_server = "${var.vsphere_server}"
how_many = "2"
name_starting_val = "15"
template_choice = "win7-dATemplate.v5"
admin_password_value = "${var.admin_password_value}"
name_prefix = "win7-da"
vm_folder_name = "${var.vm_folder_name}"
vcpu_count = "16"
cores_per_socket = "8"
memory_amount = "65536"
dns_domain = "${var.dns_domain}"
dns_search_zones = "${var.dns_search_zones}"
dns_server_addresses = "${var.dns_server_addresses}"
vm_datacenter_name = "${var.vm_datacenter_name}"
vm_cluster_name = "${var.vm_cluster_name}"
resource_pool_designator = "${var.resource_pool_designator}"
network_label_designator = "${var.network_label_designator}"
datastore_designator = "${var.datastore_designator}"
# time_zone_value = "085"
}
module "windows-build" {
source = "git::http://stash.mycompany.com/scm/ops/tf-modules.git//vsphere-mod-win?ref=v1.1.14"
vsphere_user = "${var.vsphere_user}"
vsphere_password = "${var.vsphere_password}"
vsphere_server = "${var.vsphere_server}"
how_many = "2"
name_starting_val = "65"
template_choice = "Templates/w7-gwbuildTemplate.v7"
admin_password_value = "${var.admin_password_value}"
name_prefix = "w7-build"
vm_folder_name = "${var.vm_folder_name}"
vcpu_count = "20"
cores_per_socket = "10"
memory_amount = "20480"
dns_domain = "${var.dns_domain}"
dns_search_zones = "${var.dns_search_zones}"
dns_server_addresses = "${var.dns_server_addresses}"
vm_datacenter_name = "${var.vm_datacenter_name}"
vm_cluster_name = "${var.vm_cluster_name}"
resource_pool_designator = "${var.resource_pool_designator}"
network_label_designator = "${var.network_label_designator}"
datastore_designator = "${var.datastore_designator}"
# time_zone_value = "085"
}
module "linux-test" {
source = "git::http://stash.mycompany.com/scm/ops/tf-modules.git//vsphere-mod-lin?ref=v1.1.14"
vsphere_user = "${var.vsphere_user}"
vsphere_password = "${var.vsphere_password}"
vsphere_server = "${var.vsphere_server}"
how_many = "2"
name_starting_val = "3"
template_choice = "Templates/cent69-dAtemplate.v5"
name_prefix = "cent69-da"
vm_folder_name = "${var.vm_folder_name}"
vcpu_count = "4"
cores_per_socket = "2"
memory_amount = "8192"
dns_domain = "${var.dns_domain}"
dns_search_zones = "${var.dns_search_zones}"
dns_server_addresses = "${var.dns_server_addresses}"
vm_datacenter_name = "${var.vm_datacenter_name}"
vm_cluster_name = "${var.vm_cluster_name}"
resource_pool_designator = "${var.resource_pool_designator}"
network_label_designator = "${var.network_label_designator}"
datastore_designator = "${var.datastore_designator}"
# time_zone_value = "Etc/UTC"
}
module "linux-build" {
source = "git::http://stash.mycompany.com/scm/ops/tf-modules.git//vsphere-mod-lin?ref=v1.1.14"
vsphere_user = "${var.vsphere_user}"
vsphere_password = "${var.vsphere_password}"
vsphere_server = "${var.vsphere_server}"
how_many = "2"
name_starting_val = "22"
template_choice = "Templates/cent69-buildTemplate.v5"
name_prefix = "cent69-build"
vm_folder_name = "${var.vm_folder_name}"
vcpu_count = "20"
cores_per_socket = "4"
memory_amount = "16384"
dns_domain = "${var.dns_domain}"
dns_search_zones = "${var.dns_search_zones}"
dns_server_addresses = "${var.dns_server_addresses}"
vm_datacenter_name = "${var.vm_datacenter_name}"
vm_cluster_name = "${var.vm_cluster_name}"
resource_pool_designator = "${var.resource_pool_designator}"
network_label_designator = "${var.network_label_designator}"
datastore_designator = "${var.datastore_designator}"
# time_zone_value = "Etc/UTC"
}
#module "bds-build" {
# source = "git::http://stash.mycompany.com/scm/ops/tf-modules.git//vsphere-mod-win?ref=v1.1.14"
# vsphere_user = "${var.vsphere_user}"
# vsphere_password = "${var.vsphere_password}"
# vsphere_server = "${var.vsphere_server}"
# how_many = "5"
# name_starting_val = "8"
# template_choice = "bdsbuilderTemplate"
#admin_password_value = "${var.admin_password_value}"
# name_prefix = "bdsbuilder"
# vm_folder_name = "${var.vm_folder_name}"
# vcpu_count = "2"
# memory_amount = "16384"
# dns_domain = "${var.dns_domain}"
# dns_search_zones = "${var.dns_search_zones}"
# dns_server_addresses = "${var.dns_server_addresses}"
# vm_datacenter_name = "${var.vm_datacenter_name}"
# vm_cluster_name = "${var.vm_cluster_name}"
# resource_pool_designator = "${var.resource_pool_designator}"
# network_label_designator = "${var.network_label_designator}"
# datastore_designator = "${var.datastore_designator}"
# time_zone_value = "Etc/UTC"
#}
#module "bds-test" {
# source = "git::http://stash.mycompany.com/scm/ops/tf-modules.git//vsphere-mod-win?ref=v1.1.14"
# vsphere_user = "${var.vsphere_user}"
# vsphere_password = "${var.vsphere_password}"
# vsphere_server = "${var.vsphere_server}"
# how_many = "${var.how_many}"
# name_starting_val = "${var.name_starting_val}"
# template_choice = "${var.template_choice}"
# admin_password_value = "${var.admin_password_value}"
# name_prefix = "${var.name_prefix}"
# vm_folder_name = "${var.vm_folder_name}"
# vcpu_count = "${var.vcpu_count}"
# cores_per_socket = "2"
# memory_amount = "${var.memory_amount}"
# dns_domain = "${var.dns_domain}"
# dns_search_zones = "${var.dns_search_zones}"
# dns_server_addresses = "${var.dns_server_addresses}"
# vm_datacenter_name = "${var.vm_datacenter_name}"
# vm_cluster_name = "${var.vm_cluster_name}"
# resource_pool_designator = "${var.resource_pool_designator}"
# network_label_designator = "${var.network_label_designator}"
# datastore_designator = "${var.datastore_designator}"
# time_zone_value = "085"
#}
#module "cadbuild" {
# source = "git::http://stash.mycompany.com/scm/ops/tf-modules.git//vsphere-mod-win?ref=v1.1.14"
# vsphere_user = "${var.vsphere_user}"
# vsphere_password = "${var.vsphere_password}"
# vsphere_server = "${var.vsphere_server}"
# how_many = "1"
# name_starting_val = "9"
# template_choice = "Templates/cadbuildTemplate.v1"
# admin_password_value = "${var.admin_password_value}"
# name_prefix = "cadbuild"
# vm_folder_name = "${var.vm_folder_name}"
# vcpu_count = "4"
# cores_per_socket = "2"
# memory_amount = "16384"
# dns_domain = "${var.dns_domain}"
# dns_search_zones = "${var.dns_search_zones}"
# dns_server_addresses = "${var.dns_server_addresses}"
# vm_datacenter_name = "${var.vm_datacenter_name}"
# vm_cluster_name = "${var.vm_cluster_name}"
# resource_pool_designator = "${var.resource_pool_designator}"
# network_label_designator = "${var.network_label_designator}"
# datastore_designator = "${var.datastore_designator}"
# time_zone_value = "085"
#}
module "utility-agents" {
source = "git::http://stash.mycompany.com/scm/ops/tf-modules.git//vsphere-mod-lin?ref=v1.1.14"
vsphere_user = "${var.vsphere_user}"
vsphere_password = "${var.vsphere_password}"
vsphere_server = "${var.vsphere_server}"
how_many = "2"
name_starting_val = "4"
template_choice = "Templates/cent7-utilityTemplate.v3"
name_prefix = "dev-util"
vm_folder_name = "${var.vm_folder_name}"
vcpu_count = "1"
cores_per_socket = "1"
memory_amount = "1024"
dns_domain = "${var.dns_domain}"
dns_search_zones = "${var.dns_search_zones}"
dns_server_addresses = "${var.dns_server_addresses}"
vm_datacenter_name = "${var.vm_datacenter_name}"
vm_cluster_name = "${var.vm_cluster_name}"
resource_pool_designator = "${var.resource_pool_designator}"
network_label_designator = "${var.network_label_designator}"
datastore_designator = "${var.datastore_designator}"
# time_zone_value = "Etc/UTC"
}
# Temporarily disable outputs to chase bug.
output "hostnames-wintest" {
value = "${module.windows-test.hostname}"
}
output "hostnames-winbuild" {
value = "${module.windows-build.hostname}"
}
output "hostnames-lintest" {
value = "${module.linux-test.hostname}"
}
output "hostnames-linbuild" {
value = "${module.linux-build.hostname}"
}
#output "hostnames-bdsbuild" {
# value = "${module.bds-build.hostname}"
#}
#output "hostnames-bdstest" {
# value = "${module.bds-test.hostname}"
#}
#output "hostnames-cadbuild" {
# value = "${module.cadbuild.hostname}"
#}
output "hostnames-utility" {
value = "${module.utility-agents.hostname}"
}
/* A Terraform module intended to provision Windows VMs in vSphere */
# Configure the VMware vSphere Provider
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 sources allow data to be fetched or computed for use elsewhere in Terraform
configuration. Data sources present read-only views into pre-existing data, or they
compute new values on the fly within Terraform itself. */
data "vsphere_datacenter" "target-datacenter" {
name = "${var.vm_datacenter_name}"
}
data "vsphere_datastore" "target-datastore" {
name = "${var.datastore_designator}"
datacenter_id = "${data.vsphere_datacenter.target-datacenter.id}"
}
data "vsphere_resource_pool" "target-resource-pool" {
name = "${var.vm_cluster_name}/Resources/${var.resource_pool_designator}"
datacenter_id = "${data.vsphere_datacenter.target-datacenter.id}"
}
data "vsphere_network" "target-network" {
name = "${var.network_label_designator}"
datacenter_id = "${data.vsphere_datacenter.target-datacenter.id}"
}
data "vsphere_virtual_machine" "source-template" {
name = "${var.template_choice}"
datacenter_id = "${data.vsphere_datacenter.target-datacenter.id}"
}
/* Create a virtual machine within the folder.
In the name attribute, %02d is constructed from 0, indicating that you want to pad
the number to the specified width with leading zeros. Then 2 indicates the width that
you want, and d specifies a base 10 integer. Later we added the name_starting_val so
you can start counting at a number of your choice. */
resource "vsphere_virtual_machine" "windows" {
name = "${var.name_prefix}${format("%02d", count.index + var.name_starting_val)}"
resource_pool_id = "${data.vsphere_resource_pool.target-resource-pool.id}"
datastore_id = "${data.vsphere_datastore.target-datastore.id}"
count = "${var.how_many}"
folder = "${var.vm_folder_name}"
num_cpus = "${var.vcpu_count}"
num_cores_per_socket = "${var.cores_per_socket}"
memory = "${var.memory_amount}"
guest_id = "${data.vsphere_virtual_machine.source-template.guest_id}"
scsi_type = "${data.vsphere_virtual_machine.source-template.scsi_type}"
/* Although the documentation says that "You must specify at least the same number of disk sub-resources as there are disks that exist in the template", the use of "at least" is incorrect. We must know the exact number of disks a template has and then must have exactly that number of disk sub-resources. This is covered in GitHub issue 285 for terraform-provider-vsphere.
Also, the name of the disk must be the name of the VM in the case of a clone. @vancluever says: "vSphere clones the disk during a clone with the general default naming way that a VM gets during creation. This follows the convention of VMNAME.vmdk for the initial disk, and VMNAME_INDEX.vmdk for all other disks, hence foobar.vmdk, foobar_1.vmdk, foobar_2.vmdk, and so on.*/
disk {
name = "${var.name_prefix}${format("%02d", count.index + var.name_starting_val)}.vmdk"
size = "${data.vsphere_virtual_machine.source-template.disks.0.size}"
# The thin_provisioned attribute is only strictly required for linked clones. I included it for safety to ensure we got the setting used in the template.
# But I suspected it might have caused an error around eagerly_scrub (see #316) so I commented it out. It might be worth trying again.
# thin_provisioned = "${data.vsphere_virtual_machine.source-template.disks.0.thin_provisioned}"
}
# The adapter_type must match the adapter type in the template or you will get a new NIC with a new GUID, which will nuke any custom settings
network_interface {
network_id = "${data.vsphere_network.target-network.id}"
adapter_type = "vmxnet3"
# The adapter_type attribute below is commented out because I think it is implicated in #316. It is optional, and was included only for safety.
# Me later: Interesting, I'd forgotten I commented this out and got burned when the default vmxnet3 was different from the e1000 in the template.
# It forced a new NIC to be created with a new GUID and the dynamic DNS reg check box checked.
# It's worth revisiting this to see if it really implicated in #316.
# adapter_type = "${data.vsphere_virtual_machine.source-template.network_interface_types[0]}"
}
clone {
template_uuid = "${data.vsphere_virtual_machine.source-template.id}"
# Timout in minutes. Upped from default 30. Clone was taking longer than that and upon fail leaving stub VMs.
timeout = "60"
customize {
# empty means use DHCP.
# This block is required. We must have one for each NIC the template has.
# Docs say adapter type default is e1000, but I observe that it is vmxnet3. If different from template, will create new NIC with new GUID and default settings.
network_interface {}
windows_options {
computer_name = "${var.name_prefix}${format("%02d", count.index + var.name_starting_val)}"
admin_password = "${var.admin_password_value}"
auto_logon = "true"
auto_logon_count = "1"
}
}
}
lifecycle {
create_before_destroy = "false"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment