Using CLI to Capture and Deploy VMs
Capturing and deploying Virtual Machines is a routine job when we work with IaaS Dev and Test environments on Azure. If you’ve worked with the Azure PowerShell CmdLets in the past, this will feel quite familiar.
Let’s dive in!
Before we start, it’s important to have the Azure CLI installed on our machine.
There are few ways to install the Azure CLI.
- Using an installer
- Installing Node.js and npm and then using the npm install command
- Run Azure CLI as a Docker container
Once the Azure CLI has been installed, we are able to use the azure command from a command-line interface (Bash, Terminal, Command prompt) to access the Azure CLI commands.
Preparing The CentOS Virtual Machine
For this example, I used a CentOS 7.1 Azure Virtual Machine. Use the Azure portal to find out which public port is configured as the SSH port. Then use your favorite SSH client to open an SSH Session. In this post, I will be using PuTTY.

Now, its time to prepare the Linux Virtual Machine Image capture.
- Connect to the virtual machine using an SSH client of your choice.
- In the SSH window, type the following command. Note that the output from waagent may vary slightly depending on the version of this utility:
sudo waagent -deprovision
- This command will attempt to clean the system and make it suitable for re-provisioning. This operation performs the following tasks:
- Removes SSH host keys (if Provisioning.RegenerateSshHostKeyPair is ‘y’ in the configuration file)
- Clears nameserver configuration in /etc/resolv.conf
- Removes the root user’s password from /etc/shadow (if Provisioning.DeleteRootPassword is ‘y’ in the configuration file)
- Removes cached DHCP client leases
- Resets host name to localhost.localdomain
- Deletes the last provisioned user account (obtained from /var/lib/waagent) and associated data.
- Type y to continue.
- Type Exit to close the SSH client.
Deprovisioning deletes files and data in an effort to “generalize” the image. Only run this command on virtual machines that you intend to capture as a new image template. The produced image is not cleared of all sensitive information and many not be suitable for redistribution to third parties.
Configuring The Azure CLI Session
Using a command-line interface (Bash, Terminal, Command prompt), access the Azure CLI commands to interact with Azure. These will allow you to configure the Azure CLI session that will permit us to authenticate to the desired Azure Subscription.
azure config mode asm azure account list info: Executing command account list data: Name Id Tenant Id Current data: ------------------------------------ ------------------------------------ --------- ------- data: Microsoft Azure Internal Consumption 64668628-d5e6-4c5b-9be4-9e577d5b8fd0 undefined true info: account list command OK azure account download info: Executing command account download info: Launching browser to http://go.microsoft.com/fwlink/?LinkId=254432 help: Save the downloaded file, then execute the command help: account import <file> info: account download command OK azure account import 'C:\Users\albriseb\Downloads\Free Trial-8-19-2015-credentials.publishsettings' info: Executing command account import info: account import command OK azure account list info: Executing command account list data: Name Id Tenant Id Current data: ------------------------------------ ------------------------------------ --------- ------- data: Free Trial c14fc8bb-3180-4844-965b-7cd9d90cf7cd undefined false info: account list command OK azure account set c14fc8bb-3180-4844-965b-7cd9d90cf7cd info: Executing command account set info: Setting subscription to "Free Trial" with id "c14fc8bb-3180-4844-965b-7cd9d90cf7cd". info: Changes saved info: account set command OK
Capturing The Virtual Machine Image
Using the Azure CLI session, shutdown the Azure Virtual Machine and capture it as a brand new Virtual Machine Image.
azure vm shutdown 'centos-classic' info: Executing command vm shutdown info: Getting virtual machines info: Shutting down VM info: vm shutdown command OK azure vm capture -t 'centos-classic' 'centos-classic-img' info: Executing command vm capture info: Getting virtual machines info: Checking image with name centos-classic-img exists info: Capturing VM info: vm capture command OK
Deploying the Virtual Machine Image
Verify that our Virtual Machine Image is available and deploy it.
azure vm image list info: Executing command vm image list info: Fetching VM images data: Name Category OS data: ------------------------------------- -------- ------- data: centos-classic-copy User Linux data: centos-classic-img User Linux info: vm image list command OK azure vm create -e -l 'East US 2' 'centos-classic-img-inst' 'centos-classic-img' 'brisebois' '1Microsoft!' info: Executing command vm create warn: --vm-size has not been specified. Defaulting to "Small". info: Looking up image centos-classic-img info: Looking up cloud service info: cloud service centos-classic-img-inst not found. info: Creating cloud service info: Creating VM info: vm create command OK