# terraform-vmware

## Overview
This module provisions VMware virtual machines (VMs) by using authenticated vCenter users; it then updates the newly provisioned VM(s) and bootstraps via Chef using the knife bootstrap script from irrigate-chef-repo. The module is fairly opinionated about following an Orchard-specific workflow, so care must be taken to ensure the correct files and settings are in place.

Currently this module only supports Linux.

## Usage

### Authentication

To connect to vCenter, specify the following variables:
```
  vsphere_user     = "username@domain.com" <-- use this syntax, and NOT domain\username
  vsphere_password = "yourpassword123"
  vsphere_server   = "vcenter.domain.com"
```

### vCenter/vSphere Settings

The following variables control vCenter/vSphere settings for VMs and should be self-explanatory for vCenter users:
```
  vsphere_datacenter_name
  vsphere_datastore_name
  vsphere_compute_cluster_name
  vsphere_network_name
```

### VM settings

The following variables control VM settings:
```
vm_hostnames <-- a list of VM hostnames
vm_domain <-- local domain name for all VMs specified by vm_hostnames
vm_num_cpus <-- number of vCPU for all VMs specified by vm_hostnames
vm_memory <-- amount of memory in MB for all VMs specified by vm_hostnames
```

All VMs specified by `vm_hostnames` receive the same network, domain, vCPU, memory, and datastore settings, so this functionality should primarily be used to provision similar resources, e.g. prod-web01, prod-web02, prod-web03 where all VMs have identical settings and performance characteristics.

### Post-boot settings

#### SSH access

Specify `ssh_user` and `ssh_private_key_path` in order for Terraform to complete provisioning VMs via SSH upon first boot. This module does not support password-based SSH, and neither should you.

#### Post-boot actions

Upon VM startup, the module takes four actions:

* Update the VM
* Run custom commands
* Bootstrap via Chef
* Reboot

#### Update the VM
The specific command(s) to update the newly provisioned system can be controlled by setting `vm_custom_post_boot_commands`. It defaults to a simple `yum -y update` that is suitable for RHEL-based distributions

#### Run custom commands
Custom commands can be controlled by setting `vm_custom_post_boot_commands`, which is a list of arbitrary commands to be run after the machine has been updated, but before Chef bootstrap. This is optional and does not need to be specified.

#### Bootstrap via Chef
This step leverages the Chef bootstrap script located in [irrigate-chef-repo](https://github.com/theorchard/irrigate-chef-repo). You must have a local chef-repo for this functionality to work properly. Set `chef_bootstrap_script_path` to the full path of this script and `chef_client_version` to specify the chef-client version to be installed during bootstrap.

Node names are set to the value of `vm_hostnames`, so make sure there is a node definition named `${var.vm_hostnames[count.index]}.json` in your chef-repo (e.g. `dev-vm-test01.json`)

#### Reboot
This simply executes a reboot command via SSH

## Example
```
module "vmware_vms" {
  source = "git@github.com:theorchard/terraform-vmware.git"

  vsphere_user     = "username@domain.com"
  vsphere_password = "password123
  vsphere_server   = "vcenter.domain.com"

  vm_hostnames = [
    "dev-vm-test01",
    "dev-vm-test02"
  ]
  vm_domain = "theorchard.com"

  vsphere_datacenter_name      = "VM-Datacenter01"
  vsphere_datastore_name       = "nimble01-vol01"
  vsphere_compute_cluster_name = "VM-Cluster01"
  vsphere_network_name         = "VM Network"
}
```
