Craig Forrester

The Pleasure of Finding Things Out

github linkedin email rss
Azure CLI Basics: Listing Resources
May 8, 2018
6 minutes read

In this post, we will cover some basic commands to list some resources in Azure.

TL;DR

If you just want to see a list of commands covered in this post:

az account list
az group list
az network vnet list
az network vnet subnet list -g $resource_group --vnet-name $vnet
az network nsg list -g $resource_group
az network nsg rule list -g $resource_group --nsg-name $nsg
az network nic list
az network nic ip-config list -g $resource_group --nic-name $nic
az network public-ip list -g $resource_group
az vm list -g $resource_group
az vm list -g $resource_group --show-details

Listing Accounts

First, as always, verify that you have logged in and that the correct subscription is selected by running az account list:

$ az account list
[
  {
    "cloudName": "AzureCloud",
    "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
    "isDefault": true,
    "name": "Visual Studio Enterprise",
    "state": "Enabled",
    "tenantId": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
    "user": {
      "name": "XXXXXXXXXXXXXXXXXXXXX",
      "type": "user"
    }
  }
]

The currently select subscription is the one marked True under “IsDefault” in the output. If the wrong subscription is selected, see my previous post on working with subscription logins in Azure CLI.

Output Formats

As you can see from our previous command, Azure CLI outputs in JSON format by default. If you want to change the output, you can simply add the output parameter, --output or -o, to your command line:

$ az account list --output table
Name                                CloudName    SubscriptionId                        State    IsDefault
----------------------------------  -----------  ------------------------------------  -------  -----------
Visual Studio Enterprise            AzureCloud   XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX  Enabled  True

If you want to set another output format as your default, so that you don’t have to type, say -o table, every time you run a command, you can run az configure, which will prompt you interactively to set some default options — among them, the default output option.

Output options:

--output Description
json JSON string. This setting is the default.
jsonc Colorized JSON.
table ASCII table with keys as column headings.
tsv Tab-separated values, with no keys

Listing Resources

There are many times when you start working on an existing subscription, or revisit it later, and you simply want to get information — what resources exist already, how they’re configured, where they reside, what’s changed since the last time you’ve worked on the subscription, etc. Azure CLI is a good way to run quick interactive queries to get a sense for what already exists or what’s changed on a subscription.

List Resource Groups

The first thing you’ll want to do is start with the base for all the deployed resources: resource groups. To list the resource groups for the selected subscription, use az group list:

$ az group list
Name                                Location        Status
----------------------------------  --------------  ---------
cloud-shell-storage-southcentralus  southcentralus  Succeeded
labrg1                              centralus       Succeeded
labrg2                              eastus2         Succeeded

Notice that we get the name and location of the resource groups, as well as their status.

Again, recall that this runs the command against the currently selected subscription. See the previous post for details on how to switch subscriptions in Azure CLI.

Listing Virtual Networks (VNets)

$ az network vnet list
Location    Name          ProvisioningState    ResourceGroup    ResourceGuid
----------  ------------  -------------------  ---------------  ------------------------------------
eastus      lab1-vnet     Succeeded            lab1rg           XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
eastus      lab2-vnet     Succeeded            lab2rg           XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
eastus      lab3-vnet     Succeeded            lab3rg           XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
eastus      lab4-vnet     Succeeded            lab4rg           XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
eastus      lab5-vnet     Succeeded            lab5rg           XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
eastus      lab6-vnet     Succeeded            lab6rg           XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

Listing Subnets

Here is an example show how to list subnets within specific VNets:

$ az network vnet subnet list -g lab2rg --vnet-name lab2-vnet
AddressPrefix    Name         ProvisioningState    ResourceGroup
---------------  -----------  -------------------  ---------------
10.0.0.0/24      subnet-1     Succeeded            lab2rg

Listing Network Security Groups (NSGs)

Likewise, for Network Security Groups:

$ az network nsg list -g lab2rg
Location    Name        ProvisioningState    ResourceGroup    ResourceGuid
----------  ----------  -------------------  ---------------  ------------------------------------
eastus2     labvm1-nsg  Succeeded            lab2rg          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

And NSG Rules, which require you specify the related NSG:

$ az network nsg rule list -g lab2rg --nsg-name ubuvm1-nsg
Access    DestinationAddressPrefix      DestinationPortRange  Direction    Name      Priority  Protocol    ProvisioningState    ResourceGroup    SourceAddressPrefix    SourcePortRange
--------  --------------------------  ----------------------  -----------  ------  ----------  ----------  -------------------  ---------------  ---------------------  -----------------
Allow     *                                               22  Inbound      SSH            300  TCP         Succeeded            lab2rg           *                      *

Notice that this output does not show us the default NSG rules, only those we specifically added after the intial deployment, such as the SSH-Allow rule here.

Listing NICs (Network Interfaces)

$ az network nic list
Location    Name               Primary    ProvisioningState    ResourceGroup    ResourceGuid
----------  -----------------  ---------  -------------------  ---------------  ------------------------------------
eastus2     ubuvm1000          True       Succeeded            lab2rg           XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

To list their related IP configurations, you’ll need to supply the NIC:

$ az network nic ip-config list -g lab2rg --nic-name ubuvm1000
Name       Primary    PrivateIpAddress    PrivateIpAddressVersion    PrivateIpAllocationMethod    ProvisioningState    ResourceGroup
---------  ---------  ------------------  -------------------------  ---------------------------  -------------------  ---------------
ipconfig1  True       10.0.0.4            IPv4                       Dynamic                      Succeeded            lab2rg

Listing Public IPs

Public ips are listed at the same subcommand level as NICs are:

$ az network public-ip list -g lab2rg
Name       ResourceGroup    Location    Zones    AddressVersion    AllocationMethod      IdleTimeoutInMinutes  ProvisioningState
---------  ---------------  ----------  -------  ----------------  ------------------  ----------------------  -------------------
ubuvm1-ip  lab2rg           eastus2              IPv4              Dynamic                                  4  Succeeded

Listing Virtual Machines

Listing virtual machines only gives us the name, resource group, and location:

$ az vm list -g lab2rg
Name     ResourceGroup    Location
-------  ---------------  ----------
server1  lab2rg           eastus

However, if we add the --show-details switch, we only get marginally more information:

$ az vm list --resource-group lab2rg --show-details
Name     ResourceGroup    PowerState    PublicIps      Fqdns                            Location
-------  ---------------  ------------  -------------  -------------------------------  ----------
server1  lab2rg           VM stopped    10.10.110.110  lab1.eastus.cloudapp.azure.com   eastus

So, if your default output format is table, then you’ll only see a limited number of columns, like the output showing here. If you want more detail, you’ll need to either use JSON output, or perform a query. I’ll show you how to do both.

$ az vm list --resource-group lab2rg --show-details -o json
[
  {
    "additionalProperties": {},
    "availabilitySet": null,
    "diagnosticsProfile": null,
    "fqdns": "",
    "hardwareProfile": {
      "additionalProperties": {},
      "vmSize": "Standard_B1s"
    },
    "id": "/subscriptions/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/resourceGroups/lab2rg/providers/Microsoft.Compute/virtualMachines/ubuvm1",
    "identity": null,
    "licenseType": null,
    "location": "eastus2",
    "macAddresses": "",
    "name": "ubuvm1",
    "networkProfile": {
      "additionalProperties": {},
      "networkInterfaces": [
        {
          "additionalProperties": {},
          "id": "/subscriptions/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/resourceGroups/lab2rg/providers/Microsoft.Network/networkInterfaces/ubuvm0001",
          "primary": null,
          "resourceGroup": "lab2rg"
        }
      ]
    },
    "osProfile": {
      "additionalProperties": {},
      "adminPassword": null,
      "adminUsername": "ubuntu",
      "computerName": "ubuvm1",
      "customData": null,
      "linuxConfiguration": {
        "additionalProperties": {},
        "disablePasswordAuthentication": true,
        "ssh": {
          "additionalProperties": {},
          "publicKeys": [
            {
              "additionalProperties": {},
              "keyData": "ssh-rsa ",
              "path": "/home/devadmin/.ssh/authorized_keys"
            }
          ]
        }
      },
      "secrets": [],
      "windowsConfiguration": null
    },
    "plan": null,
    "powerState": "VM deallocated",
    "privateIps": "10.10.10.10",
    "provisioningState": "Succeeded",
    "publicIps": "",
    "resourceGroup": "lab2rg",
    "resources": null,
    "storageProfile": {
      "additionalProperties": {},
      "dataDisks": [],
      "imageReference": {
        "additionalProperties": {},
        "id": null,
        "offer": "UbuntuServer",
        "publisher": "Canonical",
        "sku": "16.04-LTS",
        "version": "latest"
      },
      "osDisk": {
        "additionalProperties": {},
        "caching": "ReadWrite",
        "createOption": "FromImage",
        "diskSizeGb": 30,
        "encryptionSettings": null,
        "image": null,
        "managedDisk": null,
        "name": "ubuvm1",
        "osType": "Linux",
        "vhd": {
          "additionalProperties": {},
          "uri": "https://ubuvm1.blob.core.windows.net/vhds/ubuvm100000000000000.vhd"
        },
        "writeAcceleratorEnabled": null
      }
    },
    "tags": null,
    "type": "Microsoft.Compute/virtualMachines",
    "vmId": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    "zones": null
  }
]

As you can see, the JSON output is detailed. The JSON output will come in handy when we begin performing queries. In my next post, we will take a look at how to query for specific resources or their properties using criteria we specify, and how to construct those queries based on the structure of the objects Azure contructs for each resource type, which the JSON output shows us.

Additional Reading


Back to posts