I am trying to filter instances based on tags. I am using the below command to list instances that have wknhscale == 'active' tag. It is working fine and returns the instance name and resource group.
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines'| where tags['wknhscale']=='active' | project name, resourceGroup"| jq '[.data[] | {name, resourceGroup}]'
Now at the same time, I want to fetch the IP address of the Instance also. So I am using the below query, But it's not giving me any data.
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines' type =~ 'Microsoft.Compute/privateIPAddresses'| where tags['wknhscale']=='active'"
CodePudding user response:
I am able to retrieve the Instances name and Private address using the below query.
Note: You might have to give subscription-based (--subscription) on how your environment variables or azure config is set.
az vm list-ip-addresses --ids $(az resource list -g test-group --query "[?type=='Microsoft.Compute/virtualMachines' && tags.wknhscale== 'active'].id" -o tsv) --query "[].{Name:virtualMachine.name, RG:virtualMachine.resourceGroup, IP:virtualMachine.network.privateIpAddresses[0]}"
Output
[
{
"IP": "11.190.0.42",
"Name": "hscalenode04",
"RG": "test-group"
},
{
"IP": "11.190.0.43",
"Name": "hscalenode03",
"RG": "test-group"
},
{
"IP": "11.190.0.44",
"Name": "hscalenode05",
"RG": "test-group"
},
{
"IP": "11.190.0.45",
"Name": "hscalenode02",
"RG": "test-group"
},
{
"IP": "11.190.0.46",
"Name": "hscalenode01",
"RG": "test-group"
}
]
CodePudding user response:
Here is how I could able to retrieve virtual machine instance name and resource group using KQL Query.
Resources
| where type =~ 'microsoft.compute/virtualmachines'
| project vmId = tolower(tostring(id)), vmName = name
| join (Resources
| where type =~ 'microsoft.network/networkinterfaces'
| mv-expand ipconfig=properties.ipConfigurations
| project vmId = tolower(tostring(properties.virtualMachine.id)), privateIp = ipconfig.properties.privateIPAddress, publicIpId = tostring(ipconfig.properties.publicIPAddress.id)
| join kind=leftouter (Resources
| where type =~ 'microsoft.network/publicipaddresses'
| project publicIpId = id, publicIp = properties.ipAddress
) on publicIpId
| project-away publicIpId, publicIpId1
| summarize privateIps = make_list(privateIp), publicIps = make_list(publicIp) by vmId
) on vmId
| project-away vmId1
| sort by vmName asc
| where array_length(publicIps)>0