我可以在JSON部署模板之外调用ARM模板函数吗?

时间:2016-12-09 16:33:06

标签: json powershell azure azure-pipelines-release-pipeline

因此,我已经获得了用于将VM部署到Azure的ARM模板。为了创建唯一但确定的存储帐户名称,我使用uniqueString()函数。它看起来像:

"variables": {
    ...
    "vhdStorageName": "[concat('vhdstorage', uniqueString(resourceGroup().id))]",
    ...
}

我希望能够在部署模板之外创建相同的字符串,例如在PowerShell脚本中,或者将其用作VSTS task中的输入。

我有什么方法可以这样做吗?

1 个答案:

答案 0 :(得分:1)

阿萨弗,

这是不可能的,但假设您想在后续VSTS任务中使用您的变量,以下是实现它的步骤。

在您的主要ARM模板文件中,最后output您的变量如下:

"outputs": {
  "vhdStorageName": {
    "type": "string",
    "value": "[variables('vhdStorageName')]"
  }
}

完成部署任务后,通过执行此PowerShell脚本在VSTS task上下文中设置变量:

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) }

对于此脚本,您需要提供将在其中进行部署的Azure资源组名称。该脚本获取资源组中的最后一个部署,并将每个输出设置为VSTS任务上下文中的变量。

访问您的变量并将其用作与任何其他VSTS变量一样的参数:

-myparameter $(vhdStorageName)