-1

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 ?

Wanexa
  • 59
  • 1
  • 6

2 Answers2

0

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

Raymondo
  • 483
  • 2
  • 6
  • 20
  • this is not what I wanna do , I want to reuse a variable in json – Wanexa Feb 22 '23 at 16:59
  • Reading your question, it did read the way I answered it. In future, I would suggest you provide more information and examples so that you are granular in your requirements – Raymondo Feb 23 '23 at 08:58
0

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

Wanexa
  • 59
  • 1
  • 6