ARM 模板中特定于部署槽的应用程序设置?

Deployment slot specific appsettin in ARM template?

我正在尝试进入 Visual Studio 资源组模板。 到目前为止它看起来不错,我已经为网络应用程序添加了一些应用程序设置,但我的问题是,我怎样才能使它们特定于部署槽? json 中是否有模板或参数文件的内容?

请尝试添加在 ARM 模板中截取的 json 代码。我已经测试过了。它运行成功。

 "resources": [
        {
          "apiVersion": "2015-08-01",
          "name": "appsettings",
          "type": "config",
          "dependsOn": [
            "[resourceId('Microsoft.Web/Sites/Slots', variables('webSiteName'), 'Staging')]"
          ],
          "properties": {
            "AppSettingKey1": "Some staging value",
            "AppSettingKey2": "My second staging setting",
            "AppSettingKey3": "My third staging setting"
          }
        }
      ]

以下是我的详细步骤:

1.新建一个Azure资源组项目(更多详情请参考document

2。 Demo只是针对Azure Website Slot App的设置配置,所以请从项目中移除其他资源。

3。将 Slot 配置添加到部署文件中

4.发布 Deployment

完整的 json 代码:

  {
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "hostingPlanName": {
      "type": "string",
      "minLength": 1
    },
    "skuName": {
      "type": "string",
      "defaultValue": "S1",
      "allowedValues": [
        "F1",
        "D1",
        "B1",
        "B2",
        "B3",
        "S1",
        "S2",
        "S3",
        "P1",
        "P2",
        "P3",
        "P4"
      ],
      "metadata": {
        "description": "Describes plan's pricing tier and instance size. Check details at https://azure.microsoft.com/en-us/pricing/details/app-service/"
      }
    },
    "skuCapacity": {
      "type": "int",
      "defaultValue": 1,
      "minValue": 1,
      "metadata": {
        "description": "Describes plan's instance count"
      }
    }
  },
  "variables": {
    "webSiteName": "[concat('webSite', uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "apiVersion": "2015-08-01",
      "name": "[parameters('hostingPlanName')]",
      "type": "Microsoft.Web/serverfarms",
      "location": "[resourceGroup().location]",
      "tags": {
        "displayName": "HostingPlan"
      },
      "sku": {
        "name": "[parameters('skuName')]",
        "capacity": "[parameters('skuCapacity')]"
      },
      "properties": {
        "name": "[parameters('hostingPlanName')]"
      }
    },
    {
      "apiVersion": "2015-08-01",
      "dependsOn": [
        "[concat('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]"
      ],
      "location": "[resourceGroup().location]",
      "name": "[variables('webSiteName')]",
      "properties": {
        "name": "[variables('webSiteName')]",
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('hostingPlanName'))]"
      },
      "resources": [
        {
          "apiVersion": "2015-08-01",
          "name": "Staging",
          "type": "slots",
          "location": "[resourceGroup().location]",
          "dependsOn": [
            "[resourceId('Microsoft.Web/Sites', variables('webSiteName'))]"
          ],
          "properties": {
          },
          "resources": [
            {
              "apiVersion": "2015-08-01",
              "name": "appsettings",
              "type": "config",
              "dependsOn": [
                "[resourceId('Microsoft.Web/Sites/Slots', variables('webSiteName'), 'Staging')]"
              ],
              "properties": {
                "AppSettingKey1": "Some staging value",
                "AppSettingKey2": "My second staging setting",
                "AppSettingKey3": "My third staging setting"
              }
            }
          ]
        }

      ],
      "tags": {
        "[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]": "Resource",
        "displayName": "Website"
      },
      "type": "Microsoft.Web/sites"
    }
  ]
}

如果您在 Azure 门户上有任何插槽,我们还可以从 azure resource 获取插槽类型。 我也在 SO.

中找到了类似的 thread

接受的答案是正确的,但遗漏了一条重要信息。在当前的实施中,当交换插槽时,插槽的 AppSettings 配置将与部署一起交换。如果您担心插槽特定的配置,那么这可能不适合您。

要对插槽进行配置 "Sticky",请在您的 ARM 模板中使用以下资源。请注意,slotconfignames 部分已添加到上面 Tom 的回答中的 ARM 模板片段中。

"resources": [
        {
          "apiVersion": "2015-08-01",
          "name": "appsettings",
          "type": "config",
          "dependsOn": [
            "[resourceId('Microsoft.Web/Sites/Slots', variables('webSiteName'), 'Staging')]"
          ],
          "properties": {
            "AppSettingKey1": "Some staging value",
            "AppSettingKey2": "My second staging setting",
            "AppSettingKey3": "My third staging setting"
          },
      {
        "apiVersion": "2015-08-01",
        "name": "slotconfignames",
        "type": "config",
        "dependsOn": [
          "[resourceId('Microsoft.Web/Sites', variables('webSiteName'))]"
        ],
        "properties": {
          "appSettingNames": [ "AppSettingKey1", "AppSettingKey2" ]
        }
      }
    ]

这将使 AppSettingKey1 和 AppSettingKey2 粘在 Staging 插槽中(它们不会随部署交换)。

有关粘性插槽设置以及其他 ARM 模板提示的更多详细信息,请参阅 "Azure Resource Manager Templates Tips and Tricks" from Anthony Chu

"slotconfignames" 只应在 生产插槽 上指定,以告知哪些设置是插槽特定的,即使它们甚至不存在于插槽中。插槽特定设置的实际值仍应在插槽设置中指定。

"resources": [
{
  "apiVersion":"[variables('siteApiVersion')]",
  "name":"[variables('WebAppName')]",
  "type":"Microsoft.Web/sites",
  "kind":"api",
  "location":"[variables('location')]",      
  "tags":{
     "[concat('hidden-related:', resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName')))]":"empty"
  },
  "properties":{
     "name":"[variables('WebAppName')]",
     "serverFarmId":"[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
     "siteConfig":{
        "AlwaysOn":"[parameters('AppServiceAlwaysOn')]"
     }
  },
  "resources":[
     {
        "apiVersion":"[variables('apiVersion')]",
        "type":"config",
        "name":"appsettings",
        "dependsOn":[
           "[variables('WebAppName')]"
        ],
        "properties":{}
     },
      {
        "apiVersion":"[variables('siteApiVersion')]",
        "type": "config",                  
        "name": "slotconfignames",
        "dependsOn": [
          "[concat('Microsoft.Web/sites/', variables('WebAppName'))]"
        ],
        "properties": {
          "appSettingNames": [ "WEBJOBS_DISABLE_SCHEDULE" ]
        }
      },
      {
        "apiVersion":"[variables('siteApiVersion')]",
        "condition":"[parameters('stagingSlotEnabled')]",
        "name":"[parameters('stagingSlotName')]",
        "type":"slots",
        "tags":{
           "displayName":"[concat(variables('WebAppName'), ' ', parameters('stagingSlotName'))]"
        },
        "location":"[variables('location')]",
        "dependsOn":[
           "[resourceId('Microsoft.Web/Sites', variables('WebAppName'))]"
        ],
        "properties":{},
        "resources":[
          {
            "apiVersion":"[variables('apiVersion')]",
            "type":"config",                  
            "name":"appsettings",
            "dependsOn":[
              "[resourceId('Microsoft.Web/Sites/Slots', variables('WebAppName'), parameters('stagingSlotName'))]"
            ],
            "properties":{
              "WEBJOBS_DISABLE_SCHEDULE" : "1"
            }
          }
        ]
     }
  ]
}

]

挠头一千次我找到了解决办法!

  1. 对于生产槽(默认槽),您需要添加如下资源(站点的子资源)

    "resources":[ { "name":"appsettings", "type":"config", "apiVersion":"2018-11-01", "dependsOn":[ "[concat('Microsoft.Web/sites/', parameters('webAppName'))]" ], "tags":{ "displayName":"uisettings" }, "properties":{ "AppSettingKey1":"myvalue", "WEBSITE_LOCAL_CACHE_OPTION":"Always", "WEBSITE_LOCAL_CACHE_SIZEINMB":"2000" } }, { "name":"connectionstrings", "type":"config", "location":"[parameters('location')]", "tags":{ "displayName":"uisettings" }, "apiVersion":"2018-11-01", "dependsOn":[ "[resourceId('Microsoft.Web/sites',parameters('webAppName'))]" ], "properties":{ "ConnString2":{ "value":"[parameters('connectionstring')]", "type":"SQLServer" } } }, { "apiVersion":"2018-11-01", "name":"slotconfignames", "type":"config", "dependsOn":[ "[concat('Microsoft.Web/sites/', parameters('webAppName'))]" ], "properties":{ "appSettingNames":[ "WEBSITE_LOCAL_CACHE_OPTION", "WEBSITE_LOCAL_CACHE_SIZEINMB" ], "connectionStringNames":[ "ConnString2" ] } } ]

  2. 对于您不需要的插槽,“slotconfignames”资源 slotconfignames 仅用于默认(生产插槽) PFA 插槽的工作代码

    "resources":[ { "name":"appsettings", "type":"config", "apiVersion":"2018-11-01", "dependsOn":[ "[resourceId('Microsoft.Web/sites/slots',parameters('webAppName'),'Staging')]" ], "tags":{ "displayName":"uisettings" }, "properties":{ "AppSettingKey1":"myvalue", "WEBSITE_LOCAL_CACHE_OPTION":"Always", "WEBSITE_LOCAL_CACHE_SIZEINMB":"2000" } }, { "name":"connectionstrings", "type":"config", "location":"[parameters('location')]", "tags":{ "displayName":"uisettings" }, "apiVersion":"2018-11-01", "dependsOn":[ "[resourceId('Microsoft.Web/sites/slots',parameters('webAppName'),'Staging')]" ], "properties":{ "ConnString2":{ "value":"[parameters('connectionstring')]", "type":"SQLServer" } } } ],