问题
So, I've got this ARM template for deploying a VM to Azure. In order to create a unique but deterministic storage account name, I use the uniqueString() function. It looks something like:
"variables": {
...
"vhdStorageName": "[concat('vhdstorage', uniqueString(resourceGroup().id))]",
...
}
I want to be able to create that same string outside the deployment template, for example in a PowerShell script, or use it as an input in a VSTS task.
Is there any way for me to do this?
回答1:
Assaf,
This is not possible, but assuming you want to use your variable in a subsequent VSTS task, here are the steps to achieve it.
In your main ARM template file, at the end, output your variable like following:
"outputs": {
"vhdStorageName": {
"type": "string",
"value": "[variables('vhdStorageName')]"
}
}
After your deployment task, set your variable in the VSTS task context by executing this PowerShell script:
param ([string] $resourceGroupName)
#get the most recent deployment for the resource group
$lastRgDeployment = (Get-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName | Sort Timestamp -Descending | Select -First 1)
if(!$lastRgDeployment)
{
throw "Resource Group Deployment could not be found for '$resourceGroupName'."
}
$deploymentOutputParameters = $lastRgDeployment.Outputs
if(!$deploymentOutputParameters)
{
throw "No output parameters could be found for the last deployment of '$resourceGroupName'."
}
$deploymentOutputParameters.Keys | % { Write-Host ("##vso[task.setvariable variable="+$_+";]"+$deploymentOutputParameters[$_].Value) }
For this script, you need to provide the Azure resource group name where the deployment will be made. The script get the last deployment in the resource group, and set each output as a variable in the VSTS task context.
Access your variable and use it as a parameter like with any other VSTS variable:
-myparameter $(vhdStorageName)
来源:https://stackoverflow.com/questions/41064916/can-i-call-arm-template-functions-outside-the-json-deployment-template