Hi,
I am trying to use the dynamic blocks introduced in version 0.12. With this vesion it is possible, for example, to declare a unique resource to create virtual machines (windows / linux).
The problem I’m having is how to declare the condition to know what kind of machine to create, I have tried several options but all get the same problem.
I do not understand what is happening, also if I declare the value I need directly with locals there are no problems, but doing this does not make sense.
Any help is well come
Thank you!!
Issue description:
In the example that follows, if I use this condition I get an error because it tries to create the two dynamic blocks, even when the result of “os_win” is “true” (it is being evaluated well):
locals {
os_win = substr(lookup (var.image, "publisher", "value_not_declared"), 0, 9) == "Microsoft" ? true: false
dynamic_linux = ! local.os_win ? {dummy_create = true}: {}
dynamic_windows = local.os_win ? {dummy_create = true}: {}
}
If I declare it manually (it’s not a variable), it works correctly, but as I said earlier, it does not make sense to do so.
locals {
os_win = false
dynamic_linux = ! local.os_win ? {dummy_create = true}: {}
dynamic_windows = local.os_win ? {dummy_create = true}: {}
}
All code:
terraform -version
Terraform v0.12.5
+ provider.azurerm v1.31.0
# variable "image" {
# type = map
# default = {
# publisher = "Canonical"
# offer = "UbuntuServer"
# sku = "16.04-LTS"
# version = "latest"
# }
# }
variable "image" {
type = map
default = {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2016-Datacenter"
version = "latest"
}
}
locals {
os_win = substr(lookup(var.image, "publisher", "value_not_declared"), 0, 9) == "Microsoft" ? true : false
#os_win = true
dynamic_linux = ! local.os_win ? { dummy_create = true } : {}
dynamic_windows = local.os_win ? { dummy_create = true } : {}
}
resource "azurerm_resource_group" "main" {
name = "testjc-resources"
location = "West US 2"
}
resource "azurerm_virtual_network" "main" {
name = "testjc-network"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
}
resource "azurerm_subnet" "internal" {
name = "internal"
resource_group_name = azurerm_resource_group.main.name
virtual_network_name = azurerm_virtual_network.main.name
address_prefix = "10.0.2.0/24"
}
resource "azurerm_network_interface" "main" {
name = "testjc-nic"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
ip_configuration {
name = "testconfiguration1"
subnet_id = azurerm_subnet.internal.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_virtual_machine" "main" {
name = "testjc-vm"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
network_interface_ids = [azurerm_network_interface.main.id]
vm_size = "Standard_DS1_v2"
storage_image_reference {
publisher = lookup(var.image, "publisher")
offer = lookup(var.image, "offer")
sku = lookup(var.image, "sku")
version = lookup(var.image, "version")
}
storage_os_disk {
name = "myosdisk1"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = "hostname"
admin_username = "testadmin"
admin_password = "Password1234!"
}
dynamic "os_profile_linux_config" {
for_each = local.dynamic_linux
content {
disable_password_authentication = false
}
}
dynamic "os_profile_windows_config" {
for_each = local.dynamic_windows
content {
enable_automatic_upgrades = false
provision_vm_agent = true
}
}
}
Results:
terraform plan
Error: "os_profile_windows_config": conflicts with os_profile_linux_config
on main.tf line 81, in resource "azurerm_virtual_machine" "main":
81: resource "azurerm_virtual_machine" "main" {
Error: "os_profile_linux_config": conflicts with os_profile_windows_config
on main.tf line 81, in resource "azurerm_virtual_machine" "main":
81: resource "azurerm_virtual_machine" "main" {