Azure DevOps: Add a VM to a deployment group using the Azure CLI

The other day I was preparing a demo of Azure DevOps in which I use Deployment Groups to deploy an ASP.Net MVC web application to a Windows 2016 Server with IIS. Instead of creating this virtual machine through the portal I wanted to do things programmaticly to be able to easily repeat these steps so I can delete the vm or create more machines. There are two ways of doing this. Using ARM templates or using the Azure Cli. I don’t like ARM templates too much. They tend to get very big, unreadable and are full of little details you need to know of to be able to work with them. So I figured I wanted to see what I could do using the Azure Cli.

Create the resources

There’s a couple of things we need to create. Firts, we need a resource group. You can create one using the following command.

Next, we create the vm itself using the following command.

Since I want to deploy a website on this machine I need to open up port 80. By default only the remote desktop port is open.

The Azure DevOps Agent extension

Last but not lease I need to install the Azure CaptureDevOps agent to use this machine as a deployment target. Luckily, there is a virtual machine extension which does just that. There was one problem however. I could not find any documentation on which parameters I needed to use as shown in the portal. There’s this page (see Extensions Reference at the bottom of the menu) documenting some of the extensions. There’s the following commands which give you information about the extensions.

Non of them however showed me how to use this particular one. After searching the internet for a while I found this answer on GitHub. Although it’s not a direct answer to my problem, it did give enough hints. The following snippet shows the parameters I have to use.

They lead to the following command:

After you’ve run these commands you’ll see this machine pop up in your deployment group ready to use.

Machine configuration

I use the following two scripts to make my machine ready for my application to be deployed. I run them on the machine in a release pipeline. The first one installs same required dependencies. The second one applies a PowerShell DSC configuration to install IIS and some .Net frameworks. You might wonder why I install .Net 3.5. This is a requirement of the Website Deployment Task in Azure DevOps.

 

You may also like...