16

I understand there is option to have conditional output for property values, but is it possible to have conditional property itself. For example I have template which creates Microsoft.Compute/VirtualMachine and it's the same template for both Windows and Linux. But for windows I need to specify property which do not exist for Linux ("licenseType": "Windows_Server"). Presense of this property will fail deployment with error The property 'LicenseType' cannot be used together with property 'linuxConfiguration'

I'm trying to figure out if it's possible to have this property included only for Windows images while keeping template the same?

Gregory Suvalian
  • 3,566
  • 7
  • 37
  • 66

2 Answers2

17

yes it is possible, but hacky. several options:

  1. create 2 VM resources with different properties, condition them so that only one gets deployed
  2. use union function and variables to construct resulting object
  3. append property as a separate deployment (might not work with all the cases)

let me expand number two a bit:

"variables": {
    "baseObject": {
        "propertyOne": "xxx",
        "propertyTwo": "yyy
    }
    "additionalObject: {
        "optionalProperty": "zzz"
    }
}

and then in your object you can do:

"property": "[if(something, variables('baseObject'), # new line for readability
    union(variables('baseObject'), variables('additionalObject') ))]"
4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • This requires all properties of VirtualMachine to be defined as `variable` to work, right? – Gregory Suvalian Dec 21 '18 at 16:39
  • not necessary, you can apply this at any level, you just cant make half the object defined and half calculated – 4c74356b41 Dec 21 '18 at 16:51
  • `licenseType` property top level property for VirtualMachine, so for this approach to work I need to put the rest of the properties to be calculated too? – Gregory Suvalian Dec 21 '18 at 17:23
  • 2
    if it is directly under properties, yes. one thing you can try is doing: `"licenseType": "[json('null')]"` – 4c74356b41 Dec 21 '18 at 17:49
  • 3
    Take a look at: https://github.com/Azure/azure-quickstart-templates/blob/master/201-vm-custom-script-output/nestedtemplates/vm.json#L166 – bmoore-msft Dec 22 '18 at 17:36
14

Here is what I end up doing based on a previous answer as well as comments

  1. Define variable is you are dealing with Windows

"isWindowsOS": "[equals(parameters('ImageReferenceOffer'), 'WindowsServer')]"

  1. In properties for VM resource use it as below. No need for nested deployments etc. "properties": { "licenseType": "[if(variables('isWindowsOS'), 'Windows_Server', json('null'))]",
Gregory Suvalian
  • 3,566
  • 7
  • 37
  • 66