如何列出通过链接的ARM模板部署的存储帐户上的密钥?

时间:2019-06-08 00:56:24

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

下面,我有一个(简化的)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,它也会尝试立即解析此值,而无需等待存储帐户被部署。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

如果

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

不要将其作为值传递(实际上没有任何意义),只需在部署内部使用相同的表达式即可。因为部署将仅在先前的部署完成后才开始,并且存储帐户将已经存在。

此外,我看不到您为什么要使用嵌套模板来执行此操作,我看不到在您的情况下执行此操作的任何原因,您过度使您的部署\代码变得毫无用处(甚至因为那)。只需删除嵌套的部署并使用资源即可。

相关问题