Search code examples
foreachazure-devopsyamlhashtableazure-pipelines-tasks

Pass value from hashtable to a Azure DevOps YAML scheduled task


I have a scheduled yaml pipeline. 2 Tasks to execute.1st task is to create a hashtable which is combination of subscription and array of resourcegroups. 2nd Task is to call another template using one subscription and one rg from 1st task. Code is mentioned below.

steps:

- task: AzurePowerShell@5
  displayName: 
  condition: succeeded()
  inputs:
    azureSubscription: '$(AzureSubscription)'
    ScriptType: 'InlineScript'
    Inline: |
      $Subscriptions = Get-AzSubscription
      $AKSSubscriptionAndRG = @{}
 
      foreach ($Subscription in $Subscriptions) {
      Set-AzContext -SubscriptionName $Subscription
      $AKSResourceGroup = (Get-AzResource -ResourceType "Microsoft.ContainerService/managedClusters").ResourceGroupName
      if(![String]::IsNullOrWhiteSpace($AKSResourceGroup)){
      $AKSSubscriptionAndRG.add($Subscription.Name,$AKSResourceGroup)
      }
      }
      Write-Output "Exporting pipeline variable AKSSubscription with value AKSRG"
      Write-Output "##vso[task.setvariable variable=AKSSubscriptionAndRG;]$AKSSubscriptionAndRG"
    azurePowerShellVersion: 'LatestVersion'
    FailOnStandardError: true

- template: another.yaml
  parameters:
    AzureSubscription: '$(AzureSubscription)'
    AksResourceGroupName: '$(AksResourceGroupName)'

My question is : How to pass each subscription and RG to the 2nd template? Can someone help me?


Solution

  • Based on my test, when I pass the Defined variables to AzureSubscription parameters in template, it seems that the parameters could only recognize the text itself. The defined variable couldn't be passed to the parameters.

    Here is the error message:

    enter image description here

    This is a known issue. You have to pass the Azure subscription as a literal.

    For example:

    - template: another.yml
      parameters:
         AzureSubscription: Subscriptionname
         AksResourceGroupName: groupname
    

    As far as I know, there is no workaround for this now.

    Here is a ticket about this limitation in Github.