Skip to content

Commit

Permalink
add terraform 0.12 for 05 example
Browse files Browse the repository at this point in the history
  • Loading branch information
diodonfrost committed Sep 18, 2019
1 parent 3cc276a commit 97b1b56
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 133 deletions.
34 changes: 18 additions & 16 deletions 05-pool-with-multiple-network/00-variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ variable "dns_servers" {
#
variable "http_vm_params" {
default = {
vcpu = "2"
ram = "4096"
vcpu = "2"
ram = "4096"
# You can't set a datastore name with interspace
disk_datastore = "datastore_test"
disk_size = "15"
Expand All @@ -59,14 +59,15 @@ variable "http_vm_params" {

variable "http_network_params" {
default = {
domain = "test.local"
label = "http_network"
vlan_id = "1"
base_address = "192.168.1."
prefix_length = "24"
gateway = "192.168.1.254"
domain = "test.local"
label = "http_network"
vlan_id = "1"
base_address = "192.168.1."
prefix_length = "24"
gateway = "192.168.1.254"
}
}

# Define the root hostname of http instance
# The root is incrementing for each instance create
# e.g: a root name with "frt0" will give:
Expand All @@ -89,8 +90,8 @@ variable "http_vm_desired_capacity" {
#
variable "db_vm_params" {
default = {
vcpu = "2"
ram = "2048"
vcpu = "2"
ram = "2048"
# You can't set a datastore name with interspace
disk_datastore = "datastore_test"
disk_size = "15"
Expand All @@ -99,12 +100,12 @@ variable "db_vm_params" {

variable "db_network_params" {
default = {
domain = "test.local"
label = "db_network"
vlan_id = "2"
base_address = "192.168.2."
prefix_length = "24"
gateway = "192.168.2.254"
domain = "test.local"
label = "db_network"
vlan_id = "2"
base_address = "192.168.2."
prefix_length = "24"
gateway = "192.168.2.254"
}
}

Expand All @@ -123,3 +124,4 @@ variable "db_base_hostname" {
variable "db_vm_desired_capacity" {
default = "3"
}

99 changes: 50 additions & 49 deletions 05-pool-with-multiple-network/010-data-retrieve.tf
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
#### GLOBAL CONFIG DC AND HOST
# Define datacenter
data "vsphere_datacenter" "dc" {
name = "${var.dc}"
}

# Exctrat data port vlan creation
data "vsphere_host" "host" {
name = "${var.host}"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

data "vsphere_resource_pool" "pool" {
# If you haven't resource pool, put "Resources" after cluster name
name = "${var.cluster}/Resources"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

# Retrieve datastore information on vsphere
data "vsphere_datastore" "datastore_http" {
name = "${var.http_vm_params["disk_datastore"]}"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

# Retrieve datastore information on vsphere
data "vsphere_datastore" "datastore_db" {
name = "${var.db_vm_params["disk_datastore"]}"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

# Retrieve network information on vsphere
data "vsphere_network" "network_http" {
name = "${var.http_network_params["label"]}"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
depends_on = ["vsphere_host_port_group.http_port"]
}

# Retrieve network information on vsphere
data "vsphere_network" "network_db" {
name = "${var.db_network_params["label"]}"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
depends_on = ["vsphere_host_port_group.db_port"]
}

# Retrieve template information on vsphere
data "vsphere_virtual_machine" "template" {
name = "${var.template_image}"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
}
#### GLOBAL CONFIG DC AND HOST
# Define datacenter
data "vsphere_datacenter" "dc" {
name = var.dc
}

# Exctrat data port vlan creation
data "vsphere_host" "host" {
name = var.host
datacenter_id = data.vsphere_datacenter.dc.id
}

data "vsphere_resource_pool" "pool" {
# If you haven't resource pool, put "Resources" after cluster name
name = "${var.cluster}/Resources"
datacenter_id = data.vsphere_datacenter.dc.id
}

# Retrieve datastore information on vsphere
data "vsphere_datastore" "datastore_http" {
name = var.http_vm_params["disk_datastore"]
datacenter_id = data.vsphere_datacenter.dc.id
}

# Retrieve datastore information on vsphere
data "vsphere_datastore" "datastore_db" {
name = var.db_vm_params["disk_datastore"]
datacenter_id = data.vsphere_datacenter.dc.id
}

# Retrieve network information on vsphere
data "vsphere_network" "network_http" {
name = var.http_network_params["label"]
datacenter_id = data.vsphere_datacenter.dc.id
depends_on = [vsphere_host_port_group.http_port]
}

# Retrieve network information on vsphere
data "vsphere_network" "network_db" {
name = var.db_network_params["label"]
datacenter_id = data.vsphere_datacenter.dc.id
depends_on = [vsphere_host_port_group.db_port]
}

# Retrieve template information on vsphere
data "vsphere_virtual_machine" "template" {
name = var.template_image
datacenter_id = data.vsphere_datacenter.dc.id
}

21 changes: 11 additions & 10 deletions 05-pool-with-multiple-network/020-network.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@
#### HTTP PORT CONFIG
# Create port frt with vlan
resource "vsphere_host_port_group" "http_port" {
name = "${var.http_network_params["label"]}"
host_system_id = "${data.vsphere_host.host.id}"
virtual_switch_name = "${var.vswitch}"
vlan_id = "${var.http_network_params["vlan_id"]}"
name = var.http_network_params["label"]
host_system_id = data.vsphere_host.host.id
virtual_switch_name = var.vswitch
vlan_id = var.http_network_params["vlan_id"]
allow_promiscuous = true
provisioner "local-exec" {
command = "sleep 10"
command = "sleep 10"
}
}

#### DB PORT CONFIG
# Create port db with vlan
resource "vsphere_host_port_group" "db_port" {
name = "${var.db_network_params["label"]}"
host_system_id = "${data.vsphere_host.host.id}"
virtual_switch_name = "${var.vswitch}"
vlan_id = "${var.db_network_params["vlan_id"]}"
name = var.db_network_params["label"]
host_system_id = data.vsphere_host.host.id
virtual_switch_name = var.vswitch
vlan_id = var.db_network_params["vlan_id"]
allow_promiscuous = true
provisioner "local-exec" {
command = "sleep 10"
command = "sleep 10"
}
}

59 changes: 30 additions & 29 deletions 05-pool-with-multiple-network/060-http_vm.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,45 @@
#
#
resource "vsphere_virtual_machine" "http_vm" {
count = "${var.http_vm_desired_capacity}"
name = "${var.http_base_hostname}${count.index + 1}"
num_cpus = "${var.http_vm_params["vcpu"]}"
memory = "${var.http_vm_params["ram"]}"
datastore_id = "${data.vsphere_datastore.datastore_http.id}"
host_system_id = "${data.vsphere_host.host.id}"
resource_pool_id = "${data.vsphere_resource_pool.pool.id}"
guest_id = "${data.vsphere_virtual_machine.template.guest_id}"
scsi_type = "${data.vsphere_virtual_machine.template.scsi_type}"
count = var.http_vm_desired_capacity
name = "${var.http_base_hostname}${count.index + 1}"
num_cpus = var.http_vm_params["vcpu"]
memory = var.http_vm_params["ram"]
datastore_id = data.vsphere_datastore.datastore_http.id
host_system_id = data.vsphere_host.host.id
resource_pool_id = data.vsphere_resource_pool.pool.id
guest_id = data.vsphere_virtual_machine.template.guest_id
scsi_type = data.vsphere_virtual_machine.template.scsi_type

# Configure network interface
network_interface {
network_id = "${data.vsphere_network.network_http.id}"
network_id = data.vsphere_network.network_http.id
}

disk {
name = "${var.http_base_hostname}${count.index + 1}.vmdk"
size = "${var.http_vm_params["disk_size"]}"
name = "${var.http_base_hostname}${count.index + 1}.vmdk"
size = var.http_vm_params["disk_size"]
}

# Define template and customisation params
clone {
template_uuid = "${data.vsphere_virtual_machine.template.id}"

customize {
linux_options {
host_name = "${var.http_base_hostname}${count.index + 1}"
domain = "${var.http_network_params["domain"]}"
}

network_interface {
ipv4_address = "${var.http_network_params["base_address"]}${count.index + 10}"
ipv4_netmask = "${var.http_network_params["prefix_length"]}"
dns_server_list = "${var.dns_servers}"
}

ipv4_gateway = "${var.http_network_params["gateway"]}"
clone {
template_uuid = data.vsphere_virtual_machine.template.id

customize {
linux_options {
host_name = "${var.http_base_hostname}${count.index + 1}"
domain = var.http_network_params["domain"]
}

network_interface {
ipv4_address = "${var.http_network_params["base_address"]}${count.index + 10}"
ipv4_netmask = var.http_network_params["prefix_length"]
dns_server_list = var.dns_servers
}

ipv4_gateway = var.http_network_params["gateway"]
}
depends_on = ["vsphere_host_port_group.http_port"]
}
depends_on = [vsphere_host_port_group.http_port]
}

59 changes: 30 additions & 29 deletions 05-pool-with-multiple-network/061-db_vm.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,45 @@
#
#
resource "vsphere_virtual_machine" "db_vm" {
count = "${var.db_vm_desired_capacity}"
name = "${var.db_base_hostname}${count.index + 1}"
num_cpus = "${var.db_vm_params["vcpu"]}"
memory = "${var.db_vm_params["ram"]}"
datastore_id = "${data.vsphere_datastore.datastore_db.id}"
host_system_id = "${data.vsphere_host.host.id}"
resource_pool_id = "${data.vsphere_resource_pool.pool.id}"
guest_id = "${data.vsphere_virtual_machine.template.guest_id}"
scsi_type = "${data.vsphere_virtual_machine.template.scsi_type}"
count = var.db_vm_desired_capacity
name = "${var.db_base_hostname}${count.index + 1}"
num_cpus = var.db_vm_params["vcpu"]
memory = var.db_vm_params["ram"]
datastore_id = data.vsphere_datastore.datastore_db.id
host_system_id = data.vsphere_host.host.id
resource_pool_id = data.vsphere_resource_pool.pool.id
guest_id = data.vsphere_virtual_machine.template.guest_id
scsi_type = data.vsphere_virtual_machine.template.scsi_type

# Configure network interface
network_interface {
network_id = "${data.vsphere_network.network_db.id}"
network_id = data.vsphere_network.network_db.id
}

disk {
name = "${var.db_base_hostname}${count.index + 1}.vmdk"
size = "${var.db_vm_params["disk_size"]}"
name = "${var.db_base_hostname}${count.index + 1}.vmdk"
size = var.db_vm_params["disk_size"]
}

# Define template and customisation params
clone {
template_uuid = "${data.vsphere_virtual_machine.template.id}"

customize {
linux_options {
host_name = "${var.db_base_hostname}${count.index + 1}"
domain = "${var.db_network_params["domain"]}"
}

network_interface {
ipv4_address = "${var.db_network_params["base_address"]}${count.index + 10}"
ipv4_netmask = "${var.db_network_params["prefix_length"]}"
dns_server_list = "${var.dns_servers}"
}

ipv4_gateway = "${var.db_network_params["gateway"]}"
clone {
template_uuid = data.vsphere_virtual_machine.template.id

customize {
linux_options {
host_name = "${var.db_base_hostname}${count.index + 1}"
domain = var.db_network_params["domain"]
}

network_interface {
ipv4_address = "${var.db_network_params["base_address"]}${count.index + 10}"
ipv4_netmask = var.db_network_params["prefix_length"]
dns_server_list = var.dns_servers
}

ipv4_gateway = var.db_network_params["gateway"]
}
depends_on = ["vsphere_host_port_group.db_port"]
}
depends_on = [vsphere_host_port_group.db_port]
}

4 changes: 4 additions & 0 deletions 05-pool-with-multiple-network/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

terraform {
required_version = ">= 0.12"
}

0 comments on commit 97b1b56

Please sign in to comment.