From 97b1b562035909a8e8731daa252b3f4da0d67354 Mon Sep 17 00:00:00 2001 From: diodonfrost Date: Wed, 18 Sep 2019 21:46:00 +0200 Subject: [PATCH] add terraform 0.12 for 05 example --- 05-pool-with-multiple-network/00-variables.tf | 34 ++++--- .../010-data-retrieve.tf | 99 ++++++++++--------- 05-pool-with-multiple-network/020-network.tf | 21 ++-- 05-pool-with-multiple-network/060-http_vm.tf | 59 +++++------ 05-pool-with-multiple-network/061-db_vm.tf | 59 +++++------ 05-pool-with-multiple-network/versions.tf | 4 + 6 files changed, 143 insertions(+), 133 deletions(-) create mode 100644 05-pool-with-multiple-network/versions.tf diff --git a/05-pool-with-multiple-network/00-variables.tf b/05-pool-with-multiple-network/00-variables.tf index ae412bc..36b85f9 100644 --- a/05-pool-with-multiple-network/00-variables.tf +++ b/05-pool-with-multiple-network/00-variables.tf @@ -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" @@ -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: @@ -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" @@ -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" } } @@ -123,3 +124,4 @@ variable "db_base_hostname" { variable "db_vm_desired_capacity" { default = "3" } + diff --git a/05-pool-with-multiple-network/010-data-retrieve.tf b/05-pool-with-multiple-network/010-data-retrieve.tf index 0d39ad3..59a2e1a 100644 --- a/05-pool-with-multiple-network/010-data-retrieve.tf +++ b/05-pool-with-multiple-network/010-data-retrieve.tf @@ -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 +} + diff --git a/05-pool-with-multiple-network/020-network.tf b/05-pool-with-multiple-network/020-network.tf index 88c9594..ab01c13 100644 --- a/05-pool-with-multiple-network/020-network.tf +++ b/05-pool-with-multiple-network/020-network.tf @@ -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" } } + diff --git a/05-pool-with-multiple-network/060-http_vm.tf b/05-pool-with-multiple-network/060-http_vm.tf index e2b6d40..edace32 100644 --- a/05-pool-with-multiple-network/060-http_vm.tf +++ b/05-pool-with-multiple-network/060-http_vm.tf @@ -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] +} + diff --git a/05-pool-with-multiple-network/061-db_vm.tf b/05-pool-with-multiple-network/061-db_vm.tf index 4ceda02..4b629e8 100644 --- a/05-pool-with-multiple-network/061-db_vm.tf +++ b/05-pool-with-multiple-network/061-db_vm.tf @@ -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] +} + diff --git a/05-pool-with-multiple-network/versions.tf b/05-pool-with-multiple-network/versions.tf new file mode 100644 index 0000000..ac97c6a --- /dev/null +++ b/05-pool-with-multiple-network/versions.tf @@ -0,0 +1,4 @@ + +terraform { + required_version = ">= 0.12" +}