Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use variables with Azure CLI

I am trying to use variable in azure CLI like we used in powershell.

In powershell we define variable as follows

$LOCATION = value

And used it in command as follows

az group create --name foo --location $LOCATION

What I have tried :-

I have tried to find it out in Microsoft documentation

https://learn.microsoft.com/en-us/cli/azure/get-started-with-azure-cli?view=azure-cli-latest

but I did not get any information about that.

Question :-

  1. How we can define variable in azure CLI?(like powershell)
  2. How we can used it in command?(like powershell)

Note:- I have installed azure CLI at my local.

like image 843
Dev Avatar asked Sep 06 '20 10:09

Dev


People also ask

How do you pass variables in Azure CLI?

Use shell variables You can use variables in Bash to pass values for parameters to commands. Using variables with the Azure CLI also allows reuse of commands, either piecemeal or in scripts. This example creates a new storage disk of the same type as the storage disk on an existing virtual machine.

How do I set environment variables in Azure cloud shell?

To set environment variables when you start a container in the Azure portal, specify them in the Advanced page when you create the container. Under Environment variables, enter NumWords with a value of 5 for the first variable, and enter MinLength with a value of 8 for the second variable.

What can you do with Azure CLI?

The Azure Command-Line Interface (CLI) is a cross-platform command-line tool to connect to Azure and execute administrative commands on Azure resources. It allows the execution of commands through a terminal using interactive command-line prompts or a script.


Video Answer


2 Answers

The easiest way to pass variables to any CLI command is by using environment variables

An environment variable is a variable whose value is set outside the program, typically through a functionality built into the operating system or microservice. An environment variable is made up of a name/value pair, and any number may be created and available for reference at a point in time.

Below you can find examples in Bash and CMD:

Bash-

Set new environment variable-

export LOCATION=westeurope

Print the environment variable-

echo ${LOCATION}

AZ CLI example-

az group create --name foo --location ${LOCATION}

CMD-

Set new environment variable-

set LOCATION=westeurope

Print the environment variable-

echo %LOCATION%

AZ CLI example-

az group create --name foo --location %LOCATION%
like image 100
Amit Baranes Avatar answered Oct 23 '22 11:10

Amit Baranes


It is the same way you do it in powershell,

To assign a value

sajeetharan@Azure:~$ LOCATION="eastus"

To check value is set,

sajeetharan@Azure:~$ echo $LOCATION
eastus
like image 26
Sajeetharan Avatar answered Oct 23 '22 12:10

Sajeetharan