【问题标题】:How to listkeys on a storage account deployed via linked ARM template?如何列出通过链接 ARM 模板部署的存储帐户上的密钥?
【发布时间】:2019-10-23 10:47:33
【问题描述】:

下面我有一个(简化的)Azure ARM 模板来部署一个网站,该网站在其 appSettings 中包含存储帐户。我最初通过一个工作正常的字符串输出参数传递了密钥。

存储模板

"outputs": {
    "storageKey": {
        "type": "string",
        "value": "[listKeys(resourceid(resourceGroup().name, 'Microsoft.Storage/storageAccounts', parameters('storageAccountName')), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value]"
    }
}

根模板

{
    "apiVersion": "[variables('apiVersion')]",
    "type": "Microsoft.Resources/deployments",
    "name": "[concat(resourceGroup().Name, '-', variables('tdfConfiguration')[copyIndex()]['roleName'], '-storage')]",
    "copy": {
        "name": "storageCopy",
        "count": "[length(variables('tdfConfiguration'))]"
    },
    "properties": {
        "mode": "Incremental",
        "templateLink": {
            "uri": "[variables('storageAccountTemplateUri')]",
            "contentVersion": "1.0.0.0"
        },
        "parameters": {
            "storageAccountName": { "value": "[variables('tdfConfiguration')[copyIndex()]['componentName']]" },
            "storageAccountLocation": { "value": "[resourceGroup().location]" },
            "storageAccountType": { "value": "[variables('storageAccountType')]" }
        }
    }
},

{
    "apiVersion": "[variables('apiVersion')]",
    "type": "Microsoft.Resources/deployments",
    "name": "[concat(resourceGroup().Name, '-', variables('tdfConfiguration')[copyIndex()]['roleName'], '-website')]",
    "copy": {
        "name": "webSiteCopy",
        "count": "[length(variables('tdfConfiguration'))]"
    },
    "dependsOn": [
        "[concat('Microsoft.Resources/deployments/', resourceGroup().Name, '-', variables('tdfConfiguration')[copyIndex()]['roleName'], '-serviceplan')]",
        "[concat('Microsoft.Resources/deployments/', resourceGroup().Name, '-', variables('tdfConfiguration')[copyIndex()]['roleName'], '-storage')]"
    ],
    "properties": {
        "mode": "Incremental",
        "templateLink": {
            "uri": "[variables('webSiteTemplateUri')]",
            "contentVersion": "1.0.0.0"
        },
        "parameters": {
            "azureWebJobsStorageAccountKey": { "value": "[reference(concat(resourceGroup().Name, '-', variables('tdfConfiguration')[copyIndex()]['roleName'], '-storage')).outputs.storageKey.value]" }

        }
    }
},

我担心将它作为字符串传递可能会在某些部署日志中暴露它。但是,如果我将其切换为安全字符串输出参数I can no longer access the value。所以看起来我需要在根模板中列出键,但是如果我将网站参数更改为

  "azureWebJobsStorageAccountKey": { "value": "[listKeys(resourceId('Microsoft.Storage/storageAccounts',variables('tdfConfiguration')[copyIndex()]['componentName']), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value]" }

它失败是因为即使存储帐户被列为dependsOn,它也会尝试立即解析此值,而无需等待部署存储帐户。有什么想法吗?

【问题讨论】:

    标签: azure azure-resource-manager arm-template


    【解决方案1】:

    list* 函数将等待资源在同一模板中创建时可用。但是您使用的是嵌套模板,因此它无法知道资源是否已配置,因此它只是假定其已配置(使用 list* 函数时的标准行为)。

    不要将它作为值传递(这真的没有意义),只需在部署中使用相同的表达式即可。因为只有在上一次部署完成并且存储帐户已经存在之后才会开始部署。

    另外,我不明白您为什么要使用嵌套模板执行此操作,在您的情况下我看不出有任何这样做的理由,您将部署\代码过于复杂而没有任何好处(甚至为自己制造了一个问题,因为那)。只需删除嵌套部署并使用资源。

    【讨论】:

    • 聪明的应该可以解决它。我使用嵌套模板是因为我们有很多使用这些通用组件、命名方案和复制逻辑的部署。这是为了在不同的堆栈部署之间尽可能多地共享代码。
    猜你喜欢
    • 2018-02-09
    • 2020-09-28
    • 2021-09-13
    • 2019-04-06
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    相关资源
    最近更新 更多