Do anyone have REST API or ARM template to assign Identity to devtest LAB Configuration and policies.

Vedachalam, Raj 1 Reputation point
2021-05-03T14:13:57.547+00:00

https://learn.microsoft.com/en-us/azure/devtest-labs/use-managed-identities-environments

about link only talk about one Devtest lab. But i need to updated 200+ devtest lab. PLease let me know better way to assign same identity ofr 200+ devtest lab

thanks

Azure DevTest Labs
Azure DevTest Labs
An Azure service that is used for provisioning development and test environments.
263 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Samara Soucy - MSFT 5,051 Reputation points
    2021-05-03T15:33:25.53+00:00

    The REST API, CLI, and ARM templates all apply changes to a single resource at a time. The answer to your issue is to use a loop, which the CLI, ARM, and SDKs are all capable of doing. Is there a way to identify which labs need to be changed through a script? If they are all the labs in the subscription, all in a given resource group or all labeled a certain way this could be automated through one of those methods. If you would be willing to share a bit more about the setup then I'd be happy to help with putting together the script.

    EDIT: Example script for Powershell/CLI
    IMPORTANT NOTE The rest call to update the lab is correct according to the docs, but not working as intended. I've reached out to the product team to see how to resolve it

    #login if not already  
    try {  
        az account show  
    } catch {  
        az login  
    }  
      
    #this script is for a single sub, but could also nest loops from an array of subscription IDs or by 'az account list' to get all subscriptions accessible to the user  
    $subId = "<subId>"  
    $createIdentity = $true  
    $identityGroup = "<resourceGroup"  
    $identityName = "realm-vm-identity-XXXXXXXX"  
      
    $id = [PSCustomObject]@{}  
    if($createIdentity) {  
        $id = az identity create -g $identityGroup -n $identityName | ConvertFrom-Json  
    }  
    else {  
        $id = az identity get -g $identityGroup -n $identityName | ConvertFrom-Json  
    }  
      
    #get the list of labs for this subscription  
    $labs = az rest --method GET --url "/subscriptions/$subId/providers/Microsoft.DevTestLab/labs?api-version=2018-09-15&`$filter=startswith(name, 'bb')" | ConvertFrom-Json  
      
    #update each lab in the subscription  
    $labs.value | ForEach-Object {  
        $body = "{'location': '$($_.location)','identity':{ 'type': 'userAssigned','userAssignedIdentities':{'$($Id.id)':{}}},'properties':{'identityUsageType':'Environment'}}"  
        $updateUri = "https://management.azure.com/subscriptions/$subId/resourceGroups/$($_.resourceGroup)/providers/Microsoft.Devtestlab/labs/$($_.name)/serviceRunners/$($id.name)?api-version=2018-09-15"  
        az rest --method PUT --uri $updateUri --body $body  
    }  
    
    
      
      
      
    
    0 comments No comments

  2. Vedachalam, Raj 1 Reputation point
    2021-05-03T16:02:19.25+00:00

    @Samara Soucy - MSFT

    thanks for your reply, we have 10 subscription and each sub have 30 Lab and lab name start with "dd" name looks like dd-XXX-neu-prod "

    i need to add user identity name like "realm-vm-identity-XXXX-XXX-XXXX" on all devtest lab DevTest Lab Configuration and policies under virtual Machine