ARM - 仅在不存在时部署存储帐户

时间:2017-06-26 20:15:32

标签: azure azure-resource-manager azure-deployment

是否有办法使用Azure资源管理器(ARM)模板仅在存储帐户不存在时才部署存储帐户?

以下模板将创建:

  1. App Insights资源(共享)
  2. 存储帐户(已共享)
  3. 应用计划
  4. 包含App Insights Instrumentation Key和存储帐户连接字符串的配置的应用程序实例。
  5. 我希望前两个步骤是可选的,如果它们已经存在,只需使用它们。

    到目前为止我唯一发现的是newOrExisting的模式,但这没有任何意义。该脚本应该能够判断这些资源是否已经存在并且只是跳过创建。

    其他部署脚本将使用相同的App Insights和存储帐户,因此如果模板可以解决这个问题会很好。

    感谢您的帮助!

    {
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "environmentName": {
            "type": "string",
            "defaultValue": "Dev",
            "allowedValues": [
                "Dev",
                "Test",
                "Prod"
            ]
        }
    },
    "variables": {
        "rgLocation": "[resourceGroup().location]",
        "insightsName": "[concat('Insights-', parameters('environmentName'))]",
        "appName": "[concat('MyAppName-', parameters('environmentName'))]",
        "genStorageName": "[concat('blgenstorage', parameters('environmentName'))]"
    },
    {
            "comments": "Creates a general storage account that is used to save various data, including configurations.",
            "name": "[variables('genStorageName')]",
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2017-06-01",
            "sku": {
                "name": "Standard_LRS"
            },
            "kind": "Storage",
            "location": "[variables('rgLocation')]",
            "tags": {},
            "properties": {}            
        },
        {
            "comments": "Creates the service plan under which the web app will live.",
            "name": "[concat('ServicePlan-MyApp-', parameters('environment'))]",
            "type": "Microsoft.Web/serverfarms",
            "apiVersion": "2016-09-01",
            "kind": "app",
            "location": "[variables('rgLocation')]",
            "tags": {},
            "properties": {
                "name": "[concat('ServicePlan-MyApp-', parameters('environmentName'))]",
                "perSiteScaling": "false",
                "reserved": "false"
            },
            "sku": {
                "name": "S1",
                "tier": "Standard",
                "size": "S1",
                "family": "S",
                "capacity": 1
            }
        },
        {
            "comments": "Primary web app deployment.",
            "name": "[variables('appName')]",
            "type": "Microsoft.Web/sites",
            "apiVersion": "2016-08-01",
            "kind": "app",
            "location": "[variables('rgLocation')]",
            "tags": {},
            "dependsOn": [
                "[resourceId('Microsoft.Web/serverfarms', concat('ServicePlan-MyApp-', variables('envShortName')))]",
                "[resourceId('Microsoft.Storage/storageAccounts', variables('genStorageName'))]",
                "[resourceId('microsoft.insights/components', variables('insightsName'))]"
            ],
            "properties": {
                "enabled": true,
                "hostNameSslStates": [
                    {
                        "name": "[concat(variables('appName'), '.azurewebsites.net')]",
                        "sslState": "Disabled"
                    },
                    {
                        "name": "[concat(variables('appName'), '.scm.azurewebsites.net')]",
                        "sslState": "Disabled"
                    }
                ],
                "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', concat('ServicePlan-MyApp-', parameters('environmentName')))]",
                "siteConfig": {
                    "numberOfWorkers": 1,
                    "defaultDocuments": [
                        "Default.htm",
                        "Default.html",
                        "Default.asp",
                        "index.htm",
                        "index.html",
                        "iisstart.htm",
                        "default.aspx",
                        "index.php",
                        "hostingstart.html"
                    ],
                    "netFrameworkVersion": "v4.6",
                    "appSettings": [
                        {
                            "name": "AppInsightsInstrumentationKey",
                            "value": "[reference(resourceId('Microsoft.Insights/components', variables('insightsName')), '2015-05-01').InstrumentationKey]"
                        }
                    ],
                    "connectionStrings": [
                        {
                            "name": "AzureStorage",
                            "connectionString": "[listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('genStorageName')), '2017-06-01').keys[0].value]",
                            "type": "Custom"
                        }
                    ],
                    "alwaysOn": true,
                    "managedPipelineMode": "Integrated",
                    "virtualApplications": [
                        {
                            "virtualPath": "/",
                            "physicalPath": "site\\wwwroot",
                            "preloadEnabled": false
                        }
                    ],
                    "autoHealEnabled": false,
                    "vnetName": ""
                },
                "microService": "WebSites",
                "clientAffinityEnabled": false,
                "clientCertEnabled": false,
                "hostNamesDisabled": false
            }
    }
    

1 个答案:

答案 0 :(得分:4)

如果存储帐户存在,您将如何创建存储帐户?

基本上,如果ARM模板遇到资源,它尝试部署它将在属性不匹配时更新它。在你的情况下,它不会做任何事情(它会跳过它)。