How to read azure devops variables in yml (yaml) as object and array data type

1.3k Views Asked by At

I am trying to create Azure resources like key vault using bicep and bicep parameter files. I am using DevOps Pipeline for deployment. I am reading a few parameters from variable/ variable groups in .yml, which is used as param in bip file. However, I want to read array and bicep objects from DevOps variable. Is there any way to achieve this?

Below is code snippet from .bicep file:

@description('Specifies the permissions to secrets in the vault. Valid values are: all, get, list, set, delete, backup, restore, recover, and purge.')
param secretsPermissions array

resource keyvault 'Microsoft.KeyVault/vaults@2022-07-01' = {
  ---extra code---
    accessPolicies: {
        --extra code--
        permissions: {
          secrets: secretsPermissions
        }
      }
}

below is code from .parameters.json file:

"parameters": {
    "secretsPermissions": {
      "value": ["get","list"] //will prefer to get this from DevOps variable group
    }

below is code from yml:

overrideParameters: -<parameter_in_bicep> "$(<variable_in_variable group>)"

i want to read these variables as bicep object and array, currently I am only able to readall values as string only. even if i write them as array or object format.

1

There are 1 best solutions below

0
Bowman Zhu-MSFT On

i want to read these variables as bicep object and array, currently I am only able to readall values as string only. even if i write them as array or object format.

The variables under pipeline concept only have one type - 'string'.

No other types, so you need to design code by yourself.

I write a python script demo in pipeline to help you get the content, add the item, update the content, remove the items of specific route in json file.

trigger:
- none

pool:
  vmImage: ubuntu-latest

steps:
- task: PythonScript@0
  inputs:
    scriptSource: 'inline'
    script: |
      #get json file parameters.json from Json_Files folder
      
      import os
      import json
      
      def add_json_content_of_specific_route(json_file_path, something_to_add, json_route):
          length = len(json_route)
          print(length)
          
          #open json file
          with open(json_file_path, 'r') as f:
              data = json.load(f)
              content = data
              #get the content f data[json_route[0]][json_route[1]]...[json_route[length-1]]
              for i in range(length):
                  if i == 0:
                      content = data[json_route[i]]
                  else:
                      content = content[json_route[i]]
              
              #add something to the content
              content.append(something_to_add)
      
              #write the data to json file
              with open(json_file_path, 'w') as f:
                  json.dump(data, f, indent=4)
      
      
      
              
      def remove_json_content_of_specific_route(json_file_path, something_to_remove, json_route):
          length = len(json_route)
          print(length)
          
          #open json file
          with open(json_file_path, 'r') as f:
              data = json.load(f)
              content = data
              #get the content f data[json_route[0]][json_route[1]]...[json_route[length-1]]
              for i in range(length):
                  if i == 0:
                      content = data[json_route[i]]
                  else:
                      content = content[json_route[i]]
              
              #remove something from the content
              content.remove(something_to_remove)
      
              #write the data to json file
              with open(json_file_path, 'w') as f:
                  json.dump(data, f, indent=4)
      
      def update_json_content_of_specific_route(json_file_path, something_to_update_from, something_to_update_to, json_route):
          length = len(json_route)
          print(length)
          
          #open json file
          with open(json_file_path, 'r') as f:
              data = json.load(f)
              content = data
              #get the content f data[json_route[0]][json_route[1]]...[json_route[length-1]]
              for i in range(length):
                  if i == 0:
                      content = data[json_route[i]]
                  else:
                      content = content[json_route[i]]
              try:
                  #update something from the content
                  content[content.index(something_to_update_from)] = something_to_update_to
              except:
                  print("something_to_update_from is not in the content")
      
              #write the data to json file
              with open(json_file_path, 'w') as f:
                  json.dump(data, f, indent=4)
      
      def get_json_content_of_specific_route_in_array_or_bicep_format(json_file_path, json_route):
          length = len(json_route)
          print(length)
          
          #open json file
          with open(json_file_path, 'r') as f:
              data = json.load(f)
              content = data
              #get the content f data[json_route[0]][json_route[1]]...[json_route[length-1]]
              for i in range(length):
                  if i == 0:
                      content = data[json_route[i]]
                  else:
                      content = content[json_route[i]]
              
              #get the content
              return content
      #==========================================Define the parameters=====================================#
      
      json_file_path = 'Json_Files/parameters.json'
      something_to_add = 'something_to_add'
      something_to_remove = 'something_to_add'
      
      something_to_update_from = 'list'
      
      something_to_update_to = 'something_to_add2'
      
      # route = ['parameters', 'secretsPermissions', 'value']
      route = ['parameters', 'secretsPermissions', 'value']
      
      
      #==========================================Run the function==========================================#
      
      add_json_content_of_specific_route(json_file_path,something_to_add=something_to_add,json_route=route)
      remove_json_content_of_specific_route(json_file_path,something_to_remove=something_to_remove,json_route=route)
      update_json_content_of_specific_route(json_file_path,something_to_update_from=something_to_update_from,something_to_update_to=something_to_update_to,json_route=route)
      print(get_json_content_of_specific_route_in_array_or_bicep_format(json_file_path,json_route=route))

For getting and changing data from YAML file, it is similar, or you can convert the YAML to JSON and then use above code.

This python code can convert yml file to json:

#convert yaml to json

import yaml
import json

with open('YAML_Folder/xxx.yml', 'r') as f:
    data = yaml.load(f, Loader=yaml.FullLoader)
    with open('JSON_Folder/xxx.json', 'w') as f:
        json.dump(data, f, indent=4)

with open('JSON_Folder/xxx.json', 'w') as f:
    json.dump(data, f, indent=4)

Original yml file:

enter image description here

After processed:

enter image description here