Home > OS >  Querying an azure service connection for resource names
Querying an azure service connection for resource names

Time:04-20

I have a service connection in ADO to connect to azure to update resources, but I require the names of some of the resources (eg, the storage account name eg: "st-base-storage-dev"

Currently, I'm grabbing this name from the portal and making a variable at the top of the stage, but was wondering if there's a way to get the value from azure without needing this type of manual intervention for future stages?

CodePudding user response:

You can add an Azure CLI task in your pipeline to run the following command to get the specific resources properties and set the value of the name to a variable. Thus you can use the variable in subsequent tasks. See az resource list

az resource list --resource-group ResourceGroup --resource-type Microsoft.Storage/storageAccounts

Below YAML for your reference:

pool:
  vmImage: Windows-latest

steps:
- task: AzureCLI@2
  inputs:
    azureSubscription: 'YourServiceConnection'
    scriptType: 'ps'
    scriptLocation: 'inlineScript'
    inlineScript: |
      $resources = (az resource list --resource-group ResourceGroup  --resource-type Microsoft.Storage/storageAccounts) | ConvertFrom-Json
      $staccount = $resources.name
      Write-Host "staccount:" $staccount
      Write-Host "##vso[task.setvariable variable=storageaccount]$staccount"
  displayName: Get Resource name and set variable

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: 'Write-Host "storageaccount" : $(storageaccount)'
  displayName: Print the variable
  • Related