azurepowershellazure-devopspacker

Running multiple Packer Powershell provisioners with the same credentials in Azure


My Packer template has several powershell provisioners that rely on scripts from an Azure Files network share. The share is mapped to the VM using the following sequence

  "$securePass = ConvertTo-SecureString \"${var.storage_account_key}\" -AsPlainText -Force",
  "$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList \"${var.storage_account_name}\",$securePass",
  "New-PSDrive -Credential $creds -Name F -PSProvider FileSystem -Root \"${var.storage_path}\" | Out-Null",

There are reboots between many of the powershell provisioners using the windows-restart provisioner in order for changes to be available for future steps. Once a reboot has completed, I'm remapping the share using the same sequence. This is, of course, redundant and clunky.

I imagine I can copy all of my scripts to the VM and execute them from there without needing to map a drive each time. That would certainly declutter the template a bit and just leave me with the task of cleaning up the scripts afterward or tucking them away somewhere on the image for future reference.

Out of curiosity, though, and because I like to just try things whether practical or not, is there another way I can perform the actions of the three lines of code without reentering them each time I need to map the drive? Is there a way, I suppose, to create some kind of function in a Packer template? Or maybe a way to pass a snippet of code to a template from the Azure DevOps pipeline and have it interpolated into the template?


Solution

  • Turns out I was overthinking this. I've been mapping the file share after a reboot when what I should have been doing is copying the files to the VM and running them from there. Only one mapping is required at the start of the build block.