Hashicorp Packer azure-arm build both VHD and Managed Image with one source. Error "Either a VHD or a managed image can be built"

Viewed 19

I'm still learning packer, but have been able to produde a Managerd Image and VHD sepeartley by commenting out different parts of the code. How would I get around this error:

Error: 2 error(s) occurred:

* Either a VHD or a managed image can be built, but not both. Please specify either capture_container_name and capture_name_prefix or managed_image_resource_group_name and managed_image_name.
* Specify either a VHD (storage_account and resource_group_name) or Managed Image (managed_image_resource_group_name and managed_image_name) output

  on windows_server2019.pkr.hcl line 10:
  (source code not available)



==> Wait completed after 0 seconds

==> Builds finished but no artifacts were created.

Code:

packer {
  required_plugins {
    windows-update = {
      version = "0.14.1"
      source  = "github.com/rgl/windows-update"
    }
  }
}

source "azure-arm" "server_2019" {
  use_azure_cli_auth                               = true
  azure_tags                                       = local.tags
  build_resource_group_name                        = var.resource_group_name
  build_key_vault_name                             = "EXAMPLE-RGP-${var.environment}-KVT"
  os_type                                          = local.azure_sku.os_type
  image_publisher                                  = local.azure_sku.image_publisher
  image_offer                                      = local.azure_sku.image_offer
  image_sku                                        = local.azure_sku.image_sku
  vm_size                                          = local.azure_sku.vm_size
  shared_gallery_image_version_exclude_from_latest = false
  virtual_network_resource_group_name              = local.network.virtual_network_resource_group_name
  virtual_network_name                             = local.network.virtual_network_name
  virtual_network_subnet_name                      = local.network.virtual_network_subnet_name
  private_virtual_network_with_public_ip           = false
  communicator                                     = "winrm"
  winrm_use_ssl                                    = true
  winrm_insecure                                   = true
  winrm_timeout                                    = "3m"
  winrm_username                                   = "winrm"
  winrm_password                                   = var.winrm_password

  # VHD (Both VHD and Managed image can't be done at the same time, comment one of them out)
  resource_group_name    = var.resource_group_name
  storage_account        = lower("examplestorage${var.environment}blobevgimg")
  capture_container_name = "evergreen-images"
  capture_name_prefix    = "packer_server2019"

  # Managed Image (Both VHD and Managed image can't be done at the same time, comment one of them out)
  managed_image_name                 = "packer_server2019"
  managed_image_resource_group_name  = var.resource_group_name
  managed_image_storage_account_type = "Standard_LRS"
  shared_image_gallery_destination {
    resource_group       = var.resource_group_name
    gallery_name         = upper("GALLERY${var.environment}EVERGREENIMAGES")
    image_name           = "Evergreen-Images-Windows"
    image_version        = formatdate("YYYY.MM.DD", timestamp())
    replication_regions  = ["australiaeast", "australiasoutheast"]
    storage_account_type = "Standard_ZRS"
  }
}

build {
  sources = [
    "sources.azure-arm.server_2019",
  ]

  provisioner "file" {
    source      = "../scripts/ConfigureRemotingForAnsible.ps1"
    destination = "C:\\temp\\ConfigureRemotingForAnsible.ps1"
  }

  provisioner "windows-update" {
    search_criteria = "IsInstalled=0"
    filters = [
      "exclude:$_.Title -like '*Preview*'",
      "include:$true",
    ]
    update_limit = 25
  }
}

I know I can accomplish this by having a complelty seperate build file and call it from the CLI directly, but wondering if there is some magic that will allow me to have the same template file and build both a Managed Image and VHD?

1 Answers

Found I can use multiple sources and build each of them. So yes it is possible to build a managed image and vhd.

packer {
  required_plugins {
    windows-update = {
      version = "0.14.1"
      source  = "github.com/rgl/windows-update"
    }
  }
}

source "azure-arm" "server_2019" {
  use_azure_cli_auth                               = true
  azure_tags                                       = local.tags
  build_resource_group_name                        = var.resource_group_name
  build_key_vault_name                             = local.build_key_vault_name
  os_type                                          = local.azure_skus.windows.os_type
  image_publisher                                  = local.azure_skus.windows.image_publisher
  image_offer                                      = local.azure_skus.windows.image_offer
  image_sku                                        = local.azure_skus.windows.image_sku
  vm_size                                          = local.azure_skus.windows.vm_size
  shared_gallery_image_version_exclude_from_latest = false
  virtual_network_resource_group_name              = local.network.virtual_network_resource_group_name
  virtual_network_name                             = local.network.virtual_network_name
  virtual_network_subnet_name                      = local.network.virtual_network_subnet_name
  private_virtual_network_with_public_ip           = false
  communicator                                     = "winrm"
  winrm_use_ssl                                    = true
  winrm_insecure                                   = true
  winrm_timeout                                    = "3m"
  winrm_username                                   = local.login_name
  winrm_password                                   = var.login_password
  managed_image_name                               = local.azure_skus.windows.packer_image_name
  managed_image_resource_group_name                = var.resource_group_name
  managed_image_storage_account_type               = "Standard_LRS"

  shared_image_gallery_destination {
    resource_group       = var.resource_group_name
    gallery_name         = local.gallery_name
    image_name           = local.azure_skus.windows.image_name
    image_version        = formatdate("YYYY.MM.DD", timestamp())
    replication_regions  = ["australiaeast", "australiasoutheast"]
    storage_account_type = "Standard_LRS"
  }
}

source "azure-arm" "server_2019_vhd" {
  use_azure_cli_auth                               = true
  azure_tags                                       = local.tags
  build_resource_group_name                        = var.resource_group_name
  build_key_vault_name                             = local.build_key_vault_name
  os_type                                          = local.azure_skus.windows.os_type
  image_publisher                                  = local.azure_skus.windows.image_publisher
  image_offer                                      = local.azure_skus.windows.image_offer
  image_sku                                        = local.azure_skus.windows.image_sku
  vm_size                                          = local.azure_skus.windows.vm_size
  shared_gallery_image_version_exclude_from_latest = false
  virtual_network_resource_group_name              = local.network.virtual_network_resource_group_name
  virtual_network_name                             = local.network.virtual_network_name
  virtual_network_subnet_name                      = local.network.virtual_network_subnet_name
  private_virtual_network_with_public_ip           = false
  communicator                                     = "winrm"
  winrm_use_ssl                                    = true
  winrm_insecure                                   = true
  winrm_timeout                                    = "3m"
  winrm_username                                   = local.login_name
  winrm_password                                   = var.login_password
  resource_group_name                              = var.resource_group_name
  storage_account                                  = local.storage_account
  capture_container_name                           = "evergreen-images"
  capture_name_prefix                              = local.azure_skus.windows.packer_image_name
}

build {
  sources = [
    "sources.azure-arm.server_2019",
    "sources.azure-arm.server_2019_vhd",
  ]

  provisioner "file" {
    source      = "../../scripts/ConfigureRemotingForAnsible.ps1"
    destination = "C:\\temp\\ConfigureRemotingForAnsible.ps1"
  }

  provisioner "windows-update" {
    search_criteria = "IsInstalled=0"
    filters = [
      "exclude:$_.Title -like '*Preview*'",
      "include:$true",
    ]
    update_limit = 25
  }
}
Related