0

I have a variable group that i'm using from a python script. Something like this:

- task: PythonScript@0
  inputs:
    scriptSource: 'inline'
    script: |
      print('variableInVariableGroup: $(variableInVariableGroup)')

I'd like to write my script so that I can iterate over the variable group without explicitly referencing individual variables. Is there a way to feed in the entire variable group to the script as a dictionary or something similar?

Dumbledore__
  • 107
  • 2
  • 8

1 Answers1

1

You could not do that directly, for the workaround is to get the vars via azure cli and set with task variable, then get them in the python script task.

Something like below:

# 'Allow scripts to access the OAuth token' was selected in pipeline.  Add the following YAML to any steps requiring access:
#       env:
#           MY_ACCESS_TOKEN: $(System.AccessToken)
# Variable Group 'vargroup1' was defined in the Variables tab
resources:
  repositories:
  - repository: self
    type: git
    ref: refs/heads/testb2
jobs:
- job: Job_1
  displayName: Agent job 1
  pool:
    vmImage: ubuntu-20.04
  steps:
  - checkout: self
    persistCredentials: True
  - task: PowerShell@2
    name: TestRef
    displayName: PowerShell Script
    inputs:
      targetType: inline
      script: >-
        echo  $(System.AccessToken) | az devops login

        $a=az pipelines variable-group variable list --org 'https://dev.azure.com/orgname/' --project testpro1 --group-id 3 --only-show-errors --output json

        echo "$a"

        echo "##vso[task.setvariable variable=allvars;isOutput=true]$a"
  - task: PythonScript@0
    displayName: Run a Python script
    inputs:
      scriptSource: inline
      script: "b=$(TestRef.allvars)\nprint(b)\n\n       "
...
Joy Wang
  • 39,905
  • 3
  • 30
  • 54