ARM Template.json动态连接字符串 [英] ARM Template.json Dynamic connectionstring

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

问题描述

我从azure中获取了一个部署模板,并将其添加到Visual Studio 2015中的部署项目中.制作并部署资源组后,除网站连接字符串外,其他所有东西都运行良好.

我有所有由此创建的TableStorage,DocumentDb和Redis实例,并且无法弄清楚如何获取这些项目的主连接字符串和主键,因此不必手动输入并添加它们.

查看 ARM模板功能 ListKeys应该可以解决问题,但是在部署之后,该值为空.此外,尝试使用简单的字符串(TestConnectionString)也会添加名称,但不会添加值.

   {
      "type": "Microsoft.Web/sites",
      "kind": "app",
      "name": "[parameters('WebAppName')]",
      "apiVersion": "2015-08-01",
      "properties": {
        "name": "[parameters('WebAppName')]",
        "resources": [],
        "siteConfig": {
          "connectionstrings": [
            {
              "name": "DocumentDbKey",
              "value": "[listKeys(resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('docDbName')), '2015-11-06').primaryMasterKey]",
              "type": "Custom"
            },
            {
              "name": "TestConnectionString",
              "value": "dummystring:pleaseignore;",
              "type": "Custom"
            }
          ]
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('docDbName'))]",
      ]
    }

解决方案

根据您的描述,我们可以使用ARM模板函数ListKeys来获取密钥.我们可以使用以下模板代码来设置连接字符串.我测试了Azure存储连接字符串和Document DB密钥,它对我来说正常工作,请尝试一下.以下是我的详细步骤:

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": {
            "documentDB": {
              "value": "[listKeys(resourceId('Microsoft.DocumentDB/databaseAccounts', variables('docDbName')), '2015-11-06').primaryMasterKey]",
              "type": "Custom"
            },
            "storage": {
              "value": "[concat('DefaultEndpointsProtocol=https;AccountName=',parameters('storageAccountName'),';AccountKey=',concat(listKeys(variables('storageAccountId'),'2015-05-01-preview').key1))]",
              "type": "Custom"
            }
          }
        }
      ]

  1. 添加相应的参数或变量,例如存储信息或docDbName

  1. 部署网站

  2. 从门户检查结果

完整的模板代码:

{
  "$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."
      }
    },
    "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": "connectionstrings",
          "type": "config",
          "apiVersion": "2015-08-01",
          "dependsOn": [
            "[concat('Microsoft.Web/sites/', variables('webSiteName'))]"
          ],
          "tags": {
            "displayName": "tomConnectionString"
          },
          "properties": {
            "documentDB": {
              "value": "[listKeys(resourceId('Microsoft.DocumentDB/databaseAccounts', variables('docDbName')), '2015-11-06').primaryMasterKey]",
              "type": "Custom"
            },
            "storage": {
              "value": "[concat('DefaultEndpointsProtocol=https;AccountName=',parameters('storageAccountName'),';AccountKey=',concat(listKeys(variables('storageAccountId'),'2015-05-01-preview').key1))]",
              "type": "Custom"
            }
          }
        }
      ]
    }
  ]
}

更新:

我们可以从 azure资源中获得有关ARM模板的更多有用信息. >

I have taken a deployment template from azure and added this to a deployment project in Visual Studio 2015. When the Resource Group is made and deployed, everything works well except for the Web Site connectionstrings.

I have TableStorage, DocumentDb, and Redis instances all being created by this and cannot figure out how to get the Primary Connection String and Primary Key of these items so that I don't have to go in by hand and add them.

looking at the ARM Template Functions ListKeys should do the trick, but after deployment the value is empty. Furthermore, trying a simple string (TestConnectionString) also adds the name, but not the value.

   {
      "type": "Microsoft.Web/sites",
      "kind": "app",
      "name": "[parameters('WebAppName')]",
      "apiVersion": "2015-08-01",
      "properties": {
        "name": "[parameters('WebAppName')]",
        "resources": [],
        "siteConfig": {
          "connectionstrings": [
            {
              "name": "DocumentDbKey",
              "value": "[listKeys(resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('docDbName')), '2015-11-06').primaryMasterKey]",
              "type": "Custom"
            },
            {
              "name": "TestConnectionString",
              "value": "dummystring:pleaseignore;",
              "type": "Custom"
            }
          ]
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('docDbName'))]",
      ]
    }

解决方案

As your description we can use ARM Template Functions ListKeys to get the Keys. And we could use the following template code to set the connection string. I test Azure storage connection string and Document DB key, It works correctly for me , please have a try. The following is my detail steps:

1.Create Basic Azure Resource Group project with template WebApp

2.From demo remove the unnecessary resource.

3.Add the connection string setting

"resources": [
        {
          "name": "connectionstrings",
          "type": "config",
          "apiVersion": "2015-08-01",
          "dependsOn": [
            "[concat('Microsoft.Web/sites/', variables('webSiteName'))]"
          ],
          "tags": {
            "displayName": "tomConnectionString"
          },
          "properties": {
            "documentDB": {
              "value": "[listKeys(resourceId('Microsoft.DocumentDB/databaseAccounts', variables('docDbName')), '2015-11-06').primaryMasterKey]",
              "type": "Custom"
            },
            "storage": {
              "value": "[concat('DefaultEndpointsProtocol=https;AccountName=',parameters('storageAccountName'),';AccountKey=',concat(listKeys(variables('storageAccountId'),'2015-05-01-preview').key1))]",
              "type": "Custom"
            }
          }
        }
      ]

  1. Add the corresponding parameters or variables such as storage info or docDbName

  1. Deploy the Website

  2. Check the result from the portal

Full template code:

{
  "$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."
      }
    },
    "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": "connectionstrings",
          "type": "config",
          "apiVersion": "2015-08-01",
          "dependsOn": [
            "[concat('Microsoft.Web/sites/', variables('webSiteName'))]"
          ],
          "tags": {
            "displayName": "tomConnectionString"
          },
          "properties": {
            "documentDB": {
              "value": "[listKeys(resourceId('Microsoft.DocumentDB/databaseAccounts', variables('docDbName')), '2015-11-06').primaryMasterKey]",
              "type": "Custom"
            },
            "storage": {
              "value": "[concat('DefaultEndpointsProtocol=https;AccountName=',parameters('storageAccountName'),';AccountKey=',concat(listKeys(variables('storageAccountId'),'2015-05-01-preview').key1))]",
              "type": "Custom"
            }
          }
        }
      ]
    }
  ]
}

Update:

We could get more useful info about ARM template from the azure resource.

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

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