英文:
Unable to filter Azure resource using tags through cli
问题
我有多个具有标签 site:prod
的命名空间资源
az tag list --resource-id "subscriptions/[subsc_id]/resourceGroups/[rg_id]/providers/Microsoft.ServiceBus/namespaces/[ns_name]"
{
"id": "/subscriptions/[subsc_id]/resourceGroups/[rg_id]/providers/Microsoft.ServiceBus/namespaces/[ns_name]/providers/Microsoft.Resources/tags/default",
"name": "default",
"properties": {
"tags": {
"module": "payment",
"site": "prod"
}
},
"resourceGroup": "[rg_id]",
"type": "Microsoft.Resources/tags"
}
我想要检索所有具有上述 site:prod
标签的资源,使用以下 cli 命令运行:
az servicebus namespace list --subscription "[subsc_id]" --query '[?tags.site=="prod"]'
然而,运行上述命令产生完全相反的输出,没有一个返回的命名空间对象带有标签 site:prod
。
我在这里做错了什么?
英文:
I have multiple namespace resources with tag site:prod
az tag list --resource-id "subscriptions/[subsc_id]/resourceGroups/[rg_id]/providers/Microsoft.ServiceBus/namespaces/[ns_name]"
{
"id": "/subscriptions/[subsc_id]/resourceGroups/[rg_id]/providers/Microsoft.ServiceBus/namespaces/[ns_name]/providers/Microsoft.Resources/tags/default",
"name": "default",
"properties": {
"tags": {
"module": "payment",
"site": "prod"
}
},
"resourceGroup": "[rg_id]",
"type": "Microsoft.Resources/tags"
}
I want retrieve all resource such as above which have site:prod
tag using cli command, by running following:
az servicebus namespace list --subscription "[subsc_id]" --query '[?tags.site=="prod"]'
However, running above command produces completely opposite output such that none of the returned namespace objects have tag site:prod
on them.
What am I doing wrong here?
答案1
得分: 0
我在你的代码中找到了问题:你在使用 "(引号)时出现了错误:
az servicebus namespace list --subscription "Subscription name" --query "[?tags.site=='prod']"
你应该将 " 放在查询的开头,将 ' 放在查询中间。
或者,
无法通过命令行界面使用标签筛选 Azure 资源
你可以简单地使用以下命令获取所有具有标签 site=prod
的资源名称:
$s= az resource list --tag site=prod
$res=$s | ConvertFrom-json
$res.name
英文:
I found the problem in your code: you are using " (quotation) incorrectly:
az servicebus namespace list --subscription "Subscription name" --query "[?tags.site=='prod']"
You should keep " at the starting of query and ' in middle of query
Alternatively,
>Unable to filter Azure resource using tags through cli
You can simply use below command to get all the resources names which have tags site=prod
:
$s= az resource list --tag site=prod
$res=$s | ConvertFrom-json
$res.name
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论