skip to Main Content

I want to create a new variable with an existing one inside a json parameter file for bicep

{
    "myVNet_name": "test_vnet",
    "myVNet_name2": "????",
}

I want myVNet_name2 for example equal as $myVNet_name_2 , how to do that ?

2

Answers


  1. Chosen as BEST ANSWER

    find the answer , cannot reuse a variable in json , not a yaml file


  2. This is purely in Bicep.

    var myVNet_name_2 = 'Some Value'
    
    var myObj = { 'myVNet_name ': 'test_vnet',  'myVNet_name2 ': '${myVNet_name_2}'}
    

    If you had a Json config file that Bicep was consuming, you could do something like (Json file):

    { "myVNet_name": "test_vnet",  "myVNet_name2": "{{DynamicValueHere}}"}
    

    I tend to you use myself the {{}} placeholders so that is obvious in the Json what values are dynamic. Naturally give it a meaningful name, rather than DynamicValueHere.

    Within your YAML (assuming you have YAML), you could then call either some inline PowerShell or a ps1 file with code along the following lines:

      $vnetFile = "myArifactsFolder/vnetFile.json"
      $newFile = Get-Content $vnetFile 
      $newFile.replace("{{DynamicValueHere}}", $vnetName) | Out-File -FilePath $vnetFile
    

    After the YAML PowerShell task, you could then call your Bicep task which would consume the file with the values replaced.

    I often do this with environments (Dev, Test, Prod), where the Json file will often have 99% identical values other than say a resource name that is specific to the enviornment e.g. mystorage-uks-dev, mystorage-uks-test etc

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search