【问题标题】:How to delete all Azure resource groups except one如何删除除一个之外的所有 Azure 资源组
【发布时间】:2020-07-13 23:39:49
【问题描述】:

我想删除除云壳使用的资源组之外的所有资源组。

$ az group list --query [].name --output tsv | grep -i -v cloud >> resGroups.txt

$ while read RGS; do echo $RGS;done<resGroups.txt
DefaultResourceGroup-EUS
NetworkWatcherRG

$ while read RGS; do az group delete -n $RGS --no-wait -y;done<resGroups.txt
validation error: Parameter 'resource_group_name' must conform to the following pattern: '^[-\\w\\._\\(\\)]+$'.
validation error: Parameter 'resource_group_name' must conform to the following pattern: '^[-\\w\\._\\(\\)]+$'.

$ while read RGS; do echo $RGS | sha256sum ;done<resGroups.txt
8ea64f061e7d52cd16b4c8a89e55ce06275c538ed91a0544b9db720281350dc2  -
b3bc010b955260f143c4b2d860020b36de391bddcbbdf31ce5a608d705f25829  -

$ echo DefaultResourceGroup-EUS | sha256sum
c06416f056e76d1c6642a3df6dd646a91b46ee40791b04db32d9c374e7ab790d  -

$ echo NetworkWatcherRG | sha256sum
c0aee8c9e5d3be7fe8a0748031ca1f5f762b2f6aa6a5e3de95dc0b1dbbc54120  -

根据错误并仅执行回显,看起来循环正在工作,但名称打印到文件的方式有问题。

我不明白为什么哈希不匹配。

xxxxxxxxxxxxxxxxxxxxxxxxxxxxx解决方案xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

感谢大家的投入,这让我开始思考,我意识到问题在于我如何输出字符串。

这不起作用,因为它在每个字符串的末尾添加了一个制表符:

notRG=$(az group list --query "[?contains(name, 'cloud')].name" --output tsv)

这将删除您 Azure 帐户中的所有资源组,但包含词云的任何资源组除外:

notRG=$(az group list --query "[?contains(name, 'cloud')].name" --output json | jq .[] -r)
for i in $(az group list --query "[?name!='$notRG'].name" --output json | jq .[] -r); do az group delete -n $i -y; done

当我开始循环时:

$ az group list | grep -i "name\|state"
    "name": "myResourceGroup",
      "provisioningState": "Deleting"
    "name": "DefaultResourceGroup-EUS",
      "provisioningState": "Succeeded"
    "name": "MC_myResourceGroup_myAKSCluster_eastus",
      "provisioningState": "Deleting"
    "name": "NetworkWatcherRG",
      "provisioningState": "Succeeded"
    "name": "cloud-shell-storage-westus",
      "provisioningState": "Succeeded"

循环完成后:

$ az group list | grep -i "name\|state"
    "name": "cloud-shell-storage-westus",
      "provisioningState": "Succeeded"

【问题讨论】:

  • 不确定您是否有理由使用将第一部分输出到文件。你有没有看过JQ。它非常适合处理 json,您可以直接从中进行循环。因此,您可以轻松地将 JSON 保存到文件中,并由 JQ 处理并将其用于循环。 starkandwayne.com/blog/bash-for-loop-over-json-array-using-jq

标签: azure azure-cli azure-resource-group


【解决方案1】:

删除具有名称的资源组:

az group delete --name ExampleResourceGroup

我试过!("groupName")^((?!groupName).)*$替换ExampleResourceGroup,但是还是报错(validation error)。

所以我使用if-statement 来排除。您可以在循环中添加代码。

groupName='xxxxxxxx';
if [ $groupName!='the group name that you want to except' ]; then az group delete -n=$groupName; fi;

我用az group show 试试这个,效果很好。

【讨论】:

  • 这对我也不起作用 :( 原来问题在于 Azure CLI 如何输出字符串。
【解决方案2】:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx解决方案xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

感谢大家的投入,这让我开始思考,我意识到问题在于我如何输出字符串。

这不起作用,因为它在每个字符串的末尾添加了一个制表符:

notRG=$(az group list --query "[?contains(name, 'cloud')].name" --output tsv)

这将删除您 Azure 帐户中的所有资源组,但包含词云的任何资源组除外:

notRG=$(az group list --query "[?contains(name, 'cloud')].name" --output json | jq .[] -r)
for i in $(az group list --query "[?name!='$notRG'].name" --output json | jq .[] -r); do az group delete -n $i -y; done

当我开始循环时:

$ az group list | grep -i "name\|state"
    "name": "myResourceGroup",
      "provisioningState": "Deleting"
    "name": "DefaultResourceGroup-EUS",
      "provisioningState": "Succeeded"
    "name": "MC_myResourceGroup_myAKSCluster_eastus",
      "provisioningState": "Deleting"
    "name": "NetworkWatcherRG",
      "provisioningState": "Succeeded"
    "name": "cloud-shell-storage-westus",
      "provisioningState": "Succeeded"

循环完成后:

$ az group list | grep -i "name\|state"
    "name": "cloud-shell-storage-westus",
      "provisioningState": "Succeeded"

【讨论】:

    猜你喜欢
    • 2019-08-11
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 2022-09-22
    相关资源
    最近更新 更多