├── .gitignore ├── provider.tf ├── instance.tf ├── LICENSE ├── README.md └── vars.tf /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled files 2 | *.tfstate 3 | *.tfstate.backup 4 | *.tfvars 5 | -------------------------------------------------------------------------------- /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 | name = "${var.vmname}" 3 | hostname = "${var.vmhostname}" 4 | vcpu = 2 5 | memory = 4096 6 | domain = "${var.vmdomain}" 7 | dns_suffixes = [ "${var.vmdomain}" ] 8 | datacenter = "YOUR_DC" 9 | cluster = "${lookup(var.vmcluster,var.vmrp)}" 10 | resource_pool = "${lookup(var.vmcluster, var.vmrp)}/Resources/${var.vmrp}" 11 | dns_servers = ["${lookup(var.vmdns1,var.vmdomain)}", "${lookup(var.vmdns2,var.vmdomain)}"] 12 | 13 | network_interface { 14 | label = "${lookup(var.vmnetlabel, var.vmdomain)}" 15 | ipv4_address = "${lookup(var.vmaddrbase,var.vmdomain)}${var.vmaddroctet}" 16 | ipv4_gateway = "${lookup(var.vmgateway,var.vmdomain)}" 17 | ipv4_prefix_length = "24" 18 | } 19 | 20 | disk { 21 | template = "Linux_templates/${var.vmtemp}" 22 | type = "thin" 23 | datastore = "${lookup(var.vmdscluster, var.vmdatastore)}/${var.vmdatastore}" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /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 2 | ======================= 3 | 4 | This is a basic build, which you can use to deploy a single VM 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/). 15 | 16 | WARNING! Works only with Linux VMs! 17 | 18 | Requirements 19 | ---------- 20 | 21 | Valid installation of vSphere with 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 8 | variable "vmname" { 9 | default = "test-vm" 10 | } 11 | 12 | // default VM hostname 13 | variable "vmhostname" { 14 | default = "test-host" 15 | } 16 | 17 | // default Resource Pool 18 | variable "vmrp" { 19 | default = "YOUR_RP" 20 | } 21 | 22 | // default VM domain for guest customization 23 | variable "vmdomain" { 24 | default = "local.domain" 25 | } 26 | 27 | // default datastore to deploy vmdk 28 | variable "vmdatastore" { 29 | default = "DS_SILVER_01" 30 | } 31 | 32 | // default VM Template 33 | variable "vmtemp" { 34 | default = "CENTOS-TEMP" 35 | } 36 | 37 | // map of the datastore clusters (vmdatastore = "vmdscluster") 38 | variable "vmdscluster" { 39 | type = "map" 40 | default = { 41 | DS_SILVER_01 = "DS_CLUSTER_SILVER" 42 | DS_SILVER_02 = "DS_CLUSTER_SILVER" 43 | DS_GOLD_01 = "DS_CLUSTER_GOLD" 44 | DS_GOLD_02 = "DS_CLUSTER_GOLD" 45 | } 46 | } 47 | 48 | // map of the compute clusters (vmrp = "vmcluster") 49 | variable vmcluster { 50 | type = "map" 51 | default = { 52 | YOUR_RP = "RESOURCE_CLUSTER_GOLD" 53 | ANOTHER_RP = "RESOURCE_01_CLUSTER_GOLD" 54 | THIRD_RP = "RESOURCE_CLUSTER_SILVER" 55 | } 56 | } 57 | 58 | // map of the first three octets of the IP address (with netmask /24, vmdomain = "vmaddrbase") 59 | variable "vmaddrbase" { 60 | type = "map" 61 | default = { 62 | local.domain = "192.168.0." 63 | second.domain = "192.168.1." 64 | } 65 | } 66 | 67 | // host octet in the IP address 68 | variable "vmaddroctet" { 69 | default = "200" 70 | } 71 | 72 | // map of the IP gateways (vmdomain = "vmgateway") 73 | variable "vmgateway" { 74 | type = "map" 75 | default = { 76 | local.domain = "192.168.0.1" 77 | second.domain = "192.168.1.1" 78 | } 79 | } 80 | 81 | // map of the DNS1 addresses (vmdomain = "vmdns1") 82 | variable "vmdns1" { 83 | type = "map" 84 | default = { 85 | local.domain = "192.168.0.5" 86 | second.domain = "192.168.1.5" 87 | } 88 | } 89 | 90 | // map of the DNS2 addresses (vmdomain = "vmdns2") 91 | variable "vmdns2" { 92 | type = "map" 93 | default = { 94 | local.domain = "192.168.0.6" 95 | second.domain = "192.168.1.6" 96 | } 97 | } 98 | 99 | // map of the VM Network (vmdomain = "vmnetlabel") 100 | variable "vmnetlabel" { 101 | type = "map" 102 | default = { 103 | local.domain = "NET_Backend_01" 104 | } 105 | } 106 | --------------------------------------------------------------------------------