Automate Everything. That’s my new mantra, and it should be yours…

Like many of you, I’m an infrastructure guy and grew up with the crutches of setup.exe and the massive installers that MSFT built in the late 90’s and 2000’s. But, that was then, and today all of us need to become DevOps engineers! It used to be when we built servers they would have a lifespan of many years, but now there is a new type of VM that might only live for a day or even less.

The concept of deleting a server would have scared the daylights out of me in 2002!? Yikes!

In this new world of Azure, we should be building VMs that are purpose-built and automated in their deployment end to end. We want the teams that are consuming these servers to be ready to work as soon as they login.

Windows, Linux, and Azure provide us with many tools to make that happen such as ARM templates, PowerShell or Yum and Apt on Linux. These tools can work together with the custom script extension for Windows or Linux to build out our VMs.

Apt or Yum on Linux works with packages that are built for either Debian or Fedora based distros. You can run simple commands from bash to install software. For example, if you wanted to install Apache or NGINX on Ubuntu you can run one line and after only a few seconds you have a web server.

sudo apt-get install -y apache

sudo apt-get install -y nginx

With Windows Server, you can also install a web server with one command in PowerShell.

Install-WindowsFeature -name Web-Server -IncludeManagementTools

Here is the difference: this command only works for Windows Features. So, you can’t install a different web server with only one line of code right out of the box. I’m not saying you can’t install apache with a PowerShell script, a quick search on GitHub and you can find a repo that someone has built, but it’s not one-line.

In walks Chocolatey…

So, the solution to your problem is Chocolatey, which is a package manager for windows. This software and community is amazing. Chocolatey uses PowerShell and existing installers together to build a way to manage your software including dealing with dependencies. It also works nicely with other configuration managers such as DSC, CHEF, Ansible, Puppet, etc.

Using Chocolatey with Azure VMs 1

Chocolatey works with Chef, Puppet, DSC and other configuration managers

According to the site, “Chocolatey packages are not just fancy zip files with PowerShell, they understand versioning and dependencies, allowing you to encapsulate everything related to managing installation and configuration for a piece of software.”

Using Chocolatey with Azure VMs 2

Chocolatey Packages

There are thousands of software packages. For example, after installing Chocolatey on your Windows VM box you could run the following command to install Firefox.

choco install -y firefox
Using Chocolatey with Azure VMs 3

Installing Firefox with Chocolatey in 15 seconds

What’s so great is how amazingly fast the software installs! The install that you see above took about 15 seconds!

I have no idea how they are doing this, but you can thank Rob Reynolds, the creator, and maintainer of Chocolatey (whom I met, by chance, in the elevator of a Tulsa Residence Inn at about 06:30, he wasn’t nearly as excited to see me as I was to see him)!

The Chocolatey packages are maintained by the community and there is just about anything you can think of to run on your machines.

Using Chocolatey with Azure VMs 4

Firefox Package from Chocolatey

So, back to our problem of installing a non-windows feature on a Windows VM with only one line of code. With Chocolatey installed on the server you can just simply run the following line.

choco install -y apache-httpd

Not only does Chocolatey install Apache for us, but before that, it pulled down and installed the required hotfixes for Windows! Are you serious!? Pulling down the hotfixes is like the holy-grail. This is amazing because this was the type of thing, as admins, we would have to figure out. What even are these hotfixes? Then you would have to manually install them, one by one, all before getting to our Apache installer. Each of these actions might have a forced reboot by Windows as well. It could easily take hours to install this software

Using Chocolatey with Azure VMs 5

Chocolately installing the required Hotfixes for Windows, prior to the Apache web server. This is the holy-grail for IT admins.

Automating Azure VMs

Now, that you have been introduced to Chocolatey let’s use this with our Azure VMs. This is where we take these different technologies and put them together.

So, let’s start with a real-world scenario: What if the Development Team came to you and said they need to be able to click one button and deploy a Development Machine that is isolated from production with all the software their developers required? Their goal is to be able to spin up new Dev boxes, in Azure, at a minute’s notice without asking your team or installing any software on their own.

Developer Software:

  • Visual Studio Community with Azure SDK and ASP.NET
  • Visual Studio Code
  • Git
  • Google Chrome

Now we have a challenge! To make this work will use an ARM template to build our VM, along with its dependencies, and then install the software we need using Chocolatey. To do this, we will use the custom script extension for PowerShell. The key for our installs will be to select the correct packages and make sure that the script has the package names. That is pretty easy, just browse the package library and you will see the names.

Using Chocolatey with Azure VMs 6

Find the Package Names for your PowerShell

First, let’s write the script that will be called by the ARM template. We will need to do the following things in the script:

  1. Install Chocolatey
  2. Run the choco installer for each package
  3. Reboot
Using Chocolatey with Azure VMs 7

PowerShell to install Chocolatey and Packages

Next, we need a simple ARM template that will call this script from a URI. We will put all this code into GitHub and then when Azure runs the deployment will just reach out over the Internet and pick up that script from our repo. In this case, the script is called InstallCountChocula.ps1.

Using Chocolatey with Azure VMs 8

ARM Template to build the VM, VNET and run the script extension

Once this is all built and checked into a GitHub repo, then developers will just click the Deploy to Azure Button and off to the races!

Using Chocolatey with Azure VMs 9

Developers can just click this one button

The only configuration would be to update the DNS name and provide a Resource Group.

Using Chocolatey with Azure VMs 10

Update the DNS Name (lowercase only ) and Resource Group name.

After the deployment is complete, the developers will use the Azure portal to connect to their VM. All of the software they requested will be have been installed Chocolatey and ready!

The username and password for this are embedded in the code:

Username: stormtrooperio

Password: Password.1!!

I hope this helps you get started with Chocolatey. Feel free to fork the CountChocula repo and have fun!

Using Chocolatey with Azure VMs 11

CountChocula Repo on Github

@deltadan

Dan Patrick is the Chief Infrastructure Architect for Solliance and a 15 year veteran at Microsoft. He has an extensive background in IT Infrastructure and Operations. Dan has both architected and lead teams building and supporting some of the largest service providers in North America with as many 15,000 Windows Servers and 120 million endpoints. Dan has worked with Azure IaaS solutions extensively since 2012. He has a passion for Virtualization with deep experience leveraging Hyper-V, Vmware, and Citrix. He is also a Clustering specialist focusing on large host clusters and SQL Always On Availability Groups. Recently Dan, authored the Networking, Azure Active Directory and Containers portion of the 70-533 Exam Reference for Microsoft Press. You can follow him on Twitter @deltadan
Microsoft MVP

Discover more from Build5Nines

Subscribe now to keep reading and get access to the full archive.

Continue reading