簡體   English   中英

創建 VM 時 Azure 分配角色

[英]Azure assign role when creating VM

我正在用 Java 編寫一個程序,該程序在 Azure 中創建一個 VM 實例,將腳本上傳到容器,並在 VM 中下載並執行腳本。 但是,我目前在授予機器訪問容器的權限方面遇到困難。 我加了

.withSystemAssignedManagedServiceIdentity()

到機器創建。 然而,這還不夠,顯然我還必須向 VM 添加角色(在我的情況下為 Storage Reader)。 當我在門戶中手動執行此操作時,在設置機器后,我通過 SSH 看到我具有訪問權限。 但是有沒有辦法在我的 Java 程序中的 VM 創建過程中做到這一點?

有可能,您可以使用withSystemAssignedIdentityBasedAccessTo(String resourceId, BuiltInRole role)方法。

這是示例

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()
                        .withSystemAssignedIdentityBasedAccessTo("<storage-account-resource-id>", BuiltInRole.READER)
                        .create();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM