Azure CLI Tutorial with Example Part-II

This article is a continuation of part-1, before reading this article I recommend to read the first part of this. The first part link is given below. The first part contains the installation and a few use cases of Azure CLI.

Azure CLI with example Part-I

In the first part, we have provisioned a few Azure resources with Azure CLI ( az command ). Here we will learn how we can use variables along with the az command.

az command with Bash variables

We can declare az command values with Bash variables. Below is the example of a bash variable called an array.

Create an associative array and add the variable to it. I will start with an example

techies@techiescorner:~$ declare -A arraysone

techies@techiescorner:~$ arrayone[value1]="first"

techies@techiescorner:~$ echo ${arrayone[value1]}

first

 

Here, the first command (declare) creates an associative array called “arrayone”. The next command will add the value “first” to the array. The third command we used to print the value from the array.  This is how the array will work. We will use this to store our Azure resource variable.

techies@techiescorner:~$ declare -A azurebook

techies@techiescorner:~$ azurebook[name]="Devops"

techies@techiescorner:~$ azurebook[RGname]="Devops-test"

techies@techiescorner:~$ azurebook[storage]="devstorageaccount0001"

techies@techiescorner:~$ azurebook[vnet]="testvnet"

techies@techiescorner:~$ azurebook[location]="eastus"

 

techies@techiescorner:~$ echo ${azurebook[vnet]}

testvnet

We have created an array named “azurearray” and added values to it. Next, create few Azure resources with these variables using az command.

Create a Resource Group named Devops-test

az group create -n ${azurebook[RGname]} -l ${azurebook[location]}

The above command pull values like Resource Group and location from the “azurebook” array.

 

Create a virtual network in the above resource group

az network vnet create -g ${azurebook[RGname]} -n ${azurebook[vnet]} --address-prefix 10.4.0.0/16

 

techies@techiescorner:~$ az network vnet create -g ${azurebook[RGname]} -n ${azurebook[vnet]} --address-prefix 10.4.0.0/16
{
  "newVNet": {
    "addressSpace": {
      "addressPrefixes": [
        "10.4.0.0/16"
      ]
    },
    "bgpCommunities": null,
    "ddosProtectionPlan": null,
    "dhcpOptions": {
      "dnsServers": []
    },
    "enableDdosProtection": false,
    "enableVmProtection": false,
    "etag": "W/\"73875d06765aaeac\"",
    "id": "/subscriptions/b220/resourceGroups/Devops-test/providers/Microsoft.Network/virtualNetworks/testvnet",
    "location": "eastus",
    "name": "testvnet",
    "provisioningState": "Succeeded",
    "resourceGroup": "Devops-test",

From the above output, it is clear that the az command pulled the required values from the array.

 

Create a storage account named devstorageaccount0001

az storage account create -g ${azurebook[RGname]} -n ${azurebook[storage]}

The command created the storage account devstorageaccount0001 under the resource group “Devops-test”. Check the output for more details.

techies@techiescorner:~$ az storage account create -g ${azurebook[RGname]} -n ${azurebook[storage]}
{
  "accessTier": null,
  "azureFilesIdentityBasedAuthentication": null,
  "creationTime": "2020-02-11T06:29:22.515830+00:00",
  "customDomain": null,
  "enableHttpsTrafficOnly": true,
  "encryption": {
    "keySource": "Microsoft.Storage",
    "keyVaultProperties": null,

 

Azure CLI Extension

The functionalities of Azure CLI can be extended with the help of extensions.

To list installed extension

techies@techiescorner:~$ az extension list

[]

I haven’t installed any extension, so the result is showing empty.

To list all available extension

techies@techiescorner:~$ az extension list-available -o table

Here, I have redirected the output as a table format. This command will list available extensions.

To install a new extension execute below command.

techies@techiescorner:~$ az extension add --name interactive The installed extension 'interactive' is in preview

If the installation failed to upgrade your installed pip package and try again. Here we have installed the extension “interactive” successfully.

To use the installed extension trigger the command as below.

techies@techiescorner:~$ az interactive

This command is in preview. It may be changed/removed in a future release.

Do you agree to sending telemetry (yes/no)? Default answer is yes: yes

az>> 

This extension will give you an interactive terminal to enter commands. Press Ctrl +D to quit from this terminal.

 

Leave a Reply

Your email address will not be published.