Terraform-如果某些值为false,则跳过创建“ storage_data_disk” Azure资源

时间:2020-02-26 03:18:05

标签: terraform

我在模块中有以下代码来创建VM:

## Create VMs
resource "azurerm_virtual_machine" "vm" {
  count               =  var.is_windows_vm == "yes" ? var.instance_numbers : 0
  name                  = "${var.team_name}_${var.env_name}_${var.release}_${count.index}"
  location              = var.loc
  resource_group_name   = "${var.team_name}_${var.env_name}_${var.release}"
  network_interface_ids = [element(azurerm_network_interface.net_int.*.id, count.index)]
  vm_size               = var.vm_size

## Delete OS and data disks when VMs are deleted
  delete_os_disk_on_termination = true
  delete_data_disks_on_termination = true

## What OS to use
storage_image_reference {
  publisher = var.storage_image_publisher
  offer     = var.storage_image_offer
  sku       = var.storage_image_sku
  version   = var.storage_image_version
}

## Create OS disk for VM
storage_os_disk {
  name              = "osdisk${count.index}"
  caching           = "ReadWrite"
  create_option     = "FromImage"
  managed_disk_type = var.disk_type
  os_type           = var.os_type
}

## Create data disk for VM
storage_data_disk {
   name              = "datadisk_${count.index}"
   managed_disk_type = var.disk_type
   create_option     = "Empty"
   lun               = 0
   disk_size_gb      = var.disk_size_gb
 }

## VM profile
os_profile {
   computer_name  = "${var.team_name}-${var.env_name}-${var.release}-${count.index}"
   admin_username = var.vm_username
   admin_password = var.vm_password
 }

 os_profile_windows_config {
   enable_automatic_upgrades = false
 }

如果变量设置为false,我想跳过storage_os_disk的创建,因此该模块可用于不需要额外磁盘的VM。

可能是这样的:

variable "additional_data_disk" {
    description = "Specify if an additional Data Disks should be created for each VM"
    default = false
}

## Create data disk for VM
dynamic "storage_data_disk" {
for_each = var.additional_data_disk ? ["data"] : []
content {
  name = "datadisk_${count.index}"
  managed_disk_type = var.disk_type
  create_option = "Empty"
  lun = 0
  disk_size_gb = var.disk_size_gb
  caching = "ReadWrite"
  }
}

但我无法使其正常运行。 Terraform计划仍显示正在创建的数据磁盘。

0 个答案:

没有答案