繁体   English   中英

使用模板规范时 ARM 模板中的默认值

[英]Default Values in ARM templates when using Template Specs

不久前,我们已经为 ARM 部署到 Azure 实施了模板规范,通过这样做我们大大减少了工作量。 我们要实现的下一件事是开始在模板规范中实现默认值,因此我们不必在参数文件中指定所有项目中相同的所有参数。 如果我们确实想覆盖默认值,我们当然可以在参数文件中指定参数。

我们过去已经使用模板和参数文件处理过这个问题,但我无法让它与模板规范一起使用。

例如,我正在尝试部署这样的应用服务计划:

模板规格:

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "appServicePlanSettings": {
            "type": "object",
            "defaultValue": {
                "isHypervContainerPlan": false
            }
        },
        "resourceNameAndTagSettings": {
            "type": "object"
        }
    },
    "variables": {
        "appServicePlanName": "[concat('o', parameters('appServicePlanSettings').nameAbbr, parameters('resourceNameAndTagSettings').environmentType, parameters('resourceNameAndTagSettings').resourceGroupNumber, parameters('resourceNameAndTagSettings').solutionNameAbbr, parameters('resourceNameAndTagSettings').locationAbbr)]"
    },
    "resources": [
        {
            "comments": "App Service Plans",
            "condition": "[parameters('appServicePlanSettings').deploy]",
            "apiVersion": "2020-09-01",
            "type": "Microsoft.Web/serverfarms",
            "name": "[variables('appServicePlanName')]",
            "location": "[resourceGroup().location]",
            "tags": {
                "_Purpose": "[parameters('appServicePlanSettings').tagValuePurpose]",
                "CostCenter": "[parameters('resourceNameAndTagSettings').tagValueCostCenter]",
                "EnvironmentType": "[parameters('resourceNameAndTagSettings').tagValueEnvironmentType]",
                "Owner": "[parameters('resourceNameAndTagSettings').tagValueOwner]"
            },
            "kind": "[parameters('appServicePlanSettings').kind]",
            "sku": {
                "name": "[parameters('appServicePlanSettings').sku]",
                "size": "[parameters('appServicePlanSettings').sku]",
                "tier": "[parameters('appServicePlanSettings').skuTier]"
            },
            "properties": {
                "hyperV": "[parameters('appServicePlanSettings').isHypervContainerPlan]",
                "perSiteScaling": "[parameters('appServicePlanSettings').perSiteScaling]",
                "reserved": "[parameters('appServicePlanSettings').isLinuxOS]"
            },
            "dependsOn": []
        }
    ],
    "outputs": {}
}

主模板:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "appServicePlanSettings": {
            "type": "array"
        },
        "dateTime": {
            "type": "string",
            "defaultValue": "[utcNow()]"
        },
        "resourceNameAndTagSettings": {
            "type": "object"
        },
        "templateSpecSettings": {
            "type": "object"
        }
    },
    "resources": [
        {
            "comments": "Apps - App Service Plans",
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2020-06-01",
            "name": "[concat('Deploy-', parameters('appServicePlanSettings')[copyIndex()].nameAbbr, parameters('resourceNameAndTagSettings').environmentType, parameters('resourceNameAndTagSettings').resourceGroupNumber, parameters('resourceNameAndTagSettings').solutionNameAbbr, parameters('resourceNameAndTagSettings').locationAbbr, '-', parameters('dateTime'))]",
            "copy": {
                "name": "appServicePlanCopy",
                "count": "[length(parameters('appServicePlanSettings'))]"
            },
            "properties": {
                "mode": "Incremental",
                "templateLink": {
                    "id": "[concat('/subscriptions/', parameters('templateSpecSettings').templateSpecSubscriptionId, '/resourceGroups/', parameters('templateSpecSettings').templateSpecResourceGroupName, '/providers/Microsoft.Resources/TemplateSpecs/', parameters('templateSpecSettings').appServicePlan.name, '/versions/', parameters('templateSpecSettings').appServicePlan.version)]"
                },
                "parameters": {
                    "appServicePlanSettings": {
                        "value": "[parameters('appServicePlanSettings')[copyIndex()]]"
                    },
                    "resourceNameAndTagSettings": {
                        "value": "[parameters('resourceNameAndTagSettings')]"
                    }
                }
            },
            "dependsOn": []
        }
    ],
    "outputs": {}
}

参数文件:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "appServicePlanSettings": {
            "value": [
                {
                    "comments": "App Service Plan 1",
                    "deploy": true,
                    "nameAbbr": "Asp",
                    "isLinuxOS": false,
                    "isHypervContainerPlan": false,
                    "perSiteScaling": false,
                    "tagValuePurpose": "Test",
                    "kind": "app",
                    "sku": "P1v2",
                    "skuTier": "PremiumV2"
                }
            ]
        },
        "resourceNameAndTagSettings": {
            "value": {
                "environmentType": "dev",
                "locationAbbr": "europe",
                "resourceGroupNumber": "001",
                "solutionNameAbbr": "test",
                "tagValueCostCenter": "123",
                "tagValueEnvironmentType": "Development",
                "tagValueOwner": "me"
            }
        },
        "templateSpecSettings": {
            "value": {
                "templateSpecResourceGroupName": "XXX",
                "templateSpecSubscriptionId": "XXX",
                "appServicePlan": {
                    "name": "appServicePlanTest",
                    "version": "1.2"
                }
            }
        }
    }
}

这部署得很好。

但如果我遗漏了:

"isHypervContainerPlan": false,

在参数文件中,部署将失败并显示以下消息:

无法处理资源“...”的模板语言表达式,位于“24”行和“9”列。 '语言表达式属性 'isHypervContainerPlan' 不存在,可用属性为 'comments, deploy, nameAbbr, isLinuxOS, perSiteScaling, tagValuePurpose, kind, sku, skuTier'。

如果在 Template Spec parameters 部分设置了 defaultValue,为什么它会因这个错误而失败? 我在这里遗漏了什么或者模板规范不支持默认值?

参数的默认值仅在未提供值时使用。 换句话说,您可以使用参数的默认值或提供一个值,而不是两者,也不是两者的任何组合。

您的 templateSpec 需要一个复杂的 object 作为appServicePlanSettings参数。 object 需要具有被部署的 serverFarm 资源引用的所有属性。 您为该参数提供了一个值,但您也省略了该参数的一个属性,这就是错误消息中标记的属性。

要以另一种方式查看此操作,请将该属性放回您的参数文件并删除一个不同的文件,您会看到类似的错误……或者您可以根本不为 ``appServicePlanSettings` 提供参数值`` 然后将使用默认值。

我一直在使用“默认值”进行测试。 我还测试了这个设置:

模板:

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "appServicePlanSettings": {
            "type": "object",
            "defaultValue": {
                "isHypervContainerPlan": false,
                "isLinuxOS": false,
                "perSiteScaling": false
            }
        },
        "resourceNameAndTagSettings": {
            "type": "object"
        }
    },
    "variables": {
        "appServicePlanName": "[concat('o', parameters('appServicePlanSettings').nameAbbr, parameters('resourceNameAndTagSettings').environmentType, parameters('resourceNameAndTagSettings').resourceGroupNumber, parameters('resourceNameAndTagSettings').solutionNameAbbr, parameters('resourceNameAndTagSettings').locationAbbr)]"
    },
    "resources": [
        {
            "comments": "App Service Plans",
            "condition": "[parameters('appServicePlanSettings').deploy]",
            "apiVersion": "2020-09-01",
            "type": "Microsoft.Web/serverfarms",
            "name": "[variables('appServicePlanName')]",
            "location": "[resourceGroup().location]",
            "tags": {
                "_Purpose": "[parameters('appServicePlanSettings').tagValuePurpose]",
                "CostCenter": "[parameters('resourceNameAndTagSettings').tagValueCostCenter]",
                "EnvironmentType": "[parameters('resourceNameAndTagSettings').tagValueEnvironmentType]",
                "Owner": "[parameters('resourceNameAndTagSettings').tagValueOwner]"
            },
            "kind": "[parameters('appServicePlanSettings').kind]",
            "sku": {
                "name": "[parameters('appServicePlanSettings').sku]",
                "size": "[parameters('appServicePlanSettings').sku]",
                "tier": "[parameters('appServicePlanSettings').skuTier]"
            },
            "properties": {
                "hyperV": "[parameters('appServicePlanSettings').isHypervContainerPlan]",
                "perSiteScaling": "[parameters('appServicePlanSettings').perSiteScaling]",
                "reserved": "[parameters('appServicePlanSettings').isLinuxOS]"
            },
            "dependsOn": []
        }
    ],
    "outputs": {}
}

参数:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "appServicePlanSettings": {
            "value":    {
                    "comments": "App Service Plan 1",
                    "deploy": true,
                    "nameAbbr": "Asp",
                    "isLinuxOS": "",
                    "isHypervContainerPlan": "",
                    "perSiteScaling": "",
                    "tagValuePurpose": "Test",
                    "kind": "app",
                    "sku": "P1v2",
                    "skuTier": "PremiumV2"
                }
        },
        "resourceNameAndTagSettings": {
            "value": {
                "environmentType": "dev",
                "locationAbbr": "europe",
                "resourceGroupNumber": "001",
                "solutionNameAbbr": "test",
                "tagValueCostCenter": "123",
                "tagValueEnvironmentType": "Development",
                "tagValueOwner": "me"
            }
        },
        "templateSpecSettings": {
            "value": {
                "templateSpecResourceGroupName": "oGen1Weu1PrdMng001",
                "templateSpecSubscriptionId": "130176f8-513a-4869-9db3-7c46d0e25159",
                "appServicePlan": {
                    "name": "appServicePlanTest",
                    "version": "1.2"
                }
            }
        }
    }
}

当我没有在参数文件中指定“isHypervContainerPlan”时它也不起作用,我认为我们测试了这个,但显然不够好......

唯一有效的是定义:

"isHypervContainerPlan": "",

在参数文件中,因此未指定值。 这是不可取的,因为即使我不想覆盖默认值,仍然必须定义所有参数。

所以,最后的解决方案变成了下面(主模板不变):

模板规格:

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "appServicePlanSettings": {
            "type": "object"
        },
        "resourceNameAndTagSettings": {
            "type": "object"
        }
    },
    "variables": {
        "appServicePlanName": "[concat('o', parameters('appServicePlanSettings').nameAbbr, parameters('resourceNameAndTagSettings').environmentType, parameters('resourceNameAndTagSettings').resourceGroupNumber, parameters('resourceNameAndTagSettings').solutionNameAbbr, parameters('resourceNameAndTagSettings').locationAbbr)]",
        "appServicePlanSettings": {
            "isHypervContainerPlan": false,
            "isLinuxOS": false,
            "kind": "app",
            "nameAbbr": "Asp",
            "perSiteScaling": false
        }
    },
    "resources": [
        {
            "comments": "App Service Plans",
            "condition": "[parameters('appServicePlanSettings').deploy]",
            "apiVersion": "2020-09-01",
            "type": "Microsoft.Web/serverfarms",
            "name": "[variables('appServicePlanName')]",
            "location": "[resourceGroup().location]",
            "tags": {
                "_Purpose": "[parameters('appServicePlanSettings').tagValuePurpose]",
                "CostCenter": "[parameters('resourceNameAndTagSettings').tagValueCostCenter]",
                "EnvironmentType": "[parameters('resourceNameAndTagSettings').tagValueEnvironmentType]",
                "Owner": "[parameters('resourceNameAndTagSettings').tagValueOwner]"
            },
            "kind": "[if(contains(parameters('appServicePlanSettings'), 'kind'), parameters('appServicePlanSettings').kind, variables('appServicePlanSettings').kind)]",
            "sku": {
                "name": "[parameters('appServicePlanSettings').sku]",
                "size": "[parameters('appServicePlanSettings').sku]",
                "tier": "[parameters('appServicePlanSettings').skuTier]"
            },
            "properties": {
                "hyperV": "[if(contains(parameters('appServicePlanSettings'), 'isHypervContainerPlan'), parameters('appServicePlanSettings').isHypervContainerPlan, variables('appServicePlanSettings').isHypervContainerPlan)]",
                "perSiteScaling": "[if(contains(parameters('appServicePlanSettings'), 'perSiteScaling'), parameters('appServicePlanSettings').perSiteScaling, variables('appServicePlanSettings').perSiteScaling)]",
                "reserved": "[if(contains(parameters('appServicePlanSettings'), 'isLinuxOS'), parameters('appServicePlanSettings').isLinuxOS, variables('appServicePlanSettings').isLinuxOS)]"
            },
            "dependsOn": []
        }
    ],
    "outputs": {}
}

如果我现在不在参数文件中指定这些值,将使用在变量部分配置的值:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "appServicePlanSettings": {
            "value": [
                {
                    "comments": "App Service Plan 1",
                    "deploy": true,
                    "nameAbbr": "Asp",
                    "tagValuePurpose": "Test",
                    "kind": "app",
                    "sku": "P1v2",
                    "skuTier": "PremiumV2"
                }
            ]
        },
        "resourceNameAndTagSettings": {
            "value": {
                "environmentType": "dev",
                "locationAbbr": "europe",
                "resourceGroupNumber": "001",
                "solutionNameAbbr": "test",
                "tagValueCostCenter": "123",
                "tagValueEnvironmentType": "Development",
                "tagValueOwner": "me"
            }
        },
        "templateSpecSettings": {
            "value": {
                "templateSpecResourceGroupName": "XXX",
                "templateSpecSubscriptionId": "XXX",
                "appServicePlan": {
                    "name": "appServicePlanTest",
                    "version": "1.2"
                }
            }
        }
    }
}

暂无
暂无

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

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