The Azure CLI alias extension

09/07/2018

4 minutes to read

In this article

The alias extension allows users to define custom commands for the Azure CLI by using existing commands. Aliases help keep your workflow simple by allowing shortcuts. Since aliases are powered by the Jinja2 template engine, they even offer advanced argument processing.

Note

The Alias Extension is in public preview. The features and configuration file format may change.

Install the alias extension

The minimum required Azure CLI version to use the alias extension is 2.0.28. To check your CLI version, run az --version. If you need to update your installation, follow the instructions in Install the Azure CLI.

Verify the installation of the extension with az extension list. If the alias extension was installed properly, it's listed in the command output.

az extension list --output table --query '[].{Name:name}'

Name
------
alias

Keep the extension up-to-date

The alias extension is under active development and new versions are released regularly. New versions aren't installed when you update the CLI. Install the updates for the extension with az extension update.

az extension update --name alias

Manage aliases for the Azure CLI

The alias extension lets you create and manage aliases for other CLI commands. To view all the available commands and parameter details, run the alias command with --help.

az alias --help

Create simple alias commands

One use of aliases is for shortening existing command groups or command names. For example, you can shorten the group command group to rg and the list command to ls.

When running this command, you give values to the positional arguments.

az get-vm-ip MyResourceGroup MyVM

You can also use environment variables in aliased commands, which are evaluated at runtime. The next example adds the create-rg alias, which creates a resource group in eastus and adds an owner tag. This tag is assigned the value of the local environment variable USER.

To register the environment variables inside the command of the alias, the dollar sign $ must be escaped.

Process arguments using Jinja2 templates

Argument substitution in the alias extension is performed by Jinja2. Jinja2 templates allow for manipulating the arguments.

With Jinja2 templates, you can write aliases that take different types of arguments than the underlying command. For example, you can make an alias that takes a storage URL. Then this URL is parsed to pass the account and container names to the storage command.

Alias configuration file

Another way to create and modify aliases is to alter the alias configuration file. Alias command definitions are written into a configuration file, located at $AZURE_USER_CONFIG/alias. The default value of AZURE_USER_CONFIG is $HOME/.azure on macOS and Linux, and %USERPROFILE%\.azure on Windows. The alias configuration file is written in the INI configuration file format. The format for alias commands is:

[alias_name]
command = invoked_commands

For aliases that have positional arguments, the format for alias commands is:

The next example shows an alias for a command with arguments. This command gets the public IP address for a VM. Aliased commands must all be on a single line, and use
all of the arguments in the alias name.