I am trying to "explore" json results from an Azure CLI command using the --query switch (e.g. az functionapp list --query <something>), and to get started I'd like the length of the resulting array.
The Azure CLI help says nothing specific, and points to jmespath.org which does indeed show that a length function exists, however it seems to require an argument. I have no name for the argument, which is the root/outermost array returned by the list command.
It seems from jmespath.org that length(something) is what I want, but I don't know what to put in for the "something" part. What do I put here? Or am I going about this all wrong??
As we know az functionapp list returns a json where the root node is an array. In order to get the length of this array we can use the following syntax:
az functionapp list --query "[] | length(#)"
Related
I am trying to leverage Powershell with Azure CLI commands to create variables in an Azure DevOps variable group.
This is typically done using this command:
az pipelines variable-group variable create --group-id "variablegroupid" --name "nameofthevariabletoadd" --value "valueforthevariable"
Since this command is run in a powershell task in my AzureDevOps pipeline, the value comes from a previous step in the pipeline
In short lets assume that the name of the variable (coming from the previous step) is vmStorageAccountName then the following command works perfectly:
az pipelines variable-group variable create --group-id "variablegroupid" --name "nameofthevariabletoadd" --value $(StepsOutputs.vmStorageAccountName)
StepsOutputs being the name of the previouspipeline step.
However since I am getting multiple outputs from the previous step I don't want to manually modify the value name multiple times.
So I have a list of 3 outputs, for example:
$outputs = 'vmstorageAccountname', 'VnetName', 'Rgname'
I tried the following classic loop:
foreach ($output in $outputs) {
az pipelines variable-group variable create --group-id "variablegroupid" --name "nameofthevariabletoadd" --value "$("StepsOutputs.$output")"
But when doing that it takes StepsOutputs.vmstorageAccountname string (or any of the other outputs) as the value for the variable to be created in the variable group. When what I was expecting was to get the vmstorageaccountname itself as we do when not using the loop.
So I guess there is something wrong in the way I am filling the value argument in the command, using a variable inside a variable. I have trying many ways of writing the value arugment but none of them worked. Again if I am not using a loop and the $output variable, this works fine.
To summarize, what I would like is to first have StepsOutputs.$output translated to something like StepOutputs.vmstorageaccountname and then the value should be filled using $(StepOutputs.vmstorageaccountname)
I hope that is clear.
Thank you for your help.
Glad that #user2154222 for solved the issue. Thank you #mclayton for your suggestions that helped to fix the issue. Posting this on behalf of your discussion so that it will be beneficial for other community members.
Using single quotes fix your issue
# with single quotes
# without using additional string while passing value
az pipelines variable-group variable create --group-id "variablegroupid" --name "nameofthevariabletoadd" --value '$($StepsOutputs.vmStorageAccountName)'
Adding some more information to fix the issue:
If you are adding some additional string in your pipeline --value, you have to keep (double quotes) "". or just pass the variable value.
# without quotes
# without using additional string while passing value
az pipelines variable-group variable create --group-id "variablegroupid" --name "nameofthevariabletoadd" --value $($StepsOutputs.vmStorageAccountName)
#using Double Quotes
# using additional string as TESTAPP-$($StepsOutputs.vmStorageAccountName)
az pipelines variable-group variable create --group-id "variablegroupid" --name "nameofthevariabletoadd" --value "TESTAPP-$($StepsOutputs.vmStorageAccountName)"
So I am trying to get the ID of all VMs across all subscriptions and regions, where a specific tag is null. For this I am using the following command
az vm list -d --query '[?!not_null(tags.run)]|[].id'
Please note: I want to get the ids only if the tag doesn't exist
Here notice I need to use single quotes to cover the query as I am using the '!' operator to inverse the not_null() function. If I were to use double quotes bash will throw an event not found error.
So now the problem arises when I also want to add a condition to check the current state of the VM and return id only if it is running and tag doesn't exist.
az vm list -d --query '[?!not_null(tags.run)] | [?powerState=="VM running"].id'
Here I have to wrap VM running in double quotes and this gives me an empty output as the string is not being matched because the query expects single quotes like so -
"[?powerState=='VM running'].id"
Could someone help me with a workaround for this?
Use raw string literals for VM running string. You just have to surround your string with a back tick and a double quote.
az vm list -d --query '[?!not_null(tags.run)]|[?powerState==`"VM running"`].id'
I need a list of RDS DBInstanceIdentifier that match the String "foobar" in their name. I found many solutions with exact match, but not substring matching. My approach looks as follows:
I get a list of all DBInstanceIdentifier using:
aws rds describe-db-instances --query "DBInstances[*].[DBInstanceIdentifier][]"
which looks like
[
"machine-001-alice-abcdefg",
"machine-002-bob-abcdefg",
"machine-003-foobar-abcdefg"
]
On the list I apply a filter like in the last example of the JMSES Tutorial
aws rds describe-db-instances --query "DBInstances[*].[DBInstanceIdentifier][]|[?contains(#,'dev') =='true']"
If I change the statement to != I get the full list, so it seems I have the filter statement wrong.
true needs to be backticked not quoted and then the backticks need to be escaped it seems - different shells may vary.
aws rds describe-db-instances --query "DBInstances[*].[DBInstanceIdentifier][]|[?contains(#,'dev')==\`true\`]"
aws rds describe-db-instances --query "DBInstances[*].[DBInstanceIdentifier][]|[?contains(#,'dev')!=\`true\`]"
You can also omit the comparison to true but I couldn't invert this successfully
aws --profile pollen-nonprod rds describe-db-instances --query "DBInstances[*].[DBInstanceIdentifier][]|[?contains(#,'dev')]"
(I'd normally do this sort of thing with jq but that's a different solution rather than necessarily a better one)
In Azure powershell, the json format is annoying at times. Need to scroll backwards and de-crypt .
How do i change the default to be --output table for all az commands?
In Azure powershell, type the following inorder-to change the default output to table
az configure
it will prompt you with following question. Select 3
Do you wish to change your settings? (y/N): y
What default output format would you like?
[1] json - JSON formatted output that most closely matches API responses.
[2] jsonc - Colored JSON formatted output that most closely matches API responses.
[3] table - Human-readable output format.
..
Please enter a choice [Default choice(1)]: 3
This is for users who doesn't like the default json format
Since you've given a solution on how to configure default table output for Azure CLI, this question should also have a solution for Azure PowerShell.
The easiest solution I can think of is just piping the output to Format-Table e.g. Get-AzResourceGroup | Format-Table -AutoSize.
You can have a look for more information at Format Azure PowerShell cmdlet output.
first of all thanks for taking the time in helping me out on this one.
I have a 12300 long list of snapshots, working on deleting certain snapshots, so im trying to list them all first thru the CLI.
I want to get the SnapshotID, the StartTime, and from the tags, the 'Name'
I tried quite a few querys, but all of them result in null on the name :/
THsi is my latest one:
aws ec2 describe-snapshots --query 'Snapshots[*].{ID:SnapshotId,Time:StartTime,Name:Tags[?Key=='Name'].Value[*]}'
Is this something one can do? or should i query all Key pairs, and then filter them out with --filters ?
Few issues to be considered:
Beware of the type of quote marks around the Key Names(backticks, not single quotes)
Forcing a single value out of the tag array.
You should specify the --owner-ids otherwise all accessible snapshots will be listed (including ones that don't belong to your account)
This command works:
aws ec2 describe-snapshots --query 'Snapshots[*].{ID:SnapshotId,Time:StartTime,Name:Tags[?Key==`Name`]|[0].Value}' --owner-ids *<YOUR-ACCOUNT-ID>*