Azure Cross-Platform Automation
Step-by-Step: Create a Virtual Machine with Azure xPlat CLI
During this demonstration, we will explore the installation process for the Azure xPlat CLI Tool on Mac OS X, how to authenticate to Microsoft Azure, and how to manage your Azure subscriptions. Authentication is supported for both Microsoft Accounts and Azure Active Directory (AAD) user accounts. If you have more than one Azure subscription, you can switch between them.
Hello, and welcome to Microsoft Azure Cross Platform CLI Tool Automation. The first step we're going to do here is to actually download the Azure CLI package and install it.
So, you'll want to go out to the GitHub project page at github.com/azure/azure-xplat-cli, and then you'll click on the releases section. Now, the current release, 0.9.12, is actually a patch release, a hot fix release, so they didn't actually release a new package for that version. So if we go back to 0.9.11, we can download the Mac dmg package.
So now, we double click the dmg package that we've downloaded and we can now see that there's a package file inside of there. So we'll go ahead and double click the package, and you'll see that we get an installer for the Azure Cross Platform CLI. So we just click continue a couple of times, and then agree, and install, and it'll prompt us for our administrative password since we're installing software on our Mac. And the installation process should actually take just a few seconds.
Once the installation process is finished, you'll see that to get started, all we have to do is type the "azure" command. If we want to uninstall the Azure xplat CLI tool, we can type "azure-uninstall." So let's go over to our bash prompt and just type "azure" and hit tab a couple of times. And as you can see, we now have the "azure" command as well as the "azure-uninstall" command. So now, we are done with the installer. The other thing you'll notice is that we actually get a documentation page automatically opened up inside of our web browser after the installation process has completed.
So let's go ahead and run the azure command for the first time. As you can see, the "azure" command prints out a bunch of useful information such as the tool version, a nice little friendly banner using ASCII art, and some of the essential commands to get started. So as you can see, we have a "help" command. So for any of these sub-commands in the Azure Cross Platform CLI tool, we can just type "azure help" and then the command name.
The first step that we have to run is actually logging into our Azure account. So I'm going to authenticate using my Azure active directory account using the Azure login command. So if I type "azure login," you'll see that by default, we get a prompt that says, "Authenticating." Eventually, this is going to generate a code for us, and it'll ask us to go out to a webpage called "aka.ms/devicelogin." So if we copy that URL and go visit that page, you'll see that we are then prompted for a code. The code is also displayed here in our console, so I'll go ahead and copy that. And then once this webpage comes up, I'll go ahead and type that into the authentication dialog, and then I'll be prompted for my user account credentials. So I paste that code into the device login and hit "continue," and the next page is going to ask me for my username and password. So I'm gonna go ahead and log in using Azure Active Directory. And as you can see, it says, "You have now signed into the Windows Azure Cross-platform Command Line Interface application." And if we switch back over to the Azure CLI Tool, eventually, once it recognizes that that authentication process has completed, it will go ahead and return the command prompt to us.
So as you can see, we now have control over our command prompt again, and the tool actually told us that it added two different Azure subscriptions to our login. So let's go ahead and see what's next. So I'm just gonna type the "azure" command again, and I'm gonna go ahead and close that authentication dialog, and minimize our web browser here for simplicity.
So as you can see, we've got a bunch of different commands available to us. One of the commands that we're gonna wanna get familiar with is called "azure account." But before we do that, I want to show you one other login option here. If you want to clear your login, you can actually just call "azure account clear" and that will actually clean up all of the pre-authenticated sessions that you have. It prompts me and says, "Are you sure you want to clean out your sessions?" And I say, "Yes, I am sure." So now, we're back to square one.
So if we don't have a web browser or we want to silently authenticate through an SSH session, we can actually call "azure login" and then let's look at the help for this. And as you can see, we've actually got a username parameter here. So if we call "azure login --username" and then specify our Azure Active Directory account on the command line, instead of being taken to a web browser, it'll immediately ask us for our password. So we securely type in our password, which is just shown by asterisks there, and Azure will go ahead and authenticate us through the command line. This process is better if you're using automation scripts or if you're inside of an SSH session and don't have a browser available to you in order to authenticate the default way. So I actually typed in a wrong password there, and you saw we got an exception. So I'm gonna go ahead and type my correct password, and we will authenticate successfully.
So now that we're authenticated to Microsoft Azure, the next step is to take a look at the subscriptions that we have available to us. As with the Azure PowerShell Project, you want to make sure that you are in the right subscription context so that any operations you perform are operating against the appropriate subscription or account. So once the command line returns here, we'll go ahead and take a look at the Azure account option.
Okay, so as you can see, we've successfully re-authenticated using the "azure login" command. So now, I'm going to take a look at "azure account -help," and this is going to show us all of the options that help us to manage our subscriptions. So the first command we can use is called "azure account list," and this will list out all of the subscriptions that are available to us. So when we call "azure account list," you'll see a little friendly table here that has the subscription names, the subscription IDs, and whether the subscription is the currently selected one. It'll also show you the state of the subscription.
So I have one subscription here that's showing as disabled because it reached the credit limit, and the other subscription is enabled because it's an active account. So if I want to choose a different subscription, I can call "azure account set." And then if we look at the help for this command, you'll see that all we do is specify the subscription name or ID. So we have the option at this point to either copy the ID of the subscription, or we can grab the subscription name itself. I personally prefer to use the subscription name because it's more memorable than the GUIDs for the account ID.
So I call "azure account set 'Microsoft Partner Network.'" And you can see that I have now changed into the context of my Microsoft Partner Network subscription. So if I call "azure account list," you'll see that the current subscription has now been changed to Microsoft Partner Network. So that way, any commands that I issue to azure-xplat-cli will be initiated on that subscription.
Let's take a look at some of the other commands that we have available inside of our account. We can also take a look at the account environment by doing "azure account env list." So let's go ahead and give that a try. So I'm going to type "azure account env list," and you'll see that I actually have several different environments available.
An environment is something such as the Azure Public Cloud, the Azure China Cloud, if you have an account there, or the Azure United States Government Cloud. An environment is also a generic representation of a cloud that can be used to manage on premises clouds, in addition to public clouds.
I can now run the "azure" command again, and one of the options you'll see is that I have the option to open the Azure portal. So if I type "azure portal," you'll see that it automatically takes me out to my web browser, and it will navigate to the Microsoft Azure portal so I can manage my resources from there.
About the Author
Trevor Sullivan is a Microsoft MVP for Windows PowerShell, and enjoys working with cloud and automation technologies. As a strong, vocal veteran of the Microsoft-centric IT field since 2004, Trevor has developed open source projects, provided significant amounts of product feedback, authored a large variety of training resources, and presented at IT functions including worldwide user groups and conferences.