├── .gitignore ├── provider.tf ├── instance.tf ├── LICENSE ├── README.md └── vars.tf /.gitignore: -------------------------------------------------------------------------------- 1 | *.tfstate 2 | *.tfstate.backup 3 | *.tfvars 4 | -------------------------------------------------------------------------------- /provider.tf: -------------------------------------------------------------------------------- 1 | # Configure the VMware vSphere Provider 2 | provider "vsphere" { 3 | user = "${var.viuser}" 4 | password = "${var.vipassword}" 5 | vsphere_server = "${var.viserver}" 6 | # if you have a self-signed cert 7 | allow_unverified_ssl = true 8 | } 9 | -------------------------------------------------------------------------------- /instance.tf: -------------------------------------------------------------------------------- 1 | resource "vsphere_virtual_machine" "TEST-VM" { 2 | count = "${var.vmcount}" 3 | name = "${var.vmname}_0${count.index + 1}" 4 | hostname = "${var.vmname}${count.index + 1}" 5 | vcpu = 2 6 | memory = 4096 7 | domain = "${var.vmdomain}" 8 | dns_suffixes = [ "${var.vmdomain}" ] 9 | datacenter = "YOUR_DC" 10 | cluster = "${lookup(var.vmcluster,var.vmrp)}" 11 | resource_pool = "${lookup(var.vmcluster, var.vmrp)}/Resources/${var.vmrp}" 12 | dns_servers = ["${lookup(var.vmdns1,var.vmdomain)}", "${lookup(var.vmdns2,var.vmdomain)}"] 13 | 14 | network_interface { 15 | label = "${lookup(var.vmnetlabel, var.vmdomain)}" 16 | ipv4_address = "${lookup(var.vmaddrbase,var.vmdomain)}${10 + count.index}" 17 | ipv4_gateway = "${lookup(var.vmgateway,var.vmdomain)}" 18 | ipv4_prefix_length = "24" 19 | } 20 | 21 | disk { 22 | template = "Linux_templates/${var.vmtemp}" 23 | type = "thin" 24 | datastore = "${lookup(var.vmdscluster, var.vmdatastore)}/${var.vmdatastore}" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Emil Wypych 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | TERRAFORM vSPHERE BUILD - MULTIPLE VMs 2 | ======================= 3 | 4 | This is a basic build, which you can use to deploy a multiple VMs in the vSphere infrastructure. Please notice that here you do not have many options like additional disk or copy .vmdk file. Anyway you do not need to specify every variables in the each build - there are a lot of maps which help to provision your infrastructure. 5 | 6 | This build can be helpful with: 7 | 8 | - deploy a new VM for some tests, 9 | - deploy many VM, one by one, 10 | - join infrastructure deploying to the Ansible builds, 11 | - be a part of some tests, based on VMs 12 | - many other cases. 13 | 14 | You can find more details on my page, where I described [how to deploy vSphere VM with Terraform](https://emilwypych.com/2017/02/26/deploying-vsphere-vm-with-terraform/). More information about deploying multiple VMs you can find also on my blog, [just click here](https://emilwypych.com/2017/10/15/deploying-multiple-vsphere-vms-terraform/). 15 | 16 | WARNING! Works only with Linux VMs! 17 | 18 | Requirements 19 | ---------- 20 | 21 | Valid installation of the vSphere with the vCenter Server 22 | 23 | Variables 24 | --------- 25 | 26 | There are couple of variables. Some of them have default values. You should check all of them and ensure yourself that everything is valid for you Infrastructure deploy. This version has maps of variables, for example: 27 | 28 | ``` 29 | variable "vmgateway" { 30 | type = "map" 31 | default = { 32 | local.domain = "192.168.0.1" 33 | second.domain = "192.16.1.1" 34 | } 35 | } 36 | ``` 37 | 38 | You need to create a file named terraform.tfvars in which you put your secrets and another custom variables. You can overwrite variables from the vars.tf file. 39 | 40 | Please remember, that the most important variable is a vmdomain var (maps base on it), but you should specify the following variables: 41 | 42 | ``` 43 | viuser 44 | vipassword 45 | vmname 46 | vmdomain 47 | vmrp 48 | vmdatastore 49 | vmtemp 50 | ``` 51 | 52 | License 53 | ---------- 54 | 55 | [MIT](https://tldrlegal.com/license/mit-license) 56 | 57 | Author 58 | ------- 59 | 60 | [Emil Wypych](https://emilwypych.com) 61 | -------------------------------------------------------------------------------- /vars.tf: -------------------------------------------------------------------------------- 1 | variable "viuser" {} 2 | variable "vipassword" {} 3 | variable "viserver" { 4 | default = "vcs1.local.domain" 5 | } 6 | 7 | // default VM name in vSphere and its hostname 8 | variable "vmname" { 9 | default = "test-vm" 10 | } 11 | 12 | // default Resource Pool 13 | variable "vmrp" { 14 | default = "YOUR_RP" 15 | } 16 | 17 | // default VM domain for guest customization 18 | variable "vmdomain" { 19 | default = "local.domain" 20 | } 21 | 22 | // default datastore to deploy vmdk 23 | variable "vmdatastore" { 24 | default = "DS_SILVER_01" 25 | } 26 | 27 | // default VM Template 28 | variable "vmtemp" { 29 | default = "CENTOS-TEMP" 30 | } 31 | 32 | // map of the datastore clusters (vmdatastore = "vmdscluster") 33 | variable "vmdscluster" { 34 | type = "map" 35 | default = { 36 | DS_SILVER_01 = "DS_CLUSTER_SILVER" 37 | DS_SILVER_02 = "DS_CLUSTER_SILVER" 38 | DS_GOLD_01 = "DS_CLUSTER_GOLD" 39 | DS_GOLD_02 = "DS_CLUSTER_GOLD" 40 | } 41 | } 42 | 43 | // map of the compute clusters (vmrp = "vmcluster") 44 | variable vmcluster { 45 | type = "map" 46 | default = { 47 | YOUR_RP = "RESOURCE_CLUSTER_GOLD" 48 | ANOTHER_RP = "RESOURCE_01_CLUSTER_GOLD" 49 | THIRD_RP = "RESOURCE_CLUSTER_SILVER" 50 | } 51 | } 52 | 53 | // map of the first three octets of the IP address (with netmask /24, vmdomain = "vmaddrbase") 54 | variable "vmaddrbase" { 55 | type = "map" 56 | default = { 57 | local.domain = "192.168.0." 58 | second.domain = "192.168.1." 59 | } 60 | } 61 | 62 | // map of the IP gateways (vmdomain = "vmgateway") 63 | variable "vmgateway" { 64 | type = "map" 65 | default = { 66 | local.domain = "192.168.0.1" 67 | second.domain = "192.168.1.1" 68 | } 69 | } 70 | 71 | // map of the DNS1 addresses (vmdomain = "vmdns1") 72 | variable "vmdns1" { 73 | type = "map" 74 | default = { 75 | local.domain = "192.168.0.5" 76 | second.domain = "192.168.1.5" 77 | } 78 | } 79 | 80 | // map of the DNS2 addresses (vmdomain = "vmdns2") 81 | variable "vmdns2" { 82 | type = "map" 83 | default = { 84 | local.domain = "192.168.0.6" 85 | second.domain = "192.168.1.6" 86 | } 87 | } 88 | 89 | // map of the VM Network (vmdomain = "vmnetlabel") 90 | variable "vmnetlabel" { 91 | type = "map" 92 | default = { 93 | local.domain = "NET_Backend_01" 94 | } 95 | } 96 | --------------------------------------------------------------------------------