gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:09:09 25 4
gpt4 key购买 nike

所以,我已经有了用于将 VM 部署到 Azure 的 ARM 模板。为了创建唯一但确定性的存储帐户名称,我使用 uniqueString()功能。它看起来像:

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

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

我有什么办法可以做到这一点吗?

最佳答案

阿萨夫,

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

在主 ARM 模板文件的末尾,output你的变量如下:

"outputs": {
"vhdStorageName": {
"type": "string",
"value": "[variables('vhdStorageName')]"
}
}
<小时/>

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

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)

关于json - 我可以在 JSON 部署模板之外调用 ARM 模板函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41064916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com