如何通过ARM模板输出return Redis primaryKey?

How to return Redis primaryKey via ARM template output?

我正在尝试借助下面列出的 ARM 模板部署 Redis - 然后 return 它的主键(Azure 门户中 Redis 的“访问密钥”->“主键”下可用的秘密字符串"):

但是我从管道“AzureResourceManagerTemplateDeployment@3”任务中收到错误消息:

[error]Unable to evaluate template outputs: 'RedisCachePassword'. Please see error details and deployment operations. Please see https://aka.ms/arm-debug for usage details.

[error]Details:

[error]DeploymentOutputEvaluationFailed: The template output 'RedisCachePassword' is not valid: The language expression property 'primaryKey' can't be evaluated..

请问我下面的 ARM 模板有什么问题?在这种情况下如何找到正确的名称?

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "redisCacheName": {
            "defaultValue": "my-redis",
            "type": "String"
        }
    },
    "variables": {
        "resourceName": "[concat(resourceGroup().name, '-', parameters('redisCacheName'))]"
    },
    "outputs": {
      "RedisCacheEndpoint": {
        "type": "string",
        "value": "[concat(reference(variables('resourceName')).hostName, ':', reference(variables('resourceName')).sslPort)]"
      },
      "RedisCachePassword": {
        "type": "string",
        "value": "[reference(variables('resourceName')).accessKeys.primaryKey]"
      }
    },
    "resources": [
        {
            "type": "Microsoft.Cache/Redis",
            "apiVersion": "2019-07-01",
            "name": "[variables('resourceName')]",
            "location": "[resourceGroup().location]",
            "properties": {
                "sku": {
                    "name": "Basic",
                    "family": "C",
                    "capacity": 1
                },
                "enableNonSslPort": false
            }
        }
    ]
}

为什么 [reference(variables('resourceName')).accessKeys.primaryKey] 不起作用?

请注意,这些输出在某些方面非常明显。您最好在 outputs 之外调用 listKeys 命令。您可以在其他模板中使用它,也可以通过 AzureCLI 或 Powershell 单独执行命令。

如果您知道自己在做什么,那么您应该可以这样设置它:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "redisCacheName": {
            "defaultValue": "myredisinstance",
            "type": "String"
        }
    },
    "variables": {
        "resourceId": "[resourceId('Microsoft.Cache/Redis', parameters('redisCacheName'))]",
        "apiVersion": "[providers('Microsoft.Cache', 'redis').apiVersions[0]]"
    },
    "outputs": {
      "RedisCachePassword": {
        "type": "string",
        "value": "[listKeys(variables('resourceId'), variables('apiVersion')).primaryKey]"
      }
    },
    "resources": []
}

Here is some more information 关于它的一般工作原理。

对于"debug"这样的东西,我喜欢用https://resources.azure.com,看看输出和"actions"标签:

Alex 提到了这一点,但我会更强烈地说明...不要在输出中放入秘密。这意味着任何对部署具有读取权限的人都可以访问他们可能无权访问的资源的秘密。 IOW,我什至可能无法访问那个 redisCache,但如果我可以访问部署,我也可以访问那个秘密。

如果需要,输出redisCache的resourceId,在需要消费的地方调用listKeys(Alex也暗示过)

在大多数情况下你甚至不需要使用输出,因为需要密钥的资源也知道资源的 resourceId。

从技术上讲,这是可能的,如果您这样做,请注意表面积...