Filtering with Azure CLI and JMESPath for network vnet peering list - azure-cli

I'm using the Azure CLI to get a list of vnet peerings: az network vnet peering list. This returns json in the following structure:
[
{
...
"name": "prefix1-name",
...
},
{
...
"name": "prefix2-name",
...
}
]
I am trying to filter the results by names starting with some prefix. I have tried various combinations of the following:
az network vnet peering list --resource-group my-rg --vnet-name my-vnet --query "[?starts_with(name,'prefix1-')].{name}"
However this always fails with a message like ].{name} was unexpected at this time.
What am I missing?

Try to use az network vnet peering list --resource-group my-rg --vnet-name my-vnet --query "[?starts_with(name,'prefix1-')].name". You do not need to include the {}in the name. This works on my side.
Edit
For the error message, I can reproduce it with these Azure CLI commands in PowerShell locally on the windows 10 machine. But this does not appear in my local Linux Bash Shell as the above screenshots. This error also does not appear on PowerShell and Bash in the Azure cloud shell.
By my validation, It looks like a specific scenario on local PowerShell. When you filter with JMESPath, I just tried that the function starts_with or contains requires a space between the function, it works like these:
--query "[?starts_with(name, 'vnet')].name"
--query "[?starts_with (name,'vnet')].name"
--query "[?starts_with(name,'vnet') ].name"
--query "[?starts_with(name,'vnet')] .name"
but this does not work as below.
--query "[?starts_with(name,'vnet')].name"

This is a Windows PowerShell issue: https://github.com/Azure/azure-cli/blob/dev/doc/use_cli_effectively.md#argument-parsing-issue-in-powershell. To workaround it, insert --% after az to force PowerShell to treat the remaining characters in the line as a literal.

The answer was apparently putting a space between the ) and ] so az network vnet peering list --resource-group my-rg --vnet-name my-vnet --query "[?starts_with(name,'prefix1-') ].name worked for me. No idea why and not found any documentation that mentions anything like this. If I remove that space I get the ].name was unexpected at this time message.

Related

Azure JMESPATH query for nested json

I'm trying to extract just the paths portion of the result of this AZ CLI query:
az network application-gateway show --query urlPathMaps --resource-group dev-aag --name dev-aag-gateway
I'm unsure if I should use az network application-gateway list instead
This is the result of the first query, however I'm not able to extract just the paths portion -- is this because paths is nested?
"backendAddressPool": {
"id": "/subscriptions/42xxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/dev-aag/providers/Microsoft.Network/applicationGateways/dev-aag-gateway/backendAddressPools/co20225020a-backend-pool",
"resourceGroup": "dev-aag"
},
"backendHttpSettings": {
"id": "/subscriptions/42xxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/dev-aag/providers/Microsoft.Network/applicationGateways/dev-aag-gateway/backendHttpSettingsCollection/dev-aag-httpsetting",
"resourceGroup": "dev-aag"
},
"etag": "W/\"9f2d3xxc-2cbd-49fr-8726-432c7ef00de7\"",
"firewallPolicy": null,
"id": "/subscriptions/42xxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/dev-aag/providers/Microsoft.Network/applicationGateways/dev-aag-gateway/urlPathMaps/dev-aag-https-routing-rule/pathRules/co20225020a-cqvgkj9xxxxx9bcu-url",
"loadDistributionPolicy": null,
"name": "co20225020a-cqvgkj9xxxxx9bcu-url",
"paths": [
"/co20225020a/cqvgkj9xxxxx9bcu/*"
],
"provisioningState": "Succeeded",
"redirectConfiguration": null,
"resourceGroup": "dev-aag",
"rewriteRuleSet": null,
"type": "Microsoft.Network/applicationGateways/urlPathMaps/pathRules"
}
I'm trying to use grep and cut like this, but maybe something else I should be using instead:
az network application-gateway show --query urlPathMaps --resource-group dev-aag --name dev-aag-gateway | grep paths | cut -d ":" -f1-19
what should I be using to make this work?
az network application-gateway list -->Can used if you want to list all the application gateways under a particular subscription.
az network application-gateway show --> should be used if you want to pull the properties of a particular app gateway.
Refer to this articles for more information about Azure CLI application gateway cmdlets.
To pull list of paths under urlPathMaps in pathRules you need to use the below JMESPath query in the az network application-gateway show cmdlet.
az network application-gateway show -n <AppGatewayName> -g <ResourceGroupName> --query urlPathMaps[].pathRules[].paths
I have tested this in Azure Cloud shell, it is working fine I would suggest you to validate from your end as well.
**Here is the Sample output screenshot for reference: **

Cannot acces instance using compute ssh : "ERROR: [....putty.exe] exited with return code [1]

Here's my problem :
I would like to connect to a gcp instance. When I run the Google Cloud SDK shell as an administrator with the command :
gcloud compute ssh my_instance --zone=europe-west1-b -- -L=8081:locahost:8081
..I get this error : ERROR (gcloud.compute.ssh) [..../putty.exe] exited with return code [1]
My instance is running with the metadata enable-oslogin as TRUE, as the project.
Do you have an idea of what is the problem ?
When using -- in the command, you are passing SSH flags after the dashes and not gcloud command flags. To explain, gcloud compute ssh is a thin wrapper around the ssh(1) command that takes care of authentication and the translation of the instance name into an IP address.
In this case, -- is equivalent to --ssh-flag as per this SDK reference. It seems that putty is outputting an error that is not passed into the command line (SDK shell). The actual error should be visible in the dialog window before putty exits.
I have tried the command myself on Windows and the exact error was unknown option "L=8081:localhost:8081". The SSH flag is not accepted as you have an = sign there (typo).
According to linuxcommand.org manual, the flag should be in this format:
-L [bind_address:]port:host:hostport
Hence, you should run the command like this:
gcloud compute ssh my_instance --zone=europe-west1-b -- -L 8081:locahost:8081
Note also that you may have to create a firewall rule to allow Ingress to the instance on port 8081.

gcloud: how to get ip addresses of group of managed instances

My problem is to create 5k instances and retrieve there public IP addresses.
Specifically for zone us-west1-a I can create a group of 50 instances by the following:
gcloud compute instance-groups managed create test --base-instance-name morning --size 50 --template benchmark-template-micro --zone us-west1-a
Questions:
How to specify the start-script to run each created instances? I can't find them here.
How to get the public IP addresses of those created instances?
the startup-script can be assigned to the template for the instance used; see here.
one can obtain information with gcloud compute instance-groups managed describe.
while there are no public IP addresses unless you'd assign external IP addresses.
As mentioned by Martin, the startup-script is configured in the instance template.
Unfortunately, there is no API that lists the ip addresses of the instances in the group. There are however APIs (and gcloud commands) to get the list of instances and the ip addresses of instances. Here is an example to fetch this information from the command line:
gcloud compute instance-groups list-instances $INSTANCE_GROUP --uri \
| xargs -I '{}' gcloud compute instances describe '{}' \
--flatten networkInterfaces[].accessConfigs[] \
--format 'csv[no-heading](name,networkInterfaces.accessConfigs.natIP)'
To speed this up, you may want to use xarg's -P flag to parallelize the instance describe requests.
Since all instances in the group have the same prefix. You can also just do a list search by prefix. Although, this may pull in another that uses the same prefix even if not part of the instance group:
gcloud compute instances list --filter='name ~ ${PREFIX}*' \
--flatten networkInterfaces[].accessConfigs[] \
--format 'csv[no-heading](name,networkInterfaces.accessConfigs.natIP)'

How to add application packages to Azure Batch task from Azure CLI?

I am trying to write a bash command line script that will create an azure batch task with an application package. The package is called "testpackage" and exists and is activated on the batch account. However, every time I create this task, I get the following error code: BlobAccessDenied.
This only occurs when I include the application-package-references option on the command line. I tried to follow the documentation here, which states the following:
--application-package-references
The space-separated list of IDs specifying the application packages to be installed. Space-separated application IDs with optional version in 'id[#version]' format.
I have tried --application-package-references "test", --application-package-references" test[1]", and --application-package-references test[1], all with no luck. Does anyone have an example of doing this properly?
Here is the complete script I am running:
#!/usr/bin/env bash
AZ_BATCH_KEY=myKey
AZ_BATCH_ACCOUNT=myBatchAccount
AZ_BATCH_ENDPOINT=myBatchEndpoint
AZ_BATCH_POOL_ID=myPoolId
AZ_BATCH_JOB_ID=myJobId
AZ_BATCH_TASK_ID=myTaskId
az batch task create \
--task-id $AZ_BATCH_TASK_ID \
--job-id $AZ_BATCH_JOB_ID \
--command-line "/bin/sh -c \"echo HELLO WORLD\"" \
--account-name $AZ_BATCH_ACCOUNT \
--account-key $AZ_BATCH_KEY \
--account-endpoint $AZ_BATCH_ENDPOINT \
--application-package-references testpackage
Ah the classic "write up a detailed SO question then immediately answer it yourself" conundrum.
All I needed was --application-package-references testpackage#1
Have a good day world.

How to capture JSON result from Azure CLI within NodeJS script

Is there a way to capture the JSON objects from the Azure NodeJS CLI from within a NodeJS script? I could do something like exec( 'azure vm list' ) and write a promise to process the deferred stdout result, or I could hijack the process.stream.write method, but looking at the CLI code, which is quite extensive, I thought there might be a way to pass a callback to the cli function or some other option that might directly return the JSON result. I see you are using the winston logger module -- I might be familiar with this, but perhaps there is a hook there that could be used.
azure vm list does have a --json option:
C:\>azure vm list -h
help: List Azure VMs
help:
help: Usage: vm list [options]
help:
help: Options:
help: -h, --help output usage information
help: -s, --subscription <id> use the subscription id
help: -d, --dns-name <name> only show VMs for this DNS name
help: -v, --verbose use verbose output
help: --json use json output
You can get the json result in the callback of an exec(...) call. Would this work for your?
Yes you can, check this gist: https://gist.github.com/4415326 and you'll see how without doing exec. You basically override the logger hanging off the CLI.
As a side note I am about to publish a new module, azure-cli-buddy that will make it easy to call the CLI using this technique and to receive results in JSON.