Friday, October 28, 2022

Azure CLI and How To Install The Azure CLI

 

What is the Azure CLI?

Completed

The Azure CLI is a command-line program to connect to Azure and execute administrative commands on Azure resources. It runs on Linux, macOS, and Windows, and allows administrators and developers to execute their commands through a terminal or command-line prompt (or script!) instead of a web browser. For example, to restart a virtual machine (VM), you would use the following command:


Azue CLI 

az vm restart -g MyResourceGroup -n MyVm


How to install the Azure CLI

On both Linux and macOS, you'll use a package manager to install the Azure CLI. The recommended package manager differs by OS and distribution:

  • Linux: apt-get on Ubuntu, yum on Red Hat, and zypper on OpenSUSE
  • Mac: Homebrew

The Azure CLI is available in the Microsoft repository, so you'll first need to add that repository to your package manager.

On Windows, you can install the Azure CLI by downloading and running an MSI file.


Using the Azure CLI in scripts

If you want to use the Azure CLI commands in scripts, you need to be aware of any issues around the "shell" (or environment) used for running the script. For example, in a bash shell, you'll use this syntax when setting variables:          

Azure CLI

variable="value"

variable=integer    

If you use a PowerShell environment for running Azure CLI scripts, you'll use this syntax for variables:

You must install the Azure CLI before you can use it to manage Azure resources from a local computer. The installation steps vary for Windows, Linux, and macOS, but once installed, the commands are common across platforms.


Install and run the Azure CLI

Windows

Here, you'll install the Azure CLI on Windows using the MSI installer.

  1. Go to https://aka.ms/installazurecliwindows, and in the browser security dialog box, click Run or Open file.
  2. In the installer, accept the license terms, then click Install.
  3. In the User Account Control dialog, select Yes.

Running the Azure CLI

You run the Azure CLI by opening a bash shell (Linux and macOS), or from the command prompt or PowerShell (Windows).

  1. Start the Azure CLI and verify your installation by running the version check.

        Azure CLI
       az --Version

You've set up your local machines to administer Azure resources with the Azure CLI. You can now use the Azure CLI locally to enter commands or execute scripts. The Azure CLI will forward your commands to the Azure datacenters where they will run inside your Azure subscription.


Work with the Azure CLI

The Azure CLI lets you type commands and execute them immediately from the command line. Recall that the overall goal in the software development example is to deploy new builds of a web app for testing. Let's talk about the sorts of tasks you can do with the Azure CLI.


What Azure resources can be managed using the Azure CLI?

The Azure CLI lets you control nearly every aspect of every Azure resource. You can work with resource groups, storage, virtual machines, Azure Active Directory (Azure AD), containers, machine learning, and so on.

Commands in the CLI are structured in groups and subgroups. Each group represents a service provided by Azure, and the subgroups divide commands for these services into logical groupings. For example, the storage group contains subgroups including accountblob, and queue.

So, how do you find the particular commands you need? One way is to use az find, the AI robot that uses the Azure documentation to tell you more about commands, the CLI and more.


Example: find the most popular commands related to the word blob.


Azure CLI
az find blob

Example: Show me the most popular commands for an Azure CLI command group, such as az vm.
Azure CLI
az find "az vm create"


If you already know the name of the command you want, the --help argument for that command will get you more detailed information on the command, and for a command group, a list of the available subcommands. So, with our storage example, here's how you can get a list of the subgroups and commands for managing blob storage:

azure CLI
az storage blob --help

How to create an Azure resource

When you're creating a new Azure resource, there are typically three steps: connect to your Azure subscription, create the resource, and verify that creation was successful. The following illustration shows a high-level overview of the process.


Connect

Since you're working with a local install of the Azure CLI, you'll need to authenticate before you can execute Azure commands, by using the Azure CLI login command.

Azure CLI

az login

The Azure CLI will typically launch your default browser to open the Azure sign-in page. If this doesn't work, follow the command-line instructions and enter an authorization code at https://aka.ms/devicelogin.

After a successful sign-in, you'll be connected to your Azure subscription.


Create

You'll often need to create a new resource group before you create a new Azure service, so we'll use resource groups as an example to show how to create Azure resources from the CLI.

The Azure CLI group create command creates a resource group. You must specify a name and location. The name must be unique within your subscription. The location determines where the metadata for your resource group will be stored. You use strings like "West US", "North Europe", or "West India" to specify the location; alternatively, you can use single word equivalents, such as westus, northeurope, or westindia. The core syntax is:


Azure  CLI

az group create --name <name> --location <location>



Verify

For many Azure resources, the Azure CLI provides a list subcommand to view resource details. For example, the Azure CLI group list command lists your Azure resource groups. This is useful to verify whether the resource group was successfully created:

azure CLI

az group list


To get a more concise view, you can format the output as a simple table:

Azure CLI

az group list -- output table







No comments:

Post a Comment

Python