Azure - 在设置之前将托管标识访问分配给 VM

Azure - assign managed identity access to VM before setup

我正在 Java 中编写一个程序,它在 Azure 中创建一个 VM 实例,将脚本上传到容器,然后在 VM 中下载并执行脚本。但是,我目前在授予机器访问容器方面遇到困难。当机器已经启动时,我可以手动转到 Azure 并分配一个具有访问权限的角色,但是我想在机器启动之前在我的程序中(创建 VM 时)执行此操作,以便程序可以 [=17= 】 不间断。有没有办法做到这一点?来自 Documentation

Currently, the Azure portal does not support assigning a user-assigned managed identity during the creation of a VM. Instead, refer to one of the following VM creation Quickstart articles to first create a VM, and then proceed to the next section for details on assigning a user-assigned managed identity to the VM

我的理解是否正确,这是不可能的?有解决方法吗?

有可能,文档只是说你不能在门户中这样做,而不是代码中的意思。

对于你的情况,实际上我不确定你想使用 system-assigned 身份还是 user-assigned 身份。

这是一个 sample which creates a Linux VM with system-assigned identity enabled via withSystemAssignedManagedServiceIdentity, if you want to use user-assigned identity, you could change the code to use WithUserAssignedManagedServiceIdentity,您可以指定一个现有的或 not-yet-created 用户指定的身份,这取决于您的要求。

VirtualMachine virtualMachine = azureResourceManager.virtualMachines()
                    .define(linuxVMName)
                        .withRegion(region)
                        .withNewResourceGroup(rgName)
                        .withNewPrimaryNetwork("10.0.0.0/28")
                        .withPrimaryPrivateIPAddressDynamic()
                        .withNewPrimaryPublicIPAddress(pipName)
                        .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
                        .withRootUsername(userName)
                        .withRootPassword(password)
                        .withSize(VirtualMachineSizeTypes.STANDARD_DS2_V2)
                        .withOSDiskCaching(CachingTypes.READ_WRITE)
                        .withSystemAssignedManagedServiceIdentity()
                        .create();