azure 此位置不应有模板函数“reference

mrzz3bfm  于 2023-02-09  发布在  其他
关注(0)|答案(2)|浏览(104)

我尝试使用Microsoft.resources/deploymentScripts执行模板中的脚本,但我也尝试在同一模板中声明用户分配的身份

{
    "type": "Microsoft.ManagedIdentity/userAssignedIdentities",
    "name": "scriptIdentity",
    "apiVersion": "2018-11-30",
    "location": "[resourceGroup().location]"
},
{
    "type": "Microsoft.Resources/deploymentScripts",
    "apiVersion": "2019-10-01-preview",
    "name": "updateAppServiceConfigMountPointScript",
    "dependsOn": [
        "[resourceId('Microsoft.Web/sites/config', parameters('appservice_name'), 'web')]",
        "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', 'scriptIdentity')]",
        "[resourceId('Microsoft.Storage/storageAccounts/blobServices', parameters('storageAccounts_name'), 'default')]"
    ],
    "location": "[resourceGroup().location]",
    "kind": "AzurePowerShell",
    "identity": {
        "type": "userAssigned",
        "userAssignedIdentities": {
            "principalId": "[reference(resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', 'scriptIdentity'), '2019-08-01', 'full').identity.principalId]",
            "clientId": "[reference(resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', 'scriptIdentity'), '2019-08-01', 'full').identity.clientId]"
        }
    },
    "properties": {
        "environmentVariables": [
            {
                "name": "account_name",
                "value": "[parameters('storageAccounts_name')]"
            },
            {
                "name": "app_name",
                "value": "[parameters('appservice_name')]"
            },
            {
                "name": "resource_group_name",
                "value": "[resourceGroup().name]"
            }
            
        ],
        "scriptContent": "$access_key = ((az storage account keys list --account-name $account_name) | ConvertFrom-JSON).value[0]; az webapp config storage-account add --name \\\"$app_name\\\" --resource-group \\\"$resource_group_name\\\" --custom-id \\\"frontend\\\" --storage-type \\\"AzureBlob\\\" --account-name \\\"stelckstorageaccount\\\" --share-name \\\"frontend\\\" --mount-path \\\"/home/site/wwwroot/frontend\\\" --access-key \\\"$access_key\\\"",
        "timeout": "PT1M",
        "cleanupPreference": "OnSuccess"
    }

模板在此部分失败:

"userAssignedIdentities": {
    "principalId": "[reference(resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', 'scriptIdentity'), '2019-08-01', 'full').identity.principalId]",
    "clientId": "[reference(resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', 'scriptIdentity'), '2019-08-01', 'full').identity.clientId]"
}

出现以下错误:
部署模板验证失败:'第' 930 '行第' 9 '列的模板资源' updateAppServiceConfigMountPointScript '无效:此位置不应有模板函数"reference"。有关用法详细信息,请参见https://aka.ms/arm-template-expressions。有关用法详细信息,请参见https://aka.ms/arm-template-expressions。"。(代码:无效模板)

2ledvvac

2ledvvac1#

使用身份的resourceId,例如

"userAssignedIdentities": {
          "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', 'scriptIdentity')]": {}
}

有关完整示例,请参见https://github.com/Azure/azure-quickstart-templates/blob/master/201-deployment-script-ssh-key-gen/azuredeploy.json

uujelgoq

uujelgoq2#

我在一个Bicep模板中遇到了同样的问题,我很难找到如何实现bmoore-msft的答案,虽然它不是原始问题的答案,但我会把它贴在这里,以防像我这样的人偶然发现这个页面。
技巧是使用字符串插值来获取冒号左侧的标识id

identity: {
  type: 'UserAssigned'
  userAssignedIdentities: {
    '${scriptIdentity.id}': {}
  }
}

相关问题