在ARM中获取连接字符串 [英] Get connection strings in ARM

查看:0
本文介绍了在ARM中获取连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Azure资源管理器模板来实例化多个资源。

我希望能够捕获的主连接字符串 Redis、AzureWebJobDashboard、AzureWebJoks Storage和AzureWebJobServiceBus。

推荐答案

根据您的描述,我建议您使用ARM模板函数ListKeys来获取密钥。我们可以使用以下模板代码来设置连接字符串。

以下是我捕获Redis、Storage、Service Bus连接字符串并将其添加到Web应用程序设置的演示。

由于AzureWebJobDashboard,AzureWebJobStorage是存储连接字符串,AzureWebJobServiceBus是服务总线根管理器连接字符串。

因此在我的模板中,我直接根据存储和服务总线名称获取连接字符串。

1.使用模板WebApp创建基本Azure资源组项目

2.从演示中删除不必要的资源。

3.添加连接字符串设置

"resources": [
        {
          "name": "connectionstrings",
          "type": "config",
          "apiVersion": "2015-08-01",
          "dependsOn": [
            "[concat('Microsoft.Web/sites/', variables('webSiteName'))]"
          ],
          "tags": {
            "displayName": "tomConnectionString"
          },
          "properties": {

            "storage": {
              "value": "[concat('DefaultEndpointsProtocol=https;AccountName=',parameters('storageAccountName'),';AccountKey=',concat(listKeys(variables('storageAccountId'),'2015-05-01-preview').key1))]",
              "type": "Custom"
            },

            "Redis": {
              "value": "[listKeys(resourceId('Microsoft.Cache/Redis', variables('RedisName')), '2016-04-01').primaryKey]",
              "type": "Custom"
            },

            "ServiceBus": {
              "value": "[listKeys(resourceId('Microsoft.ServiceBus/namespaces/AuthorizationRules',parameters('serviceBusNamespace'),'RootManageSharedAccessKey'),'2015-08-01').primaryConnectionString]",
              "type": "Custom"
            }

          }
        }
      ]

4.添加相应的参数或变量,如存储信息或服务总线名称。

5.部署模板

结果如下:

完整模板代码:

{
  "$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/"
      }
    },
    "storageAccountName": {
      "type": "string",
      "metadata": {
        "description": "Storage Account to access blob storage."
      }
    },

    "serviceBusNamespace": {
      "type": "string",
      "metadata": {
        "description": "access service bus."
      }
    },
    "skuCapacity": {
      "type": "int",
      "defaultValue": 1,
      "minValue": 1,
      "metadata": {
        "description": "Describes plan's instance count"
      }
    }
  },
  "variables": {
    "webSiteName": "[concat('webSite', uniqueString(resourceGroup().id))]",
    "RedisName": "brando",
    
    "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": "connectionstrings",
          "type": "config",
          "apiVersion": "2015-08-01",
          "dependsOn": [
            "[concat('Microsoft.Web/sites/', variables('webSiteName'))]"
          ],
          "tags": {
            "displayName": "tomConnectionString"
          },
          "properties": {

            "storage": {
              "value": "[concat('DefaultEndpointsProtocol=https;AccountName=',parameters('storageAccountName'),';AccountKey=',concat(listKeys(variables('storageAccountId'),'2015-05-01-preview').key1))]",
              "type": "Custom"
            },

            "Redis": {
              "value": "[listKeys(resourceId('Microsoft.Cache/Redis', variables('RedisName')), '2016-04-01').primaryKey]",
              "type": "Custom"
            },

            "ServiceBus": {
              "value": "[listKeys(resourceId('Microsoft.ServiceBus/namespaces/AuthorizationRules',parameters('serviceBusNamespace'),'RootManageSharedAccessKey'),'2015-08-01').primaryConnectionString]",
              "type": "Custom"
            }

          }
        }
      ]
    }
  ]
}

这篇关于在ARM中获取连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆