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"
Related
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
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
I want to delete all images in Azure Container Registry except the last two. I was looking for an script for do so but I only find to delete images older than X days. This is not possible for my situation because some days there are a lot of images created and other days only one.
Somebody has any idea?
Modify the values for $skipLastTags & $registryName to your choice and run this script on powershell.
Note: Please verify that you have az cli installed on your local system.
$registryName = 'registryName'
$doNotDeleteTags = ''
$skipLastTags = 4
$repoArray = (az acr repository list --name $registryName --output json | ConvertFrom-Json)
foreach ($repo in $repoArray)
{
$tagsArray = (az acr repository show-tags --name $registryName --repository $repo --orderby time_asc --output json | ConvertFrom-Json ) | Select-Object -SkipLast $skipLastTags
foreach($tag in $tagsArray)
{
if ($donotdeletetags -contains $tag)
{
Write-Output ("This tag is not deleted $tag")
}
else
{
az acr repository delete --name $registryName --image $repo":"$tag --yes
}
}
}
If you need this in bash.
The variable delete_from is 1-index based, i.e. if you specify the value 1, all images will be deleted.
A value of 3 keeps the 2 latest images.
#!/bin/bash -e
acr='your_acr'
repos=('repo1' 'repo2' 'repoN')
delete_from=3
for repo in "${repos[#]}"; do
tags_to_delete=$(echo $(az acr repository show-tags -n ${acr} --repository ${repo} --orderby time_desc --output tsv) | cut -d ' ' -f${delete_from}-)
for tag_to_delete in ${tags_to_delete}; do
az acr repository delete --yes -n ${acr} --image ${repo}:${tag_to_delete}
done
done
I am unable to test it right now but this little PowerShell script should work:
$acrName = 'YourACRName'
$repo = az acr repository list --name $acrName
$repo | Convertfrom-json | Foreach-Object {
$imageName = $_
(az acr repository show-tags -n $acrName --repository $_ |
convertfrom-json )| Select-Object -SkipLast 2 | Foreach-Object {
az acr repository delete --yes -n $acrName --image "$imageName:$_"
}
}
It retrieves all tags for each repository, skips the last 2, then it iterates over each tag and deletes it.
Please test it in some kind of test environment first.
If with "the last two" you mean the newest two, then this should do the trick:
az acr repository show-manifests --name your_acr --repository your_repo --orderby time_desc -o tsv --query '[].digest' | sed -n '3,$ p' | xargs -I% az acr repository delete --name your_acr --image your_repo#% --yes
You can use the build in acr purge command:
Powershell
$subscription = "your-subscription-id"
$registry = "your-registry"
$PURGE_CMD = "acr purge --filter 'acs-weather-api:.*' --keep 2 --ago 0d --untagged"
az acr run --cmd $PURGE_CMD --registry $registry --subscription $subscription /dev/null
Bash
SUBSCRIPTION="your-subscription-id"
REGISTRY="your-registry"
PURGE_CMD="acr purge --filter 'acs-weather-api:.*' --keep 2 --ago 0d --untagged"
az acr run --cmd "$PURGE_CMD" --registry "$REGISTRY" --subscription "$SUBSCRIPTION" /dev/null
Docs
Based on the answer from #Christian Holm Jørgensen
Now you can have list to be saved (save_list).
#!/bin/bash -e
acr='MY_ACR_NAME'
repos=('MY_REPO') # 'repo2' 'repoN')
save_list="0.2.0-alpha.1 latest 1.0.0"
string_remove_pattern() {
echo "${1//$2}"
}
for repo in "${repos[#]}"; do
tags_available=$(echo $(az acr repository show-tags -n "${acr}" --repository "${repo}" --orderby time_desc --output tsv))
for to_save in $save_list; do
tags_available=$(string_remove_pattern "$tags_available" "$to_save")
done
tags_to_delete=$tags_available
echo -e "The follow image, from ACR $acr and repos $repos, will be deleted:\n$tags_to_delete"
read -rp "Is the list of image to delete correct? (Y/N)" answer
if [ "$answer" == "Y" ]; then
for tag_to_delete in ${tags_to_delete}; do
az acr repository delete --yes -n "${acr}" --image "${repo}":"${tag_to_delete}"
done
fi
done
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)
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