如何在 Azure Batch 帐户中指定任务的用户身份 "Pool default user (Admin)"?

How to specify the task's user identity "Pool default user (Admin)" in the azure batch account?

我正在尝试从 Powershell 脚本创建多个批处理任务并寻找如何指定用户身份。 Dotnet 核心 API 支持此指定,但不支持 Azure-CLI。

当我 运行 下面的脚本时,我的 PowerShell 脚本出现错误,提示没有足够的权限执行此操作:

az batch account login -g $resourceGroup -n $batchAccountName 
az batch task create --job-id $BatchJobId `
    --task-id "DeployIdHere" `
    --command-line $cmdCommand `
    --account-name $batchAccountName `
    --debug `
    --verbose `
    --output table

我以为会找到类似 --elevation-level Admin 的东西,但似乎我不明白它应该是什么样子的。

您可以使用 --json-file 标志创建任务,并在命令 az batch task create --job-id myjobtest --json-file .\task.json 中指定用户身份。

格式见https://github.com/Azure/azure-docs-cli-python-samples/blob/master/batch/run-job/tasks.json

您还可以使用管理员添加多个任务。任务 JSON 文件将是这样的:

{
    "id": "mytasktest123",
    "commandLine": "/bin/bash -c \"sudo ls\"",
    "waitForSuccess": true,
    "userIdentity": {
        "autoUser": {
          "elevationLevel": "admin",
          "scope": "pool"
        },
        "userName": null
      }
    }

结果

如果您需要进一步的帮助,请告诉我。

which says that there are not enough permissions to perform this action

当您使用az batch account login -g $resourceGroup -n $batchAccountName登录时,它使用Auzre AD身份验证,它使用您使用az login登录的帐户的凭据。

如果您使用带有 --shared-key-auth 参数的命令,它会使用 共享密钥身份验证 ,但您的帐户还需要执行 Microsoft.Batch/batchAccounts/listKeys/action 操作的权限.起初我以为Reader角色就够了,但根据我的测试,它没有列出批账户密钥的权限。

因此,要解决此问题,您可以让 subscription/batch accountOwner 提供您使用 az login 登录的用户帐户作为 OwnerContributor 角色。导航到门户中的 subscription/batch 帐户 -> Access control (IAM) -> Add -> Add role assignment -> 搜索您的用户帐户(或服务主体),将其添加为 Owner/Contributor作用,详情here. Then use az account clear清除本地缓存,再运行az batch account login -g $resourceGroup -n $batchAccountNameaz batch task create就可以了。

How to specify the task's user identity “Pool default user (Admin)” in the azure batch account?

方法是使用--json-file参数,根据您的需要,在.json文件中指定elevationLevelnonadminadmin,如下面。

batch.json 文件:

{
  "id": "task1",
  "commandLine": "bash -c 'echo hello'",
  "userIdentity": {
    "autoUser": {
      "scope": "task",
      "elevationLevel": "admin"
    }
  }
}

样本:

az batch task create --job-id myjob --json-file C:\Users\joyw\Desktop\batch.json