'In Azure, can I create a mapping at runtime?

I'm trying to loop over an array of group names and I want to dynamically get the IDs of those groups so that I can assign roles to them.

I just learned I can set a variable at runtime in a pipeline like this:

steps:
  - script: echo "##vso[task.setvariable variable=myVar;]foo"
    displayName: Set variable
  - script: echo "You can use macro syntax for variables: $(myVar)"
    displayName: Echo variable

But to loop over the group names I now want to set a mapping so that I can use that in a subsequent step where I assign the roles. I tried the following:

steps:
  - script: echo "##vso[task.setvariable variable=mymapping;]{a: 1}"
    displayName: Set mapping
  - script: echo $(mymapping)
    displayName: Echo mapping
  - script: echo $(mymapping.a)
    displayName: Echo mapping value

But I get an error saying Mapping values are not allowed in this context.

Is there any other way of creating some sort of mapping/object/dict from within a pipeline?



Solution 1:[1]

Note, that every '-' starts new element in the sequence. Also, indentation of keys in the map should be exactly same.

steps:
    - bash: echo "##vso[task.setvariable variable=mymapping;]{a: 1}"
      name: Set mapping
    - bash: echo $(mymapping)
      name: Echo mapping
    - bash: echo $(mymapping.a)
      name: Echo mapping value

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kangcheng Jin-MSFT