How can i pass a variable between tasks in Azure Devops/VSTS - azure

For Task 1 I have a CLI task which simply gets the subnet name and subnet ref as below
$subnetname1 = az network vnet subnet list --resource-group vnetrg01 --vnet-name vnet01 --query "[].name" -o tsv
$subnetref1 = az network vnet subnet list --resource-group vnetrg01 --vnet-name vnet01 --query "[].id" -o tsv
For task 2 I want to deploy an arm template which will use parameters from the pipleine variables in Azure Devops
So for example the result of $subnetref1 in Task 1 above needs to populate the pipleline variable for subnetref (which is setup as a variable in the pipleine) which will then be passed to the arm template override parameters
cant seem to get this working

You can do it with Powershell command,
In the first PowerShell task set the variable as environment variable:
$subnetname1 = az network vnet subnet list --resource-group vnetrg01 --vnet-name vnet01 --query "[].name" -o tsv
Write-Host $subnetname1
Write-Host ("##vso[task.setvariable variable=subnetname1;]$subnetname1")
In the second task read the variable in this way:
$subnetname1 = $env:subnetname1
Write-Host $subnetname1

Related

az update service-endpoints using variable

I'm trying to add multiple Service-Endpoints to subnets using the update command, and having a variable to represent the SE's
When it runs, it fails with and error saying that the array is using an invalid service name.
When running the command without a variable for the SE's, it runs with out any problem.
$SE = "Microsoft.KeyVault Microsoft.Storage"
az network vnet subnet update --service-endpoints $SE --resource-group MyRg1 --vnet-name MyVnet --name MySnet
## Used to display the varaible format
Write-host "az network vnet subnet update --service-endpoints $SE --resource-group MyRg1 --vnet-name MyVnet --name MySnet"
Using a loop, and adding each SE is not a good option, as the update cmd is idempotent.
This has to do with how powershell handles variables; the $SE that you pass in is a single positional parameter, whereas the az client parses them as being distinct.
This is a common problem with powershell. For example, consider an application that prints the command line arguments:
> $SE = "my args"
> MyExe.exe $SE something else
The output would be:
arg0: MyExe.exe
arg1: my args
arg2: something
arg3: else
To correct this, you need to instruct powershell to split the string into an array, which (when it builds a commandline for az) gets split into multiple args:
az network vnet subnet update --service-endpoints $($SE -split ' ') --resource-group MyRg1 --vnet-name MyVnet --name MySnet
The parameter --service-endpoints accepts space-separated list of services.
The error is because you are not supplying a space-separated list. Instead you are providing a simple string with spaces in it.
az network vnet subnet update --service-endpoints Microsoft.KeyVault Microsoft.Storage
The above command should resolve your issue.
UPDATE :
I tried #KommandanKeen's approach. I was not able to get it working. You could give it a shot see whether it is working for you.
Alternatively, I came up with a workaround :
By making use of the Invoke Expression - Invoke-Expression evaluates a string as a command.
Invoke-Expression "az network vnet subnet update --service-endpoints $SE --resource-group MyRg1 --vnet-name MyVnet --name MySnet"
In this case the $SE is expanded and the command string is formulated.
az network vnet subnet update --service-endpoints Microsoft.KeyVault Microsoft.Storage......
Now, the command string is evaluated as an expression.

Azure CLI - Delete resource without deleting resource group

The goal is to delete all resources in a resourcegroup without deleting it using azure CLI.
From reading the doc, I can do this:
az resource delete -g MyResourceGroup -n MyVm
Therefore I assumed I can do the following
az resource list --resource-group MyResourceGroup | az resource delete
A similar command in Azure Powershell would work like the above. I am quite new to CLI, is this method possible? What is the efficient way of removing all resources in a resource group (if we have multiple types of resources)
If can you run Azure CLI in powershell, you could use ConvertFrom-Json to convert the JSON result to a list of objects from az resource list, then run az resource delete on each object id using a foreach loop.
$resources = az resource list --resource-group myResourceGroup | ConvertFrom-Json
foreach ($resource in $resources) {
az resource delete --resource-group myResourceGroup --ids $resource.id --verbose
}
We could also run this entirely in the pipeline using Foreach-Object, which is close to what you are trying to do.
az resource list --resource-group myResourceGroup
| ConvertFrom-Json
| Foreach-Object {az resource delete --resource-group myResourceGroup --ids $_.id --verbose}
If you don't want to use powershell at all, we can use bash to parse the JSON output ourselves using grep and awk.
#!/bin/bash
resources="$(az resource list --resource-group myResourceGroup | grep id | awk -F \" '{print $4}')"
for id in $resources; do
az resource delete --resource-group myResourceGroup --ids "$id" --verbose
done
As #Hong Ooi helpfully pointed out in the comments, the main issue with the above is that some resources depend on other resources, so order of deletion matters. One example is that you cannot delete virtual machine disks before the virtual machine is deleted.
To get around this, we could define an ordering of resource types in which to delete resources, as shown in the example hash table below:
$resourceOrderRemovalOrder = [ordered]#{
"Microsoft.Compute/virtualMachines" = 0
"Microsoft.Compute/disks" = 1
"Microsoft.Network/networkInterfaces" = 2
"Microsoft.Network/publicIpAddresses" = 3
"Microsoft.Network/networkSecurityGroups" = 4
"Microsoft.Network/virtualNetworks" = 5
}
Then sort the resources by their resource types and delete them:
$resources = az resource list --resource-group myResourceGroup | ConvertFrom-Json
$orderedResources = $resources
| Sort-Object #{
Expression = {$resourceOrderRemovalOrder[$_.type]}
Descending = $False
}
$orderedResources | ForEach-Object {
az resource delete --resource-group myResourceGroup --ids $_.id --verbose
}
Or in one pipeline if you prefer:
az resource list --resource-group myResourceGroup
| ConvertFrom-Json
| Sort-Object #{Expression = {$resourceOrderRemovalOrder[$_.type]}; Descending = $False}
| ForEach-Object {az resource delete --resource-group myResourceGroup --ids $_.id --verbose}
There is a faster and simpler approach:
az deployment group create --mode complete --template-uri data:application/json,%7B%22%24schema%22%3A%22https%3A%2F%2Fschema.management.azure.com%2Fschemas%2F2019-04-01%2FdeploymentTemplate.json%23%22%2C%22contentVersion%22%3A%221.0.0.0%22%2C%22resources%22%3A%5B%5D%7D --name clear-resources --resource-group <RG_NAME>
The above data URI (which may also be Base64-encoded) represents this file:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": []
}
In an Azure DevOps pipeline you have to pass an actual file with the JSON contents above as DevOps only allows HTTP(s) schemes with --template-uri:
az deployment group create --mode complete --template-file ./clear-resources.json --resource-group <RG_NAME>

How to get the value of subnet ref using azure cli based on a subnet name

I want to put the value of the subnet id of a subnet name i provide in a variable. I run the command below and it shows the id and all the other details for the subnet
az network vnet subnet show -g vnetrg01 -n subnet01 --vnet-name vnet01
So i used the below to place the ref in a variable but it doesnt work?
$subnetref = az network vnet subnet show -g vnetrg01 -n subnet01 --vnet-name vnet01 --query "[].id" -o tsv
Below worked for me to get subnet id.
SUBNET=$(az network vnet subnet list --subscription test-subscription --resource-group test-RG --vnet-name test-vnet --query "[?name=='test-subnet'].id" --output tsv)
Ok, found out the solution:
$subnetref = az network vnet subnet list --resource-group vnetrg01 --vnet-name vnet01 --query "[?name=='SUBNET01'].id"

Deleting resources attached to azure virtual machine using Azure CLI

I'm writing a shell script for deleting azure virtual machine and its associated resources, but having issues getting vm's network security group name/ids and vm's public ip name/ids.
I have the name of my resource group and the name of the machine itself. Moreover, I've found vm's NIC using the command:
vmNIC=$(az vm nic list --resource-group $rgName --vm-name $vmName --query [].id -o tsv);
And found vm's disks (OS and data) using the commands:
vmOSDisk=$(az vm show -d -g $rgName -n $vmName --query "storageProfile.osDisk.managedDisk.id" -o tsv);
vmDataDisks=$(az vm show -d -g $rgName -n $vmName --query "storageProfile.dataDisks[].managedDisk.id" -o tsv);
Does anyone know how can I retrieve the name/ids of my virtual machine's NSG and my virtual machine's Public IP?
Thank you for your help.
To retrieve the VM's public IP, you can use
vmNIC=$(az vm nic list --resource-group $rgName --vm-name $vmName --query [].id -o tsv)
az network nic show --ids $vmNIC --query "ipConfigurations[].publicIpAddress.id" -o tsv
To retrieve the name/ids of the virtual machine's NSG, you can use
The nic level NSG,
az network nic show --ids $vmNIC --query "networkSecurityGroup.id" -o tsv
The subnet level NSG,
subnetID=$(az network nic show --ids $vmNIC --query "ipConfigurations[].subnet.id" -o tsv)
az network vnet subnet show --ids $subnetID --query "networkSecurityGroup.id" -o tsv

azure cli list nic attached to VM

I am working on bash shell. I need az cli or unix script to find out NIC name attached to particular VM. I know VM name and VM Resource Group Name and my Target is to findout out which NIC is attached to this VM and which resouce group this NIC belongs to?
Please follow this line of azure cli code:
Step 1: Define a variable, like a. Note that there is no whiteSpace around the chars = :
a="$(az vm nic list --resource-group "your_resource_group" --vm-name "your_vm_name" --query "[].{id:id}" --output tsv)"
Step 2: Just get the nic name and it's resource group:
az vm nic show -g "your_resource_group" --vm-name "your_vm_name" --nic $a --query "{name:name,resourceGroup:resourceGroup}" --output table
Step 3: If you want get all the information of nic, please use the code below:
az vm nic show -g "your_resource_group" --vm-name "your_vm_name" --nic $a
az vm nic list --resource-group
--vm-name
[--subscription]
This will list all nics on a vm.
eg. az vm nic list -g MyResourceGroup --vm-name MyVm

Resources