0
votes

I'm using Linked Templates in Azure ARM templates. I have a problem trying to use the default value of a sub template but still maintaining in the parent template the reference to a parameter.

parent.json

...
parameters: {
  foo: {
    type: "string"
  }
},
resources: [{
  type: "Microsoft.Resources/deployments",
  properties: {
    templateLink: {
      uri: "sub.json",
      contentVersion: "1.0.0.0"
    },
    parameters: {
      bar: {
        value: "[parameters('foo')]"
      }
    }
  }
}]
...

sub.json

...
parameters: {
  bar: {
    type: "string",
    allowedValues: ["larry", "moe", "curly"],
    defaultValue: "curly"
  }
}
...

Unfortunately, passing the null value for foo in the parent template will error The value of deployment parameter 'foo' is null. If I pass an empty string for foo, it will error The provided value '' for the template parameter 'bar' is not valid. The parameter value is not part of the allowed value(s): 'larry,moe,curly'.

I tried putting in parent.json

...
resources: [{
  type: "Microsoft.Resources/deployments"
  properties: {
  ...
    parameters: {
      bar: {
        value: "[if(empty(parameters('foo')), json('null'), parameters('foo'))]"
      }
    }
  }
}]
...

but it'll just give the same value is null. I know this can be done in AWS nested stacks using value aws::NoValue but couldn't find any equivalent in azure. Would anyone know anything else to try?

2

2 Answers

0
votes

Just dont pass anything, dont even specify the parameter name. so pass in:

parameters: {}

I did never try it, but you might be able to achieve the desired outcome with something like:

parameters: "[variables('passMe')]"

and for the variables value you can use something like:

passMe: {
    bar: {
         value: "bla-bla-bla"
    }
}

You will obviously need to construct these variables on the fly or at least partially on the fly.

0
votes

To help anyone else in the future... expanding on @4c74356b41's response. Create variables that can dynamically generate the linked template's parameters at runtime.

Here's a working sample:

...
parameters: {
  foo: {
    type: "string"
  }
},
variables: {
  emptyObject: {},
  subParameterBar: {
    bar: {
      value: "[parameters('foo')]"
    }
  },
  hasSubParameterBar: "[if(empty(parameters('foo')), variables('emptyObject'), variables('subParameterBar'))]",
  subProperties: "unionvariables('HasSubParameterBar'), ...<any other parameter variables>)]"
},
resources: [{
  type: "Microsoft.Resources/deployments",
  name: "sub",
  properties: {
    templateLink: {
      uri: "sub.json",
      contentVersion: "1.0.0.0"
    },
    parameters: "[variables('barProperties')]"
  }
}]
...