From ce71f7e503f635976c2482d3a26d87283ee323d1 Mon Sep 17 00:00:00 2001 From: Yuping Wei <56525716+yupwei68@users.noreply.github.com> Date: Mon, 24 May 2021 16:32:30 +0800 Subject: [PATCH] Add variable `delete_data_disks_on_termination` (#170) * update * update * update * update * update * update Co-authored-by: root --- README.md | 37 +++++++++++++++++++------------------ main.tf | 17 +++++++++-------- test/fixture/main.tf | 21 +++++++++++---------- variables.tf | 6 ++++++ 4 files changed, 45 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 5f6af6b..12a2c33 100644 --- a/README.md +++ b/README.md @@ -182,24 +182,25 @@ data "azurerm_key_vault_certificate" "example" { } module "linuxservers" { - source = "Azure/compute/azurerm" - resource_group_name = azurerm_resource_group.example.name - vm_hostname = "mylinuxvm" - nb_public_ip = 0 - remote_port = "22" - nb_instances = 2 - vm_os_publisher = "Canonical" - vm_os_offer = "UbuntuServer" - vm_os_sku = "18.04-LTS" - vnet_subnet_id = module.network.vnet_subnets[0] - boot_diagnostics = true - delete_os_disk_on_termination = true - nb_data_disk = 2 - data_disk_size_gb = 64 - data_sa_type = "Premium_LRS" - enable_ssh_key = true - ssh_key_values = ["ssh-rsa AAAAB3NzaC1yc2EAAAAD..."] - vm_size = "Standard_D4s_v3" + source = "Azure/compute/azurerm" + resource_group_name = azurerm_resource_group.example.name + vm_hostname = "mylinuxvm" + nb_public_ip = 0 + remote_port = "22" + nb_instances = 2 + vm_os_publisher = "Canonical" + vm_os_offer = "UbuntuServer" + vm_os_sku = "18.04-LTS" + vnet_subnet_id = module.network.vnet_subnets[0] + boot_diagnostics = true + delete_os_disk_on_termination = true + nb_data_disk = 2 + data_disk_size_gb = 64 + data_sa_type = "Premium_LRS" + enable_ssh_key = true + ssh_key_values = ["ssh-rsa AAAAB3NzaC1yc2EAAAAD..."] + vm_size = "Standard_D4s_v3" + delete_data_disks_on_termination = true tags = { environment = "dev" diff --git a/main.tf b/main.tf index c62f424..7b50153 100644 --- a/main.tf +++ b/main.tf @@ -30,14 +30,15 @@ resource "azurerm_storage_account" "vm-sa" { } resource "azurerm_virtual_machine" "vm-linux" { - count = ! contains(tolist([var.vm_os_simple, var.vm_os_offer]), "WindowsServer") && ! var.is_windows_image ? var.nb_instances : 0 - name = "${var.vm_hostname}-vmLinux-${count.index}" - resource_group_name = data.azurerm_resource_group.vm.name - location = coalesce(var.location, data.azurerm_resource_group.vm.location) - 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 + count = ! contains(tolist([var.vm_os_simple, var.vm_os_offer]), "WindowsServer") && ! var.is_windows_image ? var.nb_instances : 0 + name = "${var.vm_hostname}-vmLinux-${count.index}" + resource_group_name = data.azurerm_resource_group.vm.name + location = coalesce(var.location, data.azurerm_resource_group.vm.location) + 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 + delete_data_disks_on_termination = var.delete_data_disks_on_termination dynamic identity { for_each = length(var.identity_ids) == 0 && var.identity_type == "SystemAssigned" ? [var.identity_type] : [] diff --git a/test/fixture/main.tf b/test/fixture/main.tf index 6774799..a8e010f 100644 --- a/test/fixture/main.tf +++ b/test/fixture/main.tf @@ -104,16 +104,17 @@ module "debianservers" { } module "debianservers2" { - source = "../../" - vm_hostname = "${random_id.ip_dns.hex}-d2" - resource_group_name = azurerm_resource_group.test.name - location = var.location_alt - admin_username = var.admin_username - vm_os_simple = var.vm_os_simple_2 - vnet_subnet_id = azurerm_subnet.subnet2.id - enable_ssh_key = true - ssh_key = "" - ssh_key_values = ["ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC8GIRF1Snlg9NKCmM74RHXqRGMXyui088+ntQqkQkFIL/BrlgP3CzOgHQmJ+3f0Up/+9UY9vX7AmT7WxVTyqBHT/Aes3VmU3wLO5/MMV/HRrT4z2QV/80futhxjk2unNdWGvbFcR6Y3I44EJFmr8GMbyXRtr0ibuv8BlTYx/K6AXSJ3V+kBqXMOF1QRvVoX9fJKPKjMsebe0cB1IYlm9KLqtciMy+aFOEsSNfrw5cNVsQfK3BgOUKAHsLfBiR7imA2ca+hh005GEtcVJvpvFzcM+bZggUpdqQwIzk1Kv/tROiJiGS0NnyzoxIZYeM3z/mQ5qnglp+174XGCG66EAnVdf5kbaI0Iu7FpAmVhJ92N+MNKoP6vT8cMkYYZf3RaiMMnzjswK/VLbb5ks6Qe9qEPXW1IBtkaaF7+0PCWbPr86I0G2bOa2tFyOHm046Z9sRlkaOO95hmer6Y6MUbMpfeprmjR87u6MVOPglnARfV3UI9i6wOUhVVIi6Wb424HWU="] + source = "../../" + vm_hostname = "${random_id.ip_dns.hex}-d2" + resource_group_name = azurerm_resource_group.test.name + location = var.location_alt + admin_username = var.admin_username + vm_os_simple = var.vm_os_simple_2 + vnet_subnet_id = azurerm_subnet.subnet2.id + enable_ssh_key = true + delete_data_disks_on_termination = true + ssh_key = "" + ssh_key_values = ["ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC8GIRF1Snlg9NKCmM74RHXqRGMXyui088+ntQqkQkFIL/BrlgP3CzOgHQmJ+3f0Up/+9UY9vX7AmT7WxVTyqBHT/Aes3VmU3wLO5/MMV/HRrT4z2QV/80futhxjk2unNdWGvbFcR6Y3I44EJFmr8GMbyXRtr0ibuv8BlTYx/K6AXSJ3V+kBqXMOF1QRvVoX9fJKPKjMsebe0cB1IYlm9KLqtciMy+aFOEsSNfrw5cNVsQfK3BgOUKAHsLfBiR7imA2ca+hh005GEtcVJvpvFzcM+bZggUpdqQwIzk1Kv/tROiJiGS0NnyzoxIZYeM3z/mQ5qnglp+174XGCG66EAnVdf5kbaI0Iu7FpAmVhJ92N+MNKoP6vT8cMkYYZf3RaiMMnzjswK/VLbb5ks6Qe9qEPXW1IBtkaaF7+0PCWbPr86I0G2bOa2tFyOHm046Z9sRlkaOO95hmer6Y6MUbMpfeprmjR87u6MVOPglnARfV3UI9i6wOUhVVIi6Wb424HWU="] depends_on = [azurerm_resource_group.test] } diff --git a/variables.tf b/variables.tf index 99c9972..79355db 100644 --- a/variables.tf +++ b/variables.tf @@ -160,6 +160,12 @@ variable "delete_os_disk_on_termination" { default = false } +variable "delete_data_disks_on_termination" { + type = bool + description = "Delete data disks when machine is terminated." + default = false +} + variable "data_sa_type" { description = "Data Disk Storage Account type." type = string