Recently, my workmate Vic wrote some great posts regarding to Azure Linked Templates. This is, a supplementary post to his ones, to show how to share parameters across the linked templates.
Scripts and templates used in this post can be found at: https://github.com/devkimchi/Linked-ARM-Templates-Sample
parametersLink
and parameters
Properties
We have a master template, master-deployment.json
, and it looks like:
Each nested template has a parameter called environment
that has the same value as the one in the master template. Each template also has a corresponding parameter file. Therefore, as above, we use both parametersLink
and parameters
properties to handle both. However, life is not easy.
Oooops! We can’t use both parametersLink
and parameters
at the same time. We have to use one or the other. This is by design at the time of this writing. Because the environment
parameter is a common denominator across all the nested templates, it is natural to think that the parameter can be passed from master to nested ones.
How can we work this out then? There are several workarounds.
- Add the common parameters to each parameter JSON file and upload it to Azure Storage Account programatically.
- Create a set of parameter files for all possible combinations.
- Use something to refer during the template deployment, whose value will be changing over time during the deployment but the reference point remains the same.
Update Parameters Programatically
The easiest way is the first option. It can be simply achieved by running a separate PowerShell script before the deployment. Let’s have a look.
This is merely to upload nested template files to Azure Storage Account. It excludes parameter files because they need to be updated before being uploaded. Let’s have a look at the following PowerShell script.
The core part of the script is:
- To read parameter file as a JSON object,
- To add the
environment
property to the JSON object, and - To overwrite the parameter file.
Then this updated parameter files are uploaded to Azure Storage Account. Now our Storage Account has got all nested templates and their parameter files. Let’s run the master template again without the parameters
property.
Tada~! It all works fine!
So far, we’ve taken a look how to sort out the restriction that we can’t use both parametersLink
and parameters
properties at the same time. We obviously need a help of another script to run linked templates with their parameters by updating it. It’s definitely not an ideal scenario but certainly it works. If this update is done by hand, it’s tedious, which should be avoided. However, in CI/CD pipelines, it wouldn’t be an issue because we can automate it.
hey mate, did you have issues with your last convertto-json?
I’m having issues with encoding, where single-quotes are getting converted to “\u0027” in the template…not ideal. Cheers.
@wez Thanks for your comment. This is the automatic character escape feature of `ConvertTo-Json` cmdlet. With this, several special characters are impacted, like `\’&` Here’s some more details: http://stackoverflow.com/questions/29306439/powershell-convertto-json-problen-containing-special-characters#29312389