diff --git a/.gitignore b/.gitignore index 9ea47ed..674e8c9 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,7 @@ Session.vim .netrwhist *~ # auto-generated tag files -tags \ No newline at end of file +tags + +# IDE configs +.idea \ No newline at end of file diff --git a/main.tf b/main.tf index bf5c8bc..cf30916 100644 --- a/main.tf +++ b/main.tf @@ -30,13 +30,13 @@ resource "azurerm_storage_account" "vm-sa" { } resource "azurerm_virtual_machine" "vm-linux" { - count = "${contains(list("${var.vm_os_simple}","${var.vm_os_offer}"), "WindowsServer") ? 0 : var.nb_instances}" - name = "${var.vm_hostname}${count.index}" - location = "${var.location}" - resource_group_name = "${azurerm_resource_group.vm.name}" - availability_set_id = "${azurerm_availability_set.vm.id}" - vm_size = "${var.vm_size}" - network_interface_ids = ["${element(azurerm_network_interface.vm.*.id, count.index)}"] + count = "${!contains(list("${var.vm_os_simple}","${var.vm_os_offer}"), "WindowsServer") && var.data_disk == "false" ? var.nb_instances : 0}" + name = "${var.vm_hostname}${count.index}" + location = "${var.location}" + resource_group_name = "${azurerm_resource_group.vm.name}" + availability_set_id = "${azurerm_availability_set.vm.id}" + vm_size = "${var.vm_size}" + network_interface_ids = ["${element(azurerm_network_interface.vm.*.id, count.index)}"] delete_os_disk_on_termination = "${var.delete_os_disk_on_termination}" storage_image_reference { @@ -48,12 +48,12 @@ resource "azurerm_virtual_machine" "vm-linux" { } storage_os_disk { - name = "osdisk-${var.vm_hostname}-${count.index}" - create_option = "FromImage" + name = "osdisk-${var.vm_hostname}-${count.index}" + create_option = "FromImage" caching = "ReadWrite" managed_disk_type = "${var.storage_account_type}" } - + os_profile { computer_name = "${var.vm_hostname}" admin_username = "${var.admin_username}" @@ -75,14 +75,64 @@ resource "azurerm_virtual_machine" "vm-linux" { } } +resource "azurerm_virtual_machine" "vm-linux-with-datadisk" { + count = "${!contains(list("${var.vm_os_simple}","${var.vm_os_offer}"), "WindowsServer") && var.data_disk == "true" ? var.nb_instances : 0}" + name = "${var.vm_hostname}${count.index}" + location = "${var.location}" + resource_group_name = "${azurerm_resource_group.vm.name}" + availability_set_id = "${azurerm_availability_set.vm.id}" + vm_size = "${var.vm_size}" + network_interface_ids = ["${element(azurerm_network_interface.vm.*.id, count.index)}"] + delete_os_disk_on_termination = "${var.delete_os_disk_on_termination}" + + storage_image_reference { + id = "${var.vm_os_id}" + publisher = "${coalesce(var.vm_os_publisher, module.os.calculated_value_os_publisher)}" + offer = "${coalesce(var.vm_os_offer, module.os.calculated_value_os_offer)}" + sku = "${coalesce(var.vm_os_sku, module.os.calculated_value_os_sku)}" + version = "${var.vm_os_version}" + } + + storage_os_disk { + name = "osdisk-${var.vm_hostname}-${count.index}" + create_option = "FromImage" + caching = "ReadWrite" + managed_disk_type = "${var.storage_account_type}" + } + + storage_data_disk { + name = "${format("datadisk-%s-%d", var.vm_hostname, count.index)}" + create_option = "Empty" + lun = 0 + disk_size_gb = "${var.data_disk_size_gb}" + managed_disk_type = "${var.data_sa_type}" + } + + os_profile { + computer_name = "${var.vm_hostname}" + admin_username = "${var.admin_username}" + admin_password = "${var.admin_password}" + } + + os_profile_linux_config { + + disable_password_authentication = true + + ssh_keys { + path = "/home/${var.admin_username}/.ssh/authorized_keys" + key_data = "${file("${var.ssh_key}")}" + } + } +} + resource "azurerm_virtual_machine" "vm-windows" { - count = "${contains(list("${var.vm_os_simple}","${var.vm_os_offer}"), "WindowsServer") ? var.nb_instances : 0}" - name = "${var.vm_hostname}${count.index}" - location = "${var.location}" - resource_group_name = "${azurerm_resource_group.vm.name}" - availability_set_id = "${azurerm_availability_set.vm.id}" - vm_size = "${var.vm_size}" - network_interface_ids = ["${element(azurerm_network_interface.vm.*.id, count.index)}"] + count = "${contains(list("${var.vm_os_simple}","${var.vm_os_offer}"), "WindowsServer") && var.data_disk == "false" ? var.nb_instances : 0}" + name = "${var.vm_hostname}${count.index}" + location = "${var.location}" + resource_group_name = "${azurerm_resource_group.vm.name}" + availability_set_id = "${azurerm_availability_set.vm.id}" + vm_size = "${var.vm_size}" + network_interface_ids = ["${element(azurerm_network_interface.vm.*.id, count.index)}"] delete_os_disk_on_termination = "${var.delete_os_disk_on_termination}" storage_image_reference { @@ -94,12 +144,52 @@ resource "azurerm_virtual_machine" "vm-windows" { } storage_os_disk { - name = "osdisk${count.index}" + name = "osdisk-${var.vm_hostname}-${count.index}" create_option = "FromImage" caching = "ReadWrite" managed_disk_type = "${var.storage_account_type}" } + os_profile { + computer_name = "${var.vm_hostname}" + admin_username = "${var.admin_username}" + admin_password = "${var.admin_password}" + } +} + +resource "azurerm_virtual_machine" "vm-windows-with-datadisk" { + count = "${contains(list("${var.vm_os_simple}","${var.vm_os_offer}"), "WindowsServer") && var.data_disk == "true" ? var.nb_instances : 0}" + name = "${var.vm_hostname}${count.index}" + location = "${var.location}" + resource_group_name = "${azurerm_resource_group.vm.name}" + availability_set_id = "${azurerm_availability_set.vm.id}" + vm_size = "${var.vm_size}" + network_interface_ids = ["${element(azurerm_network_interface.vm.*.id, count.index)}"] + delete_os_disk_on_termination = "${var.delete_os_disk_on_termination}" + + storage_image_reference { + id = "${var.vm_os_id}" + publisher = "${coalesce(var.vm_os_publisher, module.os.calculated_value_os_publisher)}" + offer = "${coalesce(var.vm_os_offer, module.os.calculated_value_os_offer)}" + sku = "${coalesce(var.vm_os_sku, module.os.calculated_value_os_sku)}" + version = "${var.vm_os_version}" + } + + storage_os_disk { + name = "osdisk-${var.vm_hostname}-${count.index}" + create_option = "FromImage" + caching = "ReadWrite" + managed_disk_type = "${var.storage_account_type}" + } + + storage_data_disk { + name = "${format("datadisk-%s-%d", var.vm_hostname, count.index)}" + create_option = "Empty" + lun = 0 + disk_size_gb = "${var.data_disk_size_gb}" + managed_disk_type = "${var.data_sa_type}" + } + os_profile { computer_name = "${var.vm_hostname}" admin_username = "${var.admin_username}" @@ -149,10 +239,10 @@ resource "azurerm_network_security_group" "vm" { } resource "azurerm_network_interface" "vm" { - count = "${var.nb_instances}" - name = "nic-${var.vm_hostname}-${count.index}" - location = "${azurerm_resource_group.vm.location}" - resource_group_name = "${azurerm_resource_group.vm.name}" + count = "${var.nb_instances}" + name = "nic-${var.vm_hostname}-${count.index}" + location = "${azurerm_resource_group.vm.location}" + resource_group_name = "${azurerm_resource_group.vm.name}" network_security_group_id = "${azurerm_network_security_group.vm.id}" ip_configuration { diff --git a/variables.tf b/variables.tf index 5de118c..f771dfa 100644 --- a/variables.tf +++ b/variables.tf @@ -108,6 +108,22 @@ variable "delete_os_disk_on_termination" { default = "false" } +variable "data_sa_type" { + description = "Data Disk Storage Account type" + default = "Standard_LRS" +} + +variable "data_disk_size_gb" { + description = "Storage data disk size size" + default = "" +} + +variable "data_disk" { + type = "string" + description = "Set to true to add a datadisk." + default = "false" +} + variable "boot_diagnostics" { description = "(Optional) Enable or Disable boot diagnostics" default = "false" @@ -116,4 +132,4 @@ variable "boot_diagnostics" { variable "boot_diagnostics_sa_type" { description = "(Optional) Storage account type for boot diagnostics" default = "Standard_LRS" -} \ No newline at end of file +}