miniGweek
miniGweek

Reputation: 470

Azure CLI query help - JMESPath - filter using key having space , extract keys with space in them ( from Powershell )

Good morning everyone, Happy Friday!
Edit: Ended up solving it myself while debugging through it..
Thought I will post it here regardless so that anyone who comes here may get the solution... Was helped by microsoft docs.

https://learn.microsoft.com/en-us/cli/azure/use-cli-effectively

Original question

I have Azure Resources which are tagged like :

"tags":{
"Application Name":"The Best App",
"Budget Line":"Home project",
"Technology":"Best Tech",
"Environment":"Development",
"Project":"Learn"
}

Sample json with array of resources and tags would look like

[{"name":"Resource1",
"tags": {
"Application Name":"The Best App",
"Budget Line":"Home project",
"Technology":"Best Tech",
"Environment":"Development",
"Project":"Learn"
 }},
{"name":"Resource2",
"tags": {
"Application Name":"The Best App",
"Budget Line":"Home project",
"Technology":"Best Tech",
"Environment":"Development",
"Project":"Learn"
 }},
{"name":"Resource3",
"tags": {
"Application Name":"Not App",
"Budget Line":"Home project",
"Technology":"Best Tech",
"Environment":"Development",
"Project":"Learn"
 }
}
]

So now I am looking to query all Azure Resources where the tag **Application Name** = **The Best App**.

I was going through the JMESPath tutorial and got the following syntax to work on the JMESPath tutorial website. What I tried to do : Filter a resource based on a tag value, and then extract another tag value of those resources.

[?tags."Application Name"==`"The Best App"`].tags."Budget Line"

JMES Path filter with space in key name and space in key name while projecting

Next step was to try the same query as part of an azure cli query with JMESPath filter query. Note I am running azure cli from PowerShell as I am more comfortable in it than Bash. Unable to get it to work

Solution after quite a few trial and error

Looking at the powershell examples for escaping double quotes from https://learn.microsoft.com/en-us/cli/azure/use-cli-effectively These worked : Examples are in PowerShell

Happy Friday guys! Hope it helps someone!

Upvotes: 0

Views: 1770

Answers (2)

colan
colan

Reputation: 2886

This is a totally different context, but just in case it helps somebody, the author's solution didn't work for me doing a JSON query from Ansible (which also uses JMESPath). Instead, I had to do the following:

storage_url: "{{ openstack_api_versions.stdout | from_json | json_query('[? \"Service Type\" == `object-store`].Endpoint') | first }}"

In this context, it only works without backticks.

Upvotes: 0

miniGweek
miniGweek

Reputation: 470

Solution after quite a few trial and error

Looking at the powershell examples for escaping double quotes from https://learn.microsoft.com/en-us/cli/azure/use-cli-effectively These worked : Examples are in PowerShell

  • Query an azure resource based on a tag key which does not have space in it, but project/extract the value of a tag having key name with space.
    az resource list -g RESOURCEGROUPNAME --query "[?tags.Technology==`'Best Tech'`].tags.\`"Budget Line\`"" # Works in powershell
    
  • Query an azure resource based on a tag key which has a space in it, and extract/project the value of a tag having key name with space.
    az resource list -g RESOURCEGROUPNAME --query "[?tags.\`"Budget Line\`"==`'Home Project'`].tags.\`"Application Name\`"" # Works in powershell
    

Happy Friday guys! Hope it helps someone!

Upvotes: 0

Related Questions