0

我想在 Azure 资源管理模板中使用 DocumentDB(MongoDB) 连接字符串作为环境变量。例如,我有一个资源组,它有一个 wep 应用程序和一个 DocumentDB(MongoDB) 数据库。

"siteConfig": {
          "appSettings": [
            {
              "name": "db",
              "value": "connection string"
            }
          ]
        }

如何将环境变量分配给模板中的连接字符串?

4

2 回答 2

0

一番挣扎之后:

"appSettings": [{
                    "Name": "DOCUMENTDB_ENDPOINT",
                    "Value": "[reference(concat('Microsoft.DocumentDb/databaseAccounts/', parameters('databaseAccountName'))).documentEndpoint]"
                }, {
                    "Name": "DOCUMENTDB_PRIMARY_KEY",
                    "Value": "[listKeys(resourceId('Microsoft.DocumentDb/databaseAccounts', parameters('databaseAccountName')), '2015-04-08').primaryMasterKey]"
                }]
于 2017-03-30T20:13:57.880 回答
0

ARM模板支持listKeyslist{Value}功能,更多细节我们可以参考ARM模板功能。我们可以找到 DocumentDB list connection strings API,因此我们可以使用 listconnectionstrings 函数来获取 ARM 模板中的 documentdb 连接字符串。

"appSettings": [
            {
              "name": "db",
              "value": " [listConnectionStrings(resourceId('Microsoft.DocumentDb/databaseAccounts', parameters('documentdb')), '2015-04-08').connectionStrings[0].connectionString]"
            }

它在我这边工作正常。如果我们想为 WebApp 添加 appsetting,我们也可以使用以下代码

  "resources": [
      {
          "name": "appsettings",
          "type": "config",
          "apiVersion": "2015-08-01",
          "dependsOn": [
              "[resourceId('Microsoft.Web/sites', variables('webSiteName'))]"
          ],
          "tags": {
              "displayName": "appsetting"
          },
        "properties": {
          "db": "[listConnectionStrings(resourceId('Microsoft.DocumentDb/databaseAccounts', parameters('documentdb')), '2015-04-08').connectionStrings[0].connectionString]"
        }
      }

  ]

在此处输入图像描述

从 Azure 门户检查结果。

在此处输入图像描述

更新:

ARM模板演示代码

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "hostingPlanName": {
      "type": "string",
      "minLength": 1
    },
    "documentdb": {
      "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))]",
    "docDbName": "tomdocumentdb",
    "storageAccountId": "[concat(resourceGroup().id,'/providers/Microsoft.Storage/storageAccounts/', parameters('storageAccountName'))]"
  },
  "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",
      "name": "[variables('webSiteName')]",
      "type": "Microsoft.Web/sites",
      "location": "[resourceGroup().location]",
      "tags": {
        "[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]": "Resource",
        "displayName": "Website"
      },
      "dependsOn": [
        "[concat('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]"
      ],
      "properties": {
        "name": "[variables('webSiteName')]",
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('hostingPlanName'))]"
      },

      "resources": [
          {
              "name": "appsettings",
              "type": "config",
              "apiVersion": "2015-08-01",
              "dependsOn": [
                  "[resourceId('Microsoft.Web/sites', variables('webSiteName'))]"
              ],
              "tags": {
                  "displayName": "appsetting"
              },
            "properties": {
              "db": "[listConnectionStrings(resourceId('Microsoft.DocumentDb/databaseAccounts', parameters('documentdb')), '2015-04-08').connectionStrings[0].connectionString]"
            }
          }

      ]
    }
  ]
}

参数文件

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "hostingPlanName": {
            "value": "tomtest" //your hostingplan name
        },
        "skuName": {
            "value": "B1"
        },
        "documentdb": {
            "value": "tomdocument" // your documentdb name
        }

    }
}
于 2017-03-31T03:13:48.460 回答