For several days, I’ve been playing with Vagrant, the popular tool that lets you create any environment based on virtual machines quickly and replicable. It’s anĀ Open Source project that is compatible with Windows, Mac and Linux and that it really facilitates the life of the developer, worrying only about what really matters: the application. On the official website of Vagrant can verify that there are different providers (that will create your virtual machines): Virtual Box, VMware, Docker and AWS. Of course, there is also one for Microsoft Azure and in this post I’ll show how to install and create a virtual machine on Azure using Vagrant.
If you still don’t use it, install Vagrant from the downloads section onĀ the official site. For this post I have used Mac OS X as operating system. Once installed, open a terminal and check that works by launching the command vagrant.
The next thing is to install the provider for Azure. You don’t need anything more than run the following command from theĀ terminal in orderĀ to begin the installation:
[plain]vagrant plugin install vagrant-azure[/plain]
From here, you can already begin to design your project. Create a folder called azure_proyect and access to it so thatĀ will be your proyect’sĀ location.
[plain]mkdir azure_proyect
cd azure_proyect[/plain]
Once there you can begin to configure your box.Ā ThereĀ is a dummyĀ boxĀ that will give you theĀ skeleton of the Vagrantfile file on Github, which describes the type of machine that you are going to configure. Launch theĀ following command to request the box:
[plain]vagrant box add azure https://github.com/msopentech/vagrant-azure/raw/master/dummy.box[/plain]
Finally, to get the required file to configure your project launch theĀ vagrant init command.
[plain]vagrant init[/plain]
If you accessĀ toĀ the folder where you defined yourĀ project forĀ Vagrant (Terminal >Ā openĀ .) you will see that it is alreadyĀ available the Vagrantfile file that you need to configure your credentials forĀ Microsoft Azure, how will be your virtual machine, whichĀ image are going to use, etc.
Modify the file to replace the contents with the following:
[plain]Vagrant.configure(‘2’) do |config|
config.vm.box = ‘azure’
config.vm.provider :azure do |azure, override|
# Mandatory Settings
azure.mgmt_certificate = ‘YOUR_CERTIFICATE.pfx’
azure.mgmt_endpoint = ‘https://management.core.windows.net’
azure.subscription_id = ‘YOUR_SUBSCRIPTION_ID’
azure.vm_image = ‘b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-15_04-amd64-server-20150224.5-beta1-en-us-30GB’
azure.vm_name = ‘ubuntugise’ # max 15 characters. contains letters, number and hyphens. can start with letters and can end with letters and numbers
# vm_password is optional when specifying the private_key_file with Linux VMs
# When building a Windows VM and using WinRM this setting is used to authenticate via WinRM (PowerShell Remoting)
azure.vm_password = ‘YOUR_PASSWORD’ # min 8 characters. should contain a lower case letter, an uppercase letter, a number and a special character
# Optional Settings
#azure.storage_acct_name = ‘NAME OF YOUR STORAGE ACCOUNT’ # optional. A new one will be generated if not provided.
#azure.vm_user = ‘PROVIDE A USERNAME’ # defaults to ‘vagrant’ if not provided
#azure.cloud_service_name = ‘PROVIDE A NAME FOR YOUR CLOUD SERVICE’ # same as vm_name. leave blank to auto-generate
#azure.deployment_name = ‘PROVIDE A NAME FOR YOUR DEPLOYMENT’ # defaults to cloud_service_name
azure.vm_location = ‘West Europe’ # e.g., West US
# Optional *Nix Settings
#azure.ssh_port = ‘A VALID PUBLIC PORT’ # defaults to 22
#azure.private_key_file = ‘Path to your ssh private key file (~/.ssh/id_rsa) to use for passwordless auth. If the id_rsa file is password protected, you will be prompted for the password.’
# Optional Windows Settings
#azure.winrm_transport = [ ‘http’, ‘https’ ] # this will open up winrm ports on both http (5985) and http (5986) ports
#azure.winrm_https_port = ‘A VALID PUBLIC PORT’ # customize the winrm https port, instead of 5986
#azure.winrm_http_port = ‘A VALID PUBLIC PORT’ # customize the winrm http port, insted of 5985
#azure.tcp_endpoints = ‘3389:53389’ # opens the Remote Desktop internal port that listens on public port 53389. Without this, you cannot RDP to a Windows VM.
end
end[/plain]
I’ve leftĀ all optional settings commented, so you can see at a glance all what can be configured. Now we will focus onĀ those that are mandatory for you in this first test with Vagrant and Azure. According to the above script, we need severalĀ mandatory parameters:
- azure.mgmt_certificate:Ā it’s the certificate to connect to your subscription onĀ Microsoft Azure. ItĀ can be in PEM, PFX or raw string format. ToĀ make it easy, download the file of publication of your Microsoft Azure account from here.
[xml]<?xml version="1.0" encoding="utf-8"?>
<PublishData>
<PublishProfile SchemaVersion="2.0" PublishMethod="AzureServiceManagementAPI">
<Subscription ServiceManagementUrl="https://management.core.windows.net" Id="THIS IS YOUR SUBSCRIPTION ID" Name="THIS IS YOUR SUBSCRIPTION NAME" ManagementCertificate="THIS IS YOUR CERTIFICATE" />
</PublishProfile>
</PublishData>[/xml]The easiest way is to use ruby to generate the pfx using this file. To do this make sure that you haveĀ toĀ installĀ the azure libraryĀ using the following command:
[ruby]gem install azure -v 0.7.0[/ruby]
With pfxer you can create a pfx fileĀ using yourĀ publish settings with the following:
[plain]pfxer transform –in PATH_PUBLISH_SETTINGS[/plain]
- azure.mgmt_endpoint:The value that is needed is https://management.core.windows.net, whichĀ is the endpoint of the administration APIs, so it is not necessary to change it.
- azure.subscription_id: It is of the id of your subscription, you can find it in previous file.
- azure.vm_image: The name of the image within the repository of Microsoft Azure you are going to use. Using Azure CLI you can retrieve thename of the images that you can use:
[plain]azure vm image list[/plain]
Don’t panic, I know that the list is endless. You can filter them by using something like the following:
[plain]azure vm image list | grep &quot;Ubuntu-15_04&quot;[/plain]
For this article I am going to use the one that has the nameĀ b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-15_04-amd64-server-20150224.5-beta1-en-us-30GB.
- azure.vm_name: name of your virtual machine.
- azure.vm_password: the administrator password.
- azure.vm_location: the region where you want to deploy your machine.
Once you have set all the parameters, you can simply launch the vagrant up command to create your new virtual machineĀ through Vagrant:
[plain]vagrant up[/plain]
Once the process is finished, you can communicate with your new virtual machine through vagrant ssh.

To complete the cycle, when you have finished working with it, you can remove it via the vagrant destroy command.
Cheers!




