簡體   English   中英

我可以在 JSON 部署模板之外調用 ARM 模板函數嗎?

[英]Can I call ARM template functions outside the JSON deployment template?

所以,我有這個 ARM 模板,用於將 VM 部署到 Azure。 為了創建唯一但確定性的存儲帳戶名稱,我使用了uniqueString()函數。 它看起來像:

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

我希望能夠在部署模板之外創建相同的字符串,例如在 PowerShell 腳本中,或者將其用作VSTS 任務中的輸入。

我有什么辦法可以做到這一點嗎?

阿薩夫,

這是不可能的,但假設您想在后續 VSTS 任務中使用您的變量,以下是實現它的步驟。

在您的主 ARM 模板文件中,最后, 輸出您的變量,如下所示:

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

在部署任務之后,通過執行以下 PowerShell 腳本在VSTS 任務上下文中設置變量:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM