Azure使用ARM模板标记资源组

8iwquhpp  于 2023-01-21  发布在  其他
关注(0)|答案(3)|浏览(178)

如何使用ARM模板标记Azure资源组并使用Azure DevOps任务Azure部署:创建或更新资源组。出现错误未找到与请求URI匹配的HTTP资源

{
  "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    
    "customTags": {
      "type": "object",
      "defaultValue": {
        "Environment Name": "TRdev",
        "Environment Type":"Dev",
        "Product family":"RT"
      }
    },
    "rgName": {
      "type": "string",
      "defaultValue": "dev-rg",
      "metadata": {
        "description": "Name of the resourceGroup to create"
      }
    },
    "serverfarms_environment_sp_sku": {
        "defaultValue": "B1",
        "allowedValues": [ "B1", "S1", "P1V2", "P2V2", "P3V2"],
        "type": "String"
    },
    "serverfarms_environment_sp_name": {
        "defaultValue": "dev-sp",
        "type": "String"
    },
    "sites_environment_api_name": {
        "defaultValue": "dev-api",
        "type": "String"
    },
    "sites_environment_ui_name": {
        "defaultValue": "dev-ui",
        "type": "String"
    }   
  },
  "variables": { },
  "resources": [
         {
      "type": "Microsoft.Resources/resourceGroups",
      "apiVersion": "2018-05-01",
      "location": "West US",    
      "name": "[parameters('rgName')]",            
      "tags": "[parameters('customTags')]",
      "properties": {}
   },
    {
      "apiVersion": "2019-08-01",
      "name": "[parameters('rgName')]",
      "type": "Microsoft.Resources/deployments",
      "resourceGroup": "[parameters('rgName')]",
      "tags": "[parameters('customTags')]",
      "properties": {
        "mode": "Incremental",
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
          "contentVersion": "1.0.0.1",
          "parameters": {},
          "resources": [
                {
                "apiVersion": "2018-02-01",
                "type": "Microsoft.Web/serverfarms",
                "kind": "app",
                "name": "[parameters('serverfarms_environment_sp_name')]",
                "location": "[resourceGroup().location]",
                "tags": "[parameters('customTags')]",
                "properties": {},
                "dependsOn": [],
                "sku": {
                    "name": "[parameters('serverfarms_environment_sp_sku')]"
                    }
                },
                {
                "apiVersion": "2018-11-01",
                "type": "Microsoft.Web/sites",
                "kind": "app",
                "name": "[parameters('sites_environment_api_name')]",
                "location": "[resourceGroup().location]",
                "tags": "[parameters('customTags')]",
                "properties": {
                    "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('serverfarms_environment_sp_name'))]"
                },
                "dependsOn": [
                    "[resourceId('Microsoft.Web/serverfarms', parameters('serverfarms_environment_sp_name'))]"
                ]
                },
                {
                "apiVersion": "2018-11-01",
                "type": "Microsoft.Web/sites",
                "kind": "app",
                "name": "[parameters('sites_environment_ui_name')]",
                "location": "[resourceGroup().location]",
                "tags": "[parameters('customTags')]",
                "properties": {
                    "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('serverfarms_environment_sp_name'))]"
                },
                "dependsOn": [
                    "[resourceId('Microsoft.Web/serverfarms', parameters('serverfarms_environment_sp_name'))]"
                ]
                }
          ]
        },
        "parameters": {}
      }
    }
  ],
  "outputs": {
    "sites_environment_api_name": {
      "type": "string",
      "value": "[parameters('sites_environment_api_name')]"
    },
    "sites_environment_ui_name": {
      "type": "string",
      "value": "[parameters('sites_environment_ui_name')]"
    }
  }
}


Error

错误未找到与请求URI "https://management. azure. com/subscriptions/订阅ID/资源组/资源组Neme/providers/Microsoft. Resources/资源组/资源组Neme?api-version = 2018 - 05 - 01"匹配的HTTP资源。
谢谢。

3hvapo4f

3hvapo4f1#

{
  "type": "Microsoft.Resources/tags",
  "name": "default",
  "apiVersion": "2021-04-01",
  "properties": {
    "tags": "[variables('resourceTags')]"
  }
}

你可以使用“tags”资源将标签应用到目标资源组。在这个例子中,我使用了一个变量来存储我的默认标签,但是你也可以显式地定义它们。
查看以下内容以了解更多详细信息:https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/tag-resources?tabs=json#apply-tags-to-resource-groups-or-subscriptions

g52tjvyc

g52tjvyc2#

您尝试实现的是订阅级别部署。如果您尝试通过门户的模板进行部署,恐怕您的运气不好。部署UI要求您指定要部署到的资源组,在调用创建资源组时,API路径路由将无效。
要克服这一点,您需要使用PowerShell或Azure CLI。
订阅级别部署的另一个问题是您不能使用resourceGroup()函数,因此需要调整模板。
您的模板应为:

{
    "$schema":"https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
    "contentVersion":"1.0.0.0",
    "parameters":{
        "customTags":{
            "type":"object",
            "defaultValue":{
                "Environment Name":"TRdev",
                "Environment Type":"Dev",
                "Product family":"RT"
            }
        },
        "rgName":{
            "type":"string",
            "defaultValue":"dev-rg",
            "metadata":{
                "description":"Name of the resourceGroup to create"
            }
        },
        "serverfarms_environment_sp_sku":{
            "defaultValue":"B1",
            "allowedValues":[
                "B1",
                "S1",
                "P1V2",
                "P2V2",
                "P3V2"
            ],
            "type":"String"
        },
        "serverfarms_environment_sp_name":{
            "defaultValue":"dev-sp",
            "type":"String"
        },
        "sites_environment_api_name":{
            "defaultValue":"dev-api",
            "type":"String"
        },
        "sites_environment_ui_name":{
            "defaultValue":"dev-ui",
            "type":"String"
        }
    },
    "variables":{

    },
    "resources":[
        {
            "type":"Microsoft.Resources/resourceGroups",
            "apiVersion":"2018-05-01",
            "location":"West US",
            "name":"[parameters('rgName')]",
            "tags":"[parameters('customTags')]",
            "properties":{

            }
        },
        {
            "apiVersion":"2019-08-01",
            "name":"[parameters('rgName')]",
            "type":"Microsoft.Resources/deployments",
            "resourceGroup":"[parameters('rgName')]",
            "tags":"[parameters('customTags')]",
            "dependsOn": [
                "[resourceId('Microsoft.Resources/resourceGroups/', parameters('rgName'))]"
            ],
            "properties":{
                "mode":"Incremental",
                "template":{
                    "$schema":"https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
                    "contentVersion":"1.0.0.1",
                    "resources":[
                        {
                            "apiVersion":"2018-02-01",
                            "type":"Microsoft.Web/serverfarms",
                            "kind":"app",
                            "name":"[parameters('serverfarms_environment_sp_name')]",
                            "location":"[resourceGroup().location]",
                            "tags":"[parameters('customTags')]",
                            "properties":{

                            },
                            "dependsOn":[

                            ],
                            "sku":{
                                "name":"[parameters('serverfarms_environment_sp_sku')]"
                            }
                        },
                        {
                            "apiVersion":"2018-11-01",
                            "type":"Microsoft.Web/sites",
                            "kind":"app",
                            "name":"[parameters('sites_environment_api_name')]",
                            "location":"[resourceGroup().location]",
                            "tags":"[parameters('customTags')]",
                            "properties":{
                                "serverFarmId":"[resourceId('Microsoft.Web/serverfarms', parameters('serverfarms_environment_sp_name'))]"
                            },
                            "dependsOn":[
                                "[resourceId('Microsoft.Web/serverfarms', parameters('serverfarms_environment_sp_name'))]"
                            ]
                        },
                        {
                            "apiVersion":"2018-11-01",
                            "type":"Microsoft.Web/sites",
                            "kind":"app",
                            "name":"[parameters('sites_environment_ui_name')]",
                            "location":"[resourceGroup().location]",
                            "tags":"[parameters('customTags')]",
                            "properties":{
                                "serverFarmId":"[resourceId('Microsoft.Web/serverfarms', parameters('serverfarms_environment_sp_name'))]"
                            },
                            "dependsOn":[
                                "[resourceId('Microsoft.Web/serverfarms', parameters('serverfarms_environment_sp_name'))]"
                            ]
                        }
                    ]
                },
            }
        }
    ],
    "outputs":{
        "sites_environment_api_name":{
            "type":"string",
            "value":"[parameters('sites_environment_api_name')]"
        },
        "sites_environment_ui_name":{
            "type":"string",
            "value":"[parameters('sites_environment_ui_name')]"
        }
    }
}

您可以将此文件保存到本地计算机并按如下所示进行部署。
动力 shell :
New-AzDeployment -TemplateFile '<path-to-template>' -Location 'West US'
Azure客户端:
az deployment create --template-file "<path-to-template>" --location "US West"
您将看到有关即将进行的重大更改的警告。Microsoft正在PowerShell中引入一个强制参数ScopeType,并在CLI中引入一个具有四个可能值的scope-type-ResourceGroup、Subscription、ManagementGroup和Tenant。在此示例中,您将ScopeType设置为Subscription,但您可以看到Microsoft将如何使用其他参数。
我希望门户模板部署UI将很快更新。

vawmfj5a

vawmfj5a3#

或者,ARM模板可以保持原样,并在运行一些简单的powershell之后执行一个额外的步骤,如:

Set-AzResourceGroup -Name "YOURRESOURCEGROUPNAME" -Tag @{Department="IT"}

这将允许您保持当前的CI/CD结构。
有关使用Powershell to update the Resource Group的详细信息

相关问题