Reputation: 470
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
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"
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
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
az resource list -g RESOURCEGROUPNAME --query "[?tags.Technology==`'Best Tech'`].tags.\`"Budget Line\`"" # Works in powershell
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
Views: 1770
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
Reputation: 470
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
az resource list -g RESOURCEGROUPNAME --query "[?tags.Technology==`'Best Tech'`].tags.\`"Budget Line\`"" # Works in powershell
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