在 ARM 模板中获取 App Configuration 的只读连接字符串

Get Read only connection strings of App Configuration in ARM Template

您好,我想在 ARM 模板中添加应用程序配置只读连接字符串

"appSettingsShared": {
            "value": [
              {
                "name": "RedisCache:ConnectionString",
                "value": "[concat(variables('RedisCacheName'),'.redis.cache.windows.net:6380,abortConnect=false,ssl=true,password=', listKeys(resourceId('Microsoft.Cache/Redis', variables('RedisCacheName')), '2015-08-01').primaryKey)]"
              },
               {
                "name": "AppConfig:ConnectionString",
                "value": "???"
              }
            ]

我知道如何使用 Azure CLI 执行此操作:

az appconfig credential list -g $resourceGroup   -n $appConfigName --query "([?name=='Primary Read Only'].connectionString)[0]" --output tsv

非常感谢任何帮助。

您可以使用 listkeys template function to retrieve your configStore keys and connection strings. The implementation is similar to the Configuration Stores - List Keys API,其中 returns 的响应类似于:

{
  "value": [
    {
      "id": "439AD01B4BE67DB1",
      "name": "Primary",
      "value": "000000000000000000000000000000000000000000000000000000",
      "connectionString": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      "lastModified": "2018-04-24T16:30:54+00:00",
      "readOnly": false
    },
    {
      "id": "CB45E100456857B9",
      "name": "Secondary",
      "value": "000000000000000000000000000000000000000000000000000000",
      "connectionString": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      "lastModified": "2018-04-24T16:30:54+00:00",
      "readOnly": false
    },
    {
      "id": "B3AC55B7E71431A9",
      "name": "Primary Read Only",
      "value": "000000000000000000000000000000000000000000000000000000",
      "connectionString": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      "lastModified": "2018-04-24T16:30:54+00:00",
      "readOnly": true
    },
    {
      "id": "E2AF6A9A89DCC177",
      "name": "Secondary Read Only",
      "value": "000000000000000000000000000000000000000000000000000000",
      "connectionString": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      "lastModified": "2018-04-24T16:30:54+00:00",
      "readOnly": true
    }
  ]
}

因为你想访问只读连接字符串,你可以在你的 ARM 模板中访问它,如下所示:

"value": "[listKeys(resourceId('Microsoft.AppConfiguration/configurationStores', variables('configurationStore_name')), '2019-11-01-preview').value[2].connectionString]"

这将为您提供 Primary Read Only 连接字符串。同样,value[3].connectionString 将检索 Secondary Read Only 连接字符串。

在您的模板文件中,您可以通过以下方式获取您配置的 Redis 缓存连接字符串:

"outputs": {
    "RedisCacheConnectionString": {
      "type": "string",
      "value":"[concat(reference(parameters('redisCacheName')).hostName,':', reference(parameters('redisCacheName')).sslPort,',password=',listKeys(resourceId('Microsoft.Cache/redis', parameters('redisCacheName')), '2020-06-01').primaryKey,',ssl=True,abortConnect=False')]" 
    }
  }

这个答案是根据@bhargavi-annadevara的回答得到的启发。