Condition based on attribute value during plan/apply

Hi,

I’m a little confused about how to achive this using conditionals on Terraform. I have some files to create VMs on Azure, but they was created using different admin_username value in a hard-coded way.

I’m trying to create a tag based on a value of this attribute, but can achieve just when using it as a variable and creating a condition inside of vm file.

There is a way to look on this attribute on each file during terraform plan/apply and create/update tag using a central local.tf file to avoid editing each file?

Example of admin_username attibute values:

file1.tf

resource "azurerm_windows_virtual_machine" "example" {
  name                = "example-machine"
  admin_username      = "adminuser"

file2.tf

resource "azurerm_windows_virtual_machine" "example" {
  name                = "test-machine"
  admin_username      = "localuser"

Thanks

Hi @fsaraiva,

I’m not sure I fully understand your goal, so this answer will be broad and might not actually answer your question. If not, please share more details.

The typical way to vary values between plan/apply rounds is to use input variables:

variable "admin_username" {
  type = string
}

resource "azurerm_windows_virtual_machine" "example" {
  name           = "example-machine"
  admin_username = var.admin_username
}

However, I notice that your name arguments also differ between the two, and so it seems like you’d need multiple input variables to cover all of these differences.