Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# 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
}
# Create a virtual machine within the folder
resource "vsphere_virtual_machine" "terraform_01" {
# name of the machine inside vCenter
name = "terraform-1"
# DNS inside the machine
dns_suffixes = ["tirefi.re"]
# Domain instead of default vsphere.local
domain = "tirefi.re"
# What datacenter to connect to
datacenter = "Datacenter"
# How many vCPUs
vcpu = 2
# How much memory in MBs
memory = 4096
# Create it in this resource pool
resource_pool = "terraform"
# Linked clones are the best clones, don't forget to create the snapshot
linked_clone = true
network_interface {
# What network you want to connect to
label = "Internal Network 3208"
}
disk {
# What template to clone
template = "template-ubuntu1604"
# What datastore to create it in
datastore = "vsanDatastore"
}
# my template is broken, but as you can see here's some pre-chef work done :)
provisioner "remote-exec" {
inline = [
"sudo rm /var/lib/dpkg/lock",
"sudo dpkg --configure -a",
"sudo apt-get install -f",
"sudo apt autoremove -y",
# This was interesting here, i needed to add a host to /etc/hosts, this injects the sudo password, then tee's the /etc/hosts
"echo admini | sudo -S echo '10.0.0.15 chef chef.tirefi.re' | sudo tee -a /etc/hosts"
]
connection {
type = "${var.connection_thingys.["connection_type"]}"
user = "${var.connection_thingys.["connection_user"]}"
password = "${var.connection_thingys.["connection_password"]}"
}
}
provisioner "chef" {
server_url = "${var.chef_provision.["server_url"]}"
user_name = "${var.chef_provision.["user_name"]}"
# I couldn't figure out how to put the userkey as a variable, so you'll need to change this for you
user_key = "${file("/Users/jjasghar/repo/vmware_playground/pems/admini.pem")}"
node_name = "${var.chef_provision.["node_name_default"]}"
# Here's a inital run_list :)
run_list = ["recipe[base]"]
recreate_client = "${var.chef_provision.["recreate_client"]}"
on_failure = "continue"
ssl_verify_mode = "${var.chef_provision.["ssl_verify_mode_setting"]}"
connection {
type = "${var.connection_thingys.["connection_type"]}"
user = "${var.connection_thingys.["connection_user"]}"
password = "${var.connection_thingys.["connection_password"]}"
}
}
}
# Main login, defaults to my admin account
variable "vsphere_user" {
default = "administrator@vsphere.local"
}
# Main password, defaults to my admin account
variable "vsphere_password" {
default = "Good4bye!"
}
# vCenter server
variable "vsphere_server" {
default = "vcenter.tirefi.re"
}
# My connections for the Chef server
variable "chef_provision" {
type = "map"
description = "Configuration details for chef server"
default = {
# A default node name
node_name_default = "terraform-1"
# Run it again? You probably need to recreate the client
recreate_client = true
# Chef server :)
server_url = "https://chef.tirefi.re/organizations/tirefi/"
# SSL is lame, so lets turn it off
ssl_verify_mode_setting = ":verify_none"
# The username that you authenticate your chef server
user_name = "admini"
}
}
variable "connection_thingys" {
type = "map"
description = "Configuration details for connecting to the remote machine"
default = {
# Default to SSH
connection_type = "ssh"
# User to connect to the server
connection_user = "admini"
# Password to connect to the server
connection_password = "admini"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment