简体   繁体   English

Azure 使用 ARM 模板标记资源组

[英]Azure tagging a resource group using an ARM template

How I can tagging a Azure resource group using an ARM template and use Azure DevOps task Azure Deployment: Create Or Update Resource Group. How I can tagging a Azure resource group using an ARM template and use Azure DevOps task Azure Deployment: Create Or Update Resource Group. I have error No HTTP resource was found that matches the request URI我有错误没有找到与请求 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

error No HTTP resource was found that matches the request URI ' https://management.azure.com/subscriptions/subscriptionsID/resourcegroups/resourcegroupsNeme/providers/Microsoft.Resources/resourceGroups/resourcegroupsNeme?api-version=2018-05-01 '. error No HTTP resource was found that matches the request URI ' https://management.azure.com/subscriptions/subscriptionsID/resourcegroups/resourcegroupsNeme/providers/Microsoft.Resources/resourceGroups/resourcegroupsNeme?api-version=2018-05-01 ' .


Thanks.谢谢。

What you're trying to achieve is referred to as a subscription level deployment.您要实现的目标称为订阅级别部署。 If you are trying to deploy this through a template via the portal I'm afraid you're out of luck.如果您尝试通过门户通过模板部署它,恐怕您不走运。 The deployment UI insists you specify a resource group to deploy in to which invalidates the API path routing when making the call to create your resource group.部署 UI 坚持要求您指定要部署到的资源组,在调用创建资源组时,API 路径路由无效。

To overcome this you'll need to use PowerShell or the Azure CLI.要克服这个问题,您需要使用 PowerShell 或 Azure CLI。

Another issue with subscription level deployments is that you can't use the resourceGroup() function, so your template will need be adjusted.订阅级别部署的另一个问题是您不能使用 resourceGroup() function,因此需要调整您的模板。

Your template should be:你的模板应该是:

{
    "$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')]"
        }
    }
}

You can save this file to your local machine and deploy as follows.您可以将此文件保存到本地计算机并按如下方式进行部署。

PowerShell: PowerShell:

New-AzDeployment -TemplateFile '<path-to-template>' -Location 'West US'

Azure CLI: Azure CLI:

az deployment create --template-file "<path-to-template>" --location "US West"

You will see a warning about upcoming breaking changes.您将看到有关即将发生的重大更改的警告。 Microsoft are introducing a mandatory parameter ScopeType to the PowerShell, and scope-type to the CLI with four possible values - ResourceGroup, Subscription, ManagementGroup and Tenant. Microsoft 将强制参数 ScopeType 引入 PowerShell,并将 scope-type 引入具有四个可能值的 CLI - ResourceGroup、Subscription、ManagementGroup 和 Tenant。 In this instance, you would set ScopeType to Subscription, but you can see where Microsoft are going with the others.在这种情况下,您可以将 ScopeType 设置为 Subscription,但您可以看到 Microsoft 与其他人一起去哪里。

I expect the portal template deployment UI will be updated soon.我预计门户模板部署 UI 将很快更新。

Alternatively the ARM template can remain as is and have an additional step after to run just some simple powershell like:或者,ARM 模板可以保持原样,并在运行一些简单的 powershell 之后有一个额外的步骤,例如:

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

This would allow you to maintain the current CI/CD structure.这将允许您维护当前的 CI/CD 结构。

More information on using Powershell to update the Resource Group有关使用Powershell 更新资源组的更多信息

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

You can apply tags to the target resource group by using the "tags" resource.您可以使用“标签”资源将标签应用到目标资源组。 In this case I've used a variable to store my default tags, but you could explicitly define them as well.在这种情况下,我使用了一个变量来存储我的默认标签,但您也可以显式定义它们。

Checkout the following for more details: https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/tag-resources?tabs=json#apply-tags-to-resource-groups-or-subscriptions查看以下内容了解更多详细信息: https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/tag-resources?tabs=json#apply-tags-to-resource-groups-or -订阅

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM