我可以在JSON部署模板之外调用ARM模板函数吗? [英] Can I call ARM template functions outside the JSON deployment template?

查看:90
本文介绍了我可以在JSON部署模板之外调用ARM模板函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经有了用于将VM部署到Azure的ARM模板.为了创建唯一但确定的存储帐户名,我使用

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))]",
    ...
}

我希望能够在部署模板之外(例如在PowerShell脚本中)创建相同的字符串,或将其用作 VSTS任务.

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.

我有什么办法吗?

推荐答案

Assaf,

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

This is not possible, but assuming you want to use your variable in a subsequent VSTS task, here are the steps to achieve it.

在您的主要ARM模板文件中,最后,

In your main ARM template file, at the end, output your variable like following:

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


完成部署任务后,在 VSTS任务上下文:


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

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

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.

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

Access your variable and use it as a parameter like with any other VSTS variable:

-myparameter $(vhdStorageName)

这篇关于我可以在JSON部署模板之外调用ARM模板函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆