Azure Devops & Custom scale set images

We have on this website previously covered using scale set agents in Azure Devops for running your pipelines. They use Azure Virtual Machine Scale Set (VMSS) in the background and require you to configure this resource in the Azure Portal.
This is beneficial if you want to make use of the scaling available in VMSS where you could tell your agent pool in Azure Devops to reduce the amount of idle agents to 0 when no jobs are queued, saving on consumption.
What happens if the marketplace image we are using when deploying our VMSS resource does not contain everything we need? For instance, maybe I need some tools installed that are not included in the Ubuntu image per default.
No problem, just like how you can deploy virtual machines with custom images you can do so as well with VMSS. In this post today I will demonstrate the setup process we can make use of and ensure in Azure Devops that we are running pipeline with scale set agents that has an image customized to our specific need. We will create a virtual machine from the marketplace and perform some changes to it that will be the basis of our new custom image.
Demo
- First off we need to create our resource group
az group create \
--name rg-prod-we-custom \
--location westeurope

- Next we will create the VM that we will customize and base our image off
az vm create \
--name vm-prod-we-custom \
--resource-group rg-prod-we-custom \
--image Ubuntu2204 \
--os-disk-size 128 \
--admin-username adminuser \
--admin-password P@ssword123! \
--security-type 'Standard'

- Take note of the public IP and sign into the machine with SSH
ssh adminuser@<pip>

- This is where we will install the tools we require. In this example we will pretend that we need Powershell installed to be able to use commands such as
New-PSSession
and more in our pipelines. - In addition to installing Powershell we will install some Powershell modules to illustrate that we can also perform configuration of software and bring that with us into our custom images running in Scale sets
- I will use VIM to create a script that will install Powershell and the module, you can find the script here, use whatever editor you like:
###################################
# Prerequisites
# Update the list of packages
sudo apt-get update
# Install pre-requisite packages.
sudo apt-get install -y wget
# Download the PowerShell package file
wget - https://github.com/PowerShell/PowerShell/releases/download/v7.2.22/powershell_7.2.22-1.deb_amd64.deb
###################################
# Install the PowerShell package
sudo dpkg -i powershell_7.2.22-1.deb_amd64.deb
# Resolve missing dependencies and finish the install (if necessary)
sudo apt-get install -f
# Delete the downloaded package file
rm powershell_7.2.22-1.deb_amd64.deb
# Install required modules for Powershell
sudo pwsh -Command 'Install-Module -Name PSWSMan -Force'
sudo pwsh -Command 'Install-WSMan'
# Start powershell
pwsh
- I will enter the following commands:
vim script.sh
(paste and save the script)
sh script.sh
- If successful you should see something similar to this:

- We have installed Powershell and the WSMan module
Complete the configuration
Once we are finished we need to generalize the VM with the following command inside our shell: sudo waagent -deprovision+user -force
- run this command and wait approximately 45 minutes.

- Deallocate the VM
az vm deallocate -g rg-prod-we-custom -n vm-prod-we-custom --no-wait

- Mark the VM as generalized
az vm generalize -g rg-prod-we-custom -n vm-prod-we-custom
Now to create the image we will run the following command in our terminal
az image create \
--resource-group rg-prod-we-custom \
--name vm-prod-we-custom \
--source vm-prod-we-custom \
--hyper-v-generation "V2"


Now we will use this image as the base for our new scaleset:
az vmss create \
--resource-group rg-prod-we-custom \
--name vmss-prod-we-custom \
--image vm-prod-we-custom \
--admin-username adminuser \
--admin-password P@ssword123! \
--instance-count 2 \
--upgrade-policy-mode manual \
--orchestration-mode Uniform \
--single-placement-group true


Now we are ready to complete the configuration of the agent pool in Azure Devops, for information on how to do this please visit a previous post I made:

References

About me
