In this lab we will walkthrough how to use the Azure CLI (`az cli`) to reconfigure the networking settings of a VM that already exists.
**Our primary focus is on Azure CLI**, with VM Networking being the example used to help demonstrate concepts. As such, we expect you the have familiarity with VM Networking.
This intermediate level hands-on lab covers several concepts, including the following:
* Using Bash variables with Azure CLI
* Outputting and querying information with Azure CLI
This hands-on lab uses the Azure Cloud Shell. You can follow along at work or at home using your web browser and don’t need to install any specific software.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Create New Network Resources
Create new network resources using Azure CLI and the following settings:
Public IP Address
Resource Group: Existing resource group
Name: pubip1
SKU: Basic
Allocation method: DynamicNetwork Interface
Resource Group: Existing resource group
Name: nic2
IP Address: 10.1.0.10
Assigned VNet: vnet1
Assigned Subnet: subnet1
Use the below instructions as a guide:
Note: The resource group has already been created for you.
- Login to the Azure portal using the credentials provided.
- Navigate to the Cloud Shell, using the
>_
icon in the toolbar. - Ensure you are using Bash for the Azure CLI.
- Identify the existing resource group:
az group list
- Create variables (see below): for example
nic2Name="nic2"
- Create the public IP address:
az network public-ip create
- Create a NIC:
az network nic create
Note:
az login
is not required when using Cloud Shell within the Azure Portal. The session authenticates as the user you are logged in as. You will need to use this command if connecting to Azure using Azure CLI on your own machine.The variables used in this lesson are as follows:
rg="paste your resource group name here"
pubName="pubip1"
pubSku="basic"
pubAlloc="dynamic"
nic2Name="nic2"
nic2Address="10.1.0.10"
vnetName="vnet1"
subnetName="subnet1"
vm="vm1"
loc="westus"
- Update the Existing VM Network Settings
Update the virtual machine to use the new network resources using Azure CLI and the following settings:
- VM1
Add new NIC: NIC2, created earlier
Public IP address: pubip1, attached to NIC2
Use the below instructions as a guide:
- Login to the Azure portal using the credentials provided.
- Navigate to the Cloud Shell, using the
>_
icon in the toolbar. - Ensure you are using Bash for the Azure CLI.
- Identify the id of the new NIC:
az network nic show -n $nic2Name -g $rg
- Update the current VM NIC to not be primary:
az vm update -n $vm -g $rg --set networkProfile.networkInterfaces[0].primary=false
- Add the new NIC to the VM:
az vm update -n $vm -g $rg --add networkProfile.networkInterfaces primary=true id=$nicId
- Remove the existing NIC:
az vm nic remove -g $rg --vm-name $vm --nics vm1-nic1
Note:
az login
is not required when using Cloud Shell within the Azure Portal. The session authenticates as the user you are logged in as. You will need to use this command if connecting to Azure using Azure CLI on your own machine.The following Azure CLI arguments are provided to help you:
- Output information about a resource using
show
command: e.g.az network nic show -n $nic2Name -g $rg
- Change the Azure CLI output format: e.g. to table format
az network nic show -n $nic2Name -g $rg -o table
- Assign command output to a variable: e.g. NIC id
nicId=$(az network nic show -n $nic2Name -g $rg --query id -o tsv)
- VM1