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.

  1. Using an installer
  2. Installing Node.js and npm and then using the npm install command
  3. 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.

  1. Connect to the virtual machine using an SSH client of your choice.
  2. 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
  3. 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.
  4. Type y to continue.
  5. 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
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


Trackbacks and Pingbacks:

  1. Disk UUIDs and Linux on Azure « Alexandre Brisebois ☁ - September 1, 2015

    […] recently ran a few tests to figure out how UUIDs behave on Azure when we capture VM Images, copy VHDs, move Virtual Machines and re-provision them in different data […]


  2. Luper's Learnings - Azure Technical Community for Partners (September 2015) - Luper’s Learnings - Site Home - TechNet Blogs - September 16, 2015

    […] More on the Azure CLI, Alexandre Brisebois posted a blog on Using CLI to Capture and Deploy VMs. […]


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.