Skip to content

Instantly share code, notes, and snippets.

@troydai
Last active September 17, 2024 16:01
Show Gist options
  • Save troydai/14e19f62b0bf9f401d44d142b9df2127 to your computer and use it in GitHub Desktop.
Save troydai/14e19f62b0bf9f401d44d142b9df2127 to your computer and use it in GitHub Desktop.
Purpose: Azure CLI alias

What is az alias

The az alias is an extension point allows user to define their own command alias based on existing commands. The alias will enable scenarios which improve the usability of azure cli.

The definition of the aliases could be a configuration file in a uesr friendly format, like yaml or ini

An inspiration of the alias system is git alias

Features

  • Text based user defined alias configuration placed under .azure folder.
  • Templated base alias definition
  • Encourage personalization and sharing configuraitons.

Challenges

  • Find a proper script/template language for the alias definition. Candidats: bash script, go template, python native.
  • Find a proper mechanism to resolve conflict and name collusion.
  • Performance.

Scenarios

Shorten the command

Many azure cli commands have long names. For example, to create a diagnostic setting users need to type az monitor diagnostic-setting create. To shorten the command, user can define such alias in the alias configuration:

[alias]
az mn diag = az monitor diagnostic-setting create
$ az mn diag -n new_setting --resource <...>

Enable positional argument

Azure CLI commands don't support positional arguments. However for some commands, the positional arguments are preferable. An alias template would allow user to transform a command into supporting positional arguments.

[alias]
az cp {src_uri} {dst_url} = az storage blob copy start-batch --source {src_uri} --destination {dst_uri}
$ az cp http://account1.blob.windows.net/source http://account2.blob.windows.net/dest

Environment variable injection

There are situations where a well defined environment variable is repeatedly used in a command.

[alias]
# Effect: all the vm created will have the 'owner' tag
az myvm *=az vm create * --tags owner={$USER}

Arguments parsing (Advanced)

Transform a user input and fill in the existing command parameters can enable user to build a more intuitive form of existing command.

[alias]
az ls {path}=az storage blob list --account-name {path split '://' index 0} --contaienr {path split '://' index 1}
$ az ls my_storage_acct://targer_container

The script/template language used here to parse argument is a implementation detailed. In above example, these are pseudo language.

Mini scripting (Advanced)

Allows user to define an alias as a sequence of commands.

Example 1

[alias]
# SSH into vm
az ssh {vm}= >>> EOL

name=$(az vm show -n troyperf02 -g troy-perf-permanent -otsv --query 'osProfile.adminUsername')
ip=$(az vm list-ip-addresses -n {vm} --query '[0].virtualMachine.network.publicIpAddresses[0].ipAddress' -otsv)
ssh {name}@{ip}

EOL
$ az ssh my_vm

Example 2

[alias]
# Retieve storage account and name and saved to environment variable
az storage account save {acct}= >>> EOL

rg=$(az storage account list -otsv --query '[].{Name:name, rg:resourceGroup}' | grep trdai | cut -f2)
key=$(az storage account keys list -n {acct} -g {rg} --query [0].value)
eval export AZURE_STORAGE_ACCOUNT={acct}
eval export AZURE_STORAGE_KEY={key}

EOL
$ az storage account save my_storage_account
$ az storage container list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment