Azure DevOps hosted ubuntu agent issue updating Application Gateway - azure

I deployed some infra using Terraform, including an application gateway. Unfortunately not all settings can be set/updated with terraform. SO I have a shell script that updates the application gateway.
#!/bin/bash
SP_ID=${1}
SP_SECRET=${2}
TENANT_ID=${3}
SUBSCRIPTION=${4}
RG=${5}
az login --service-principal -u ${SP_ID} -p ${SP_SECRET} -t ${TENANT_ID}
az account set --subscription ${SUBSCRIPTION}
az account list -o table
# Get the name of the AG
echo "RG = ${RG}"
AG=$(az network application-gateway list --resource-group ${RG} | tail -n 1 | awk '{ print $2 }')
echo "AG = ${AG}"
# Get the AG backend pool name
BP=$(az network application-gateway address-pool list --resource-group ${RG} --gateway-name ${AG} | tail -n 1 | awk '{ print $1 }')
echo "Backend pool = ${BP}"
# Get the frontendip of the load balancer
LB=$(az network lb list --resource-group ${RG} | tail -n 1 | awk '{ print $2 }')
LBFEIP=$(az network lb frontend-ip list --lb-name ${LB} --resource-group ${RG} | tail -n 1 | awk '{ print $2 }')
echo "Load balancer = ${LB}"
echo "Frontend ip LB = ${LBFEIP}"
# Update the backend pool of the AG with the frontend ip of the loadbalancer
echo "Updating Backend address pool of AG ${AG}"
az network application-gateway address-pool update --gateway-name $AG --resource-group $RG --name $BP --servers ${LBFEIP}
# Update http settings
echo "Updating HTTP settings of AG ${AG}"
AG_HTS=$(az network application-gateway http-settings list --resource-group ${RG} --gateway-name ${AG} | tail -n 1 | awk '{ print $2 }')
az network application-gateway http-settings update --resource-group ${RG} --gateway-name ${AG} --name ${AG_HTS} --host-name-from-backend-pool true
# Update health probe
echo "Updating Health probe of AG ${AG}"
AG_HP=$(az network application-gateway probe list --resource-group ${RG} --gateway-name ${AG} | tail -n 1 | awk '{ print $4 }')
az network application-gateway probe update --resource-group ${RG} --gateway-name ${AG} --name ${AG_HP} --host '' --host-name-from-http-settings true
This script works fine running locally from my laptop but via the azure devops release pipeline I get the error:
ERROR: az network application-gateway address-pool list: error: argument --gateway-name: expected one argument
Somehow it cannot get the application gateway name when the script is running through the release pipeline.
Again, when running this script locally it works fine. Anyoone an idea of what I maybe missing here or can try?
I created the script on WSL Ubuntu and used a ubuntu hosted agent to publish the artifacts and also use a hosted ubuntu agent to deploy the script.

The error shows the problem directly. Your parameter "AG" is empty. You can get the parameter "AG" with the CLI command:
az network application-gateway list -g nancyweb --query "[].name" -o tsv
Or as you want with the output format table:
az network application-gateway list -g nancyweb -o table | tail -n 1 | awk '{print $3}'
You can get more details about az network application-gateway list. But if you want to get the specific one there is a point you should pay attention to because the list command shows all the application gateways.

Related

I need to list all resources in all subscriptions

I need to list all resources in all RGRPs on all subscriptions.
all what is there basically.
I try to do it with regex but does not work.
Get all resources on all subscriptions:
#! /bin/bash
for sub in $(az account list --query [].name -o tsv); do
az resource list -o tsv --subscription $sub 2>/dev/null
done
Check if your resource exists and print subscription of it
#! /bin/bash
for sub in $(az account list --query [].name -o tsv); do
az resource list -o tsv --subscription $sub 2>/dev/null --query [].name -o tsv 2>/dev/null | grep -i $1 && echo "SUBSCRIPTION: $sub" && exit
done
Let me know if there is simpler way.
Cheers
You can use a resource graph query (kusto/kcl) for that
Resources
| project name, type, location
| order by name asc
See also here:
https://learn.microsoft.com/en-us/azure/governance/resource-graph/samples/starter?tabs=azure-cli#list-resources
PowerShell:
Search-AzGraph -Query "Resources | project name, type, location | order by name asc"

How to get VM names under backend address pools of load balancer of resource group in azure

I have tried below command,to get vmname and backend pool name to excel sheet. but its showing only VMs but not backend pool name.
while read rgName
do
vmlist=$(az vm list -g $rgName --query [].name -o tsv)
for vm in $vmlist
do
nicId=$(az vm show -g $rgName -n $vm --query networkProfile.networkInterfaces[].id -o tsv)
backendPoolId=$(az network nic show --ids $nicId --query ipConfigurations[].loadBalancerBackendAddressPools[].id -o tsv)
backendPoolName=${backendPoolId##*/}
done
echo $vmlist,$backendPoolName >> test.csv
done < ilb_group
To retrieve the VMs and backendPoolName, you could use the following bash scripts.
#!/bin/bash
rgName=nancylbrg
vmlist=$(az vm list -g $rgName --query [].name -o tsv)
for vm in $vmlist
do
nicId=$(az vm show -g $rgName -n $vm --query networkProfile.networkInterfaces[].id -o tsv)
backendPoolId=$(az network nic show --ids $nicId --query ipConfigurations[].loadBalancerBackendAddressPools[].id -o tsv)
backendPoolName=${backendPoolId##*/}
done
echo $vmlist,$backendPoolName
Result

AZ CLI - List all resource group empty

I am trying to list all resource group without any resources inside like disks, vm, ip address and others.
I have found this scirpt which is listing only resource group with resources but I want the opposite that is to list all resource group not contains ressources.
for i in `az group list -o tsv --query [].name`; do if [ "$(az resource list -g $i -o tsv)" ]; then echo "$i is not empty"; fi; done
You can just do with this simple command
az group list --query [].name --output json
EDIT :
i found a reference here
for i in `az group list -o tsv --query [].name`; do if [ "$(az resource list -g $i -o tsv)" ]; then echo "$i is not empty"; else az group delete -n $i -y --no-wait; fi; done
instead of delete you can print them

Storing Output of Azure CLI command as variable

I need to store the output of an AZ cli commands that fetches my private IPs as a variable.
I am using the following in a bash script:
echo "Fetching Monitoring Server IP"
SERVER_IP=$(az vm show -n ${THIS_VM_NAME} -g ${RSC_GRP_NAME} --query privateIps -o tsv)
echo "$SERVER_IP
It would appear that this isnt working as when I echo the variable, it comes back empty.
+ THIS_VM_NAME=XXXX-XX-XX-XX-XX
+ echo 'Fetching Monitoring Server IP'
Fetching Monitoring Server IP
++ az vm show -n XXXX-XX-XX-XX-XX3 -g XXXX-XX-XX-XX-XX --query privateIps -o tsv
+ SERVER_IP=
+ echo ''
I will appreciate any pointers on this
Edit
The command you post lost a parameter to get the private IPs, you can use the command with the parameter -d or --show-details like this:
az vm show -g resourceGrouName -n vmName -d
But this command just gets all the IPs including the secondary IP.
You can get all the VM primary IPs of each interface through a shell script like this:
count=0
while : ; do
nic=$(az vm nic list -g resourceGroupName --vm-name vmName --query [$count].id -o tsv)
if [[ $nic == '' ]]; then
break
fi
privateIps[$count]=$(az vm nic show -g resourceGroupName --vm-name vmName --nic $nic --query ipConfigurations[0].privateIpAddress -o tsv)
let count++
done
echo ${privateIps[*]}
A solution to my similar problem in Azure Cloud Shell was putting a dollar sign before your SERVER_IP variable if Powershell in chosen.
$SERVER_IP=$(az vm show --name vmname --resource-group rgname --show-details --query [publicIps] --output tsv)

How to assign Tags to all VMs only in a resource group using Azure cli

I am trying to write an Azure CLI script that logs me into the portal using service principal, selects the subscription and then tag VMs in that subscription per Resource group only.
az login -u $service_principal_ID -p $service_principal_password --service-principal --tenant $tenant_ID
az account set --subscription $subID
az resource tag --resource-group $rg \
--tags tags.project=$project tags.owner=$owner tags.environment=$env \
--resource-type "Microsoft.Compute/virtualMachines" \
--output tsv
Azure gives me error when running this saying --name is missing, but I don't want to tag the VMs one by one.
Any Ideas?
I haven't actually tried this (I don't need any resources tagging ;) )
but something like this should get you working.
Basically, you need to pass that named parameter, and the only way to do that is to parse through the VM list first.
sample=$(az vm list --resource-group $rg )
for row in $(echo "${sample}" | jq -r '.[] | #base64'); do
_jq() {
echo ${row} | base64 --decode | jq -r ${1}
}
VMName=$(_jq '.name')
az resource tag --resource-group $rg \
--tags tags.project=$project tags.owner=$owner tags.environment=$env \
--resource-type "Microsoft.Compute/virtualMachines" \
--name $VMName \
--output tsv
done
Entirely unrelated, and this should probably be edited out - but damn, I do miss how easy this stuff is in PowerShell.
Add desired tags to the resource group:
az group update -n $rg \
--set tags.project=$project tags.owner=$owner tags.environment=$env
Then apply all the tags from that resource group to each child resource:
do
jsontag=$(az group show -n $rg --query tags)
t=$(echo $jsontag | tr -d '"{},' | sed 's/: /=/g')
r=$(az resource list -g $rg --query [].id --output tsv)
for resid in $r
do
az resource tag --tags $t --id $resid
done
done

Resources