Azure使用ARM模板标记资源组

时间:2019-10-21 19:43:19

标签: azure azure-resource-manager arm-template

如何使用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/subscriptionsID/resourcegroups/resourcegroupsNeme/providers/Microsoft.Resources/resourceGroups/resourcegroupsNeme?api-version=2018-05-01'相匹配的HTTP资源。


谢谢。

2 个答案:

答案 0 :(得分:0)

您要实现的目标称为订阅级别部署。如果您尝试通过门户网站通过模板进行部署,恐怕您不走运。部署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')]"
        }
    }
}

您可以将该文件保存到本地计算机,并按以下步骤进行部署。

PowerShell:

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

Azure CLI:

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会很快更新。

答案 1 :(得分:0)

或者,ARM模板可以保持原样,并且在运行诸如模板之类的简单Powershell之后还需要执行其他步骤:

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

这将允许您维护当前的CI / CD结构。

有关使用Powershell to update the Resource Group

的更多信息