Search code examples
azureterraformterraform-provider-azure

Receiving error from Azure API when attempting to use additional_unattend_content argument


Hoping someone might be able to assist. Using Terraform on Azure and looking for a method to deploy windows VMs and auto-login + configure winrm. I’ve found that some use the azurerm_windows_virtual_machine.<name_of_vm>.additional_unattend_content to set this up normally.

Example found in provider github repo: https://github.com/hashicorp/terraform-provider-azurerm/blob/b0c897055329438be6a3a[…]ned-to-active-directory/modules/active-directory-domain/main.tf

I’m getting some errors from the azure backend and was hoping maybe someone more knowledgeable than me would have experience with this. Getting pushback from Azure support when I requested their help. Appreciate any info anyone can provide!

Happy to provide logs or anything else thats needed!!!

resource "azurerm_windows_virtual_machine" "wks_win10" {
  count                           = var.number_of_win10_wks
  depends_on                      = [azurerm_network_interface.wks_nic_win10]
  name                            = "wks-win10-${count.index}" 
  location                        = var.location
  resource_group_name             = var.rg_name
  size                            = var.vm_size
  provision_vm_agent              = true
  computer_name                   = "wks-win10-${count.index}"
  admin_username                  = var.windows_username
  admin_password                  = var.windows_password
  network_interface_ids           = ["${element(azurerm_network_interface.wks_nic_win10.*.id, count.index)}"]
  os_disk {
    caching                       = "ReadWrite"
    name                          = "wks-win10-osdisk-${count.index}"
    disk_size_gb                  = "250"
    storage_account_type          = "StandardSSD_LRS"
  }
  source_image_reference {
    publisher = "MicrosoftWindowsDesktop"
    offer     = "Windows-10"
    sku       = "win10-21h2-ent"
    version   = "latest"
  }
  additional_unattend_content {
    setting = "AutoLogon"
    content = local.auto_logon_data
  #  content = "<AutoLogon><Password><Value>${var.windows_password}</Value></Password><Enabled>true</Enabled><LogonCount>3</LogonCount><Username>${var.windows_username}</Username></AutoLogon>"
  }

  winrm_listener {
    protocol = "Http"
  }
  tags = merge(var.tags,
    {
    "kind"="workstation"
    "os"="windows"
    })
  }

resource "azurerm_virtual_machine_extension" "wks_win10_vm_extension_network_watcher" {
  count                      = var.number_of_win10_wks
  depends_on                 = [azurerm_windows_virtual_machine.wks_win10]
  name                       = "win10netwatch${count.index}"
  virtual_machine_id         = "${element(azurerm_windows_virtual_machine.wks_win10.*.id, count.index )}"
  publisher                  = "Microsoft.Azure.NetworkWatcher"
  type                       = "NetworkWatcherAgentWindows"
  type_handler_version       = "1.4"
  auto_upgrade_minor_version = true
}

Errors:

module.compute.azurerm_network_interface.wks_nic_win10[0]: Creation complete after 1s [id=/subscriptions/<subscription-id>/resourceGroups/test-rg/providers/Microsoft.Network/networkInterfaces/wks-win10-nic-0]

module.compute.azurerm_windows_virtual_machine.wks_win10[0]: Creating...

2022-11-23T16:09:12.176-0500 [ERROR] provider.terraform-provider-azurerm_v3.30.0_x5: Response contains error diagnostic: @module=sdk.proto diagnostic_detail= diagnostic_severity=ERROR diagnostic_summary="creating Windows Virtual Machine: (Name "wks-win10-0" / Resource Group "test-rg"): compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="InvalidParameter" Message="The value of parameter windowsConfiguration.additionalUnattendContent.content is invalid." Target="windowsConfiguration.additionalUnattendContent.content"" @caller=github.com/hashicorp/[email protected]/tfprotov5/internal/diag/diagnostics.go:56 tf_provider_addr=provider tf_req_id=6a628786-49b2-388d-85f9-07e4eeb8a618 tf_resource_type=azurerm_windows_virtual_machine tf_rpc=ApplyResourceChange tf_proto_version=5.2 timestamp=2022-11-23T16:09:12.176-0500

2022-11-23T16:09:12.181-0500 [ERROR] vertex "module.compute.azurerm_windows_virtual_machine.wks_win10[0]" error: creating Windows Virtual Machine: (Name "wks-win10-0" / Resource Group "test-rg"): compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="InvalidParameter" Message="The value of parameter windowsConfiguration.additionalUnattendContent.content is invalid." Target="windowsConfiguration.additionalUnattendContent.content"

Solution

  • I tried to reproduce the scenario in my environment:

    Terraform code:

    resource "azurerm_windows_virtual_machine" "example" {
      name                  = "kaacctvm"
      location              = data.azurerm_resource_group.example.location
      resource_group_name   = data.azurerm_resource_group.example.name
      network_interface_ids = [azurerm_network_interface.example.id]
      size               = "Standard_F2"
      admin_username = "txxxin"
       admin_password = "Pasxxx4!"
    
      os_disk {
        caching              = "ReadWrite"
        storage_account_type = "Standard_LRS"
        
      }
      
    
      os_profile {
        computer_name  = "hostname"
        admin_username = "txxxmin"
        admin_password = "gfgxx4!"
      }
    
    
     source_image_reference {
        publisher = "MicrosoftWindowsDesktop"
        offer     = "Windows-10"
        sku       = "win10-21h2-ent"
        version   = "latest"
      }
    additional_unattend_content {
        setting = "AutoLogon"
     content = "<AutoLogon><Password><Value>${var.windows_password}</Value></Password><Enabled>true</Enabled><LogonCount>3</LogonCount><Username>${var.windows_username}</Username></AutoLogon>"
      }
    
       winrm_listener {
        protocol = "Http"
      }
    
      tags = {
        environment = "staging"
      }
    }
    
    resource "azurerm_virtual_machine_extension" "example" {
      name                 = "kavyahostname"
      virtual_machine_id   = azurerm_windows_virtual_machine.example.id
      publisher            ="Microsoft.Azure.NetworkWatcher"
      type                       = "NetworkWatcherAgentWindows"
      type_handler_version       = "1.4"
      auto_upgrade_minor_version = true
    
      settings = <<SETTINGS
     {
      "commandToExecute": "hostname && uptime"
     }
    SETTINGS
    
    
      tags = {
        environment = "Production"
      }
    }
    

    Received similar error: VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="InvalidParameter" Message="The value of parameter “” is invalid."

    Make sure the contents in the username and password are of correct format here while calling autologon data .

    content = "<AutoLogon><Password><Value>${var.windows_password}</Value></Password><Enabled>true</Enabled><LogonCount>3</LogonCount><Username>${var.windows_username}</Username></AutoLogon>"
    

    Please check azure-quickstart-templates| issues | github

    • It can be array which can not be base64 encoded
    • Check with the colon mistakes or spelling mistakes

    I have variables:

    variable "windows_username" {
      type = string
      default = "xxx"
    }
    
    
    variable "windows_password" {
      type = string
      default = "xxx"
    }
    

    Then vm extension created sucessfully:

    enter image description here

    Also check this Microsoft.Compute/virtualMachines - Bicep, ARM template & Terraform AzAPI reference | Microsoft Learn