【发布时间】:2021-03-16 18:35:13
【问题描述】:
- 我想知道是否有可以在 JMESPATH 中利用的子字符串函数(由 az cli 支持)。
- 我有以下 az cli 请求,我只想提取具有安全组的链接子网的名称,但与其他云提供商不同,azure 不会以相同的方式存储关联的资源名称。
可以在 subnet.id 节点中提取名称,如下所示
$ az network nsg show -g my_group -n My_NSG --query "subnets[].id" -o json
[
"/subscriptions/xxxxxx2/resourceGroups/my_group/providers/Microsoft.Network/virtualNetworks/MY-VNET/subnets/My_SUBNET"
]
- 我只想从结果中提取 "MY_SUBNET"。
我知道有一种叫做搜索的东西应该模仿 子字符串(在这里解释 https://github.com/jmespath/jmespath.jep/issues/5) 但它没有 为我工作。
$ az network nsg show -g my_group -n My_NSG --query "subnets[].search(id,'@[120:-1]')" -o json
InvalidArgumentValueError: argument --query: invalid jmespath_type value: "subnets[].search(id,'@[120:-1]')"
CLIInternalError: The command failed with an unexpected error. Here is the traceback:
Unknown function: search()
谢谢
编辑:
我实际上运行了包含其他元素的请求,这就是为什么在新行中使用带有 bash 的子字符串不是我想要的。 这是完整查询的示例:
az network nsg show -g "$rg_name" -n "$sg_name" --query "{Name:name,Combo_rule_Ports:to_string(securityRules[?direction==\`Inbound\`].destinationPortRanges[]),single_rule_Ports:to_string(securityRules[?direction==\`Inbound\`].destinationPortRange),sub:subnets[].id,resourceGroup:resourceGroup}" -o json
输出
{
"Combo_rule_Ports": "[]",
"Name": "sg_Sub_demo_SSH",
"resourceGroup": "brokedba",
"single_rule_Ports": "[\"22\",\"80\",\"443\"]",
"sub": [
"/subscriptions/xxxxxxx/resourceGroups/brokedba/providers/Microsoft.Network/virtualNetworks/CLI-VNET/subnets/Sub_demo"
]
}
【问题讨论】:
-
我怀疑这是因为,与 python 不同,字符串不是 JMESPath 中的字符列表,因此您不能对一个字符串使用切片技术。
-
那太糟糕了!
标签: substring slice jmespath azure-cli2