Azure 如何使用ARM';输出';是否重视另一个发布任务?
我有一个ARM模板,其中包含和输出部分,如下所示:Azure 如何使用ARM';输出';是否重视另一个发布任务?,azure,azure-devops,azure-resource-manager,Azure,Azure Devops,Azure Resource Manager,我有一个ARM模板,其中包含和输出部分,如下所示: "outputs": { "sqlServerFqdn": { "type": "string", "value": "[reference(concat('Microsoft.Sql/servers/', variables('sqlserverName'))).fullyQualifiedDomainName]" }, "primaryConnectionString": {
"outputs": {
"sqlServerFqdn": {
"type": "string",
"value": "[reference(concat('Microsoft.Sql/servers/', variables('sqlserverName'))).fullyQualifiedDomainName]"
},
"primaryConnectionString": {
"type": "string",
"value": "[concat('Data Source=tcp:', reference(concat('Microsoft.Sql/servers/', variables('sqlserverName'))).fullyQualifiedDomainName, ',1433;Initial Catalog=', variables('databaseName'), ';User Id=', parameters('administratorLogin'), '@', variables('sqlserverName'), ';Password=', parameters('administratorLoginPassword'), ';')]"
},
"envResourceGroup": {
"type": "string",
"value": "[parameters('hostingPlanName')]"
}
}
我有一个使用该模板的Azure资源组部署任务。然后,我想在下一个配置任务中使用变量$(sqlServerFqdn)。这个变量似乎并不只是填充,我找不到任何地方告诉我如何在发布时使用“outputs”值
运行此ARM模板后,我需要做什么来填充变量以用于配置任务?例如,powershell脚本任务或其他ARM模板的参数。VSTS允许在powershell脚本中设置变量,您可以在其他任务中使用这些变量 语法是
Write Host“##vso[task.setvariable=myvariable;]myvalue”
您可以拥有一个内嵌Powershell脚本,该脚本可以设置在尚未执行的任务中使用的所需变量。您可以像$(myvariable)
一样访问它
您可能需要将system.debug
变量调试为true
才能使用此选项
阅读更多详细信息。您只需为“Azure资源组部署”任务添加一个输出变量名,如下所示: 然后在“目标机器上的PowerShell”任务中使用变量: “目标计算机上的PowerShell”任务将使用“Azure资源组部署”任务中配置的资源: 输出变量: Azure资源组任务的创建/更新操作现在生成 执行期间输出变量。输出变量可用于 请参阅后续任务中的资源组对象。对于 示例“目标计算机上的PowerShell”任务现在可以引用资源 将输出变量分组为“$(variableName)”,以便它可以执行 资源组VM目标上的powershell脚本 限制:执行期间生成的输出变量将具有 有关VM主机名和(公共)端口(如果有)的详细信息。资格证书 要连接到VM主机,请在中明确提供 后续任务
param (
[Parameter(Mandatory=$true)]
[string]
$armOutputString
)
Write-Host $armOutputString
$armOutputObj = $armOutputString | convertfrom-json
Write-Host $armOutputObj
$armOutputObj.PSObject.Properties | ForEach-Object {
$type = ($_.value.type).ToLower()
$key = $_.name
$value = $_.value.value
if ($type -eq "securestring") {
Write-Host "##vso[task.setvariable variable=$key;issecret=true]$value"
Write-Host "Create VSTS variable with key '$key' and value '$value' of type '$type'!"
} elseif ($type -eq "string") {
Write-Host "##vso[task.setvariable variable=$key]$value"
Write-Host "Create VSTS variable with key '$key' and value '$value' of type '$type'!"
} else {
Throw "Type '$type' not supported!"
}
}
有关更多详细信息,请参阅此链接:用于Azure资源组部署的Visual Studio团队服务任务的UI上显示的输出值似乎仅适用于Eddie回答中描述的场景,即VM。事实上,如果您的部署不包括VM,则会出现如下错误: 在资源组“MY-resource-group-NAME”中未找到虚拟机。不能 在输出变量“myVariableName”中注册环境 对于非VM示例,我创建了一个在RG部署之后运行的powershell脚本。例如,此脚本获取资源组的输入变量
$resourceGroupName
,以及所需的输出变量的名称$rgDeploymentOutputParameterName
。您可以自定义并使用类似的内容:
#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'."
}
$outputParameter = $deploymentOutputParameters.Item($rgDeploymentOutputParameterName)
if(!$outputParameter)
{
throw "No output parameter could be found with the name of '$rgDeploymentOutputParameterName'."
}
$outputParameterValue = $outputParameter.Value
# From here, use $outputParameterValue, for example:
Write-Host "##vso[task.setvariable variable=$rgDeploymentOutputParameterName;]$outputParameterValue"
获取这个答案是因为在寻找解决方案时,我总是以这个问题结束 这使得ARM模板输出参数在管道的下游更为可用。但在某些情况下,您没有为订阅购买marketplace项目的权限,因此下面的PowerShell也会这样做。要使用它,请在ARM模板资源组部署步骤之后立即将其添加为powershell脚本步骤。它将查看最后一次部署,并将输出变量拉入管道变量
param(
[string] $resourceGroupName
)
$lastDeployment = Get-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName | Sort Timestamp -Descending | Select -First 1
if(!$lastDeployment) {
throw "Deployment could not be found for Resource Group '$resourceGroupName'."
}
if(!$lastDeployment.Outputs) {
throw "No output parameters could be found for the last deployment of Resource Group '$resourceGroupName'."
}
foreach ($key in $lastDeployment.Outputs.Keys){
$type = $lastDeployment.Outputs.Item($key).Type
$value = $lastDeployment.Outputs.Item($key).Value
if ($type -eq "SecureString") {
Write-Host "##vso[task.setvariable variable=$key;issecret=true]$value"
}
else {
Write-Host "##vso[task.setvariable variable=$key;]$value"
}
}
请注意,环境变量在此脚本的上下文中不可用,但将在后续任务中可用。VSTS Azure资源组部署任务现在(自)起已可用。因此,您可以将Azure资源组部署任务的部署输出中的变量名称设置为,例如,ResourceGroupDeploymentOutputs
,并使用以下内联脚本添加PowerShell脚本任务:
param (
[Parameter(Mandatory=$true)]
[string]
$armOutputString
)
Write-Host $armOutputString
$armOutputObj = $armOutputString | convertfrom-json
Write-Host $armOutputObj
$armOutputObj.PSObject.Properties | ForEach-Object {
$type = ($_.value.type).ToLower()
$key = $_.name
$value = $_.value.value
if ($type -eq "securestring") {
Write-Host "##vso[task.setvariable variable=$key;issecret=true]$value"
Write-Host "Create VSTS variable with key '$key' and value '$value' of type '$type'!"
} elseif ($type -eq "string") {
Write-Host "##vso[task.setvariable variable=$key]$value"
Write-Host "Create VSTS variable with key '$key' and value '$value' of type '$type'!"
} else {
Throw "Type '$type' not supported!"
}
}
在后续任务中,您可以使用模板变量。因此,例如,如果模板中有
sqlServerFqdn
变量,则在PowerShell脚本任务完成后,该变量将作为$(RGDO\u sqlServerFqdn)
可用。首先定义Azure资源部署任务,在此上下文中,部署输出
在下一步中,您将创建一个PowerShell任务,该任务将上面定义的部署输出作为输入参数
PowerShell脚本如下所示,并为ARM模板中定义的每个输出分配一个单独的VSTS环境变量,其名称与ARM模板输出部分中定义的名称相同。这些变量可以在后续任务中使用
param (
[Parameter(Mandatory=$true)]
[string]
$armOutputString
)
Write-Host $armOutputString
$armOutputObj = $armOutputString | convertfrom-json
Write-Host $armOutputObj
$armOutputObj.PSObject.Properties | ForEach-Object {
$type = ($_.value.type).ToLower()
$key = $_.name
$value = $_.value.value
if ($type -eq "securestring") {
Write-Host "##vso[task.setvariable variable=$key;issecret=true]$value"
Write-Host "Create VSTS variable with key '$key' and value '$value' of type '$type'!"
} elseif ($type -eq "string") {
Write-Host "##vso[task.setvariable variable=$key]$value"
Write-Host "Create VSTS variable with key '$key' and value '$value' of type '$type'!"
} else {
Throw "Type '$type' not supported!"
}
}
在后续任务中,您可以通过以下方式访问环境变量:通过'$(varName)
(这也适用于SecureString
)将它们作为参数传递,或通过$env:varName
在PowerShell脚本中(这不适用于SecureString
)
很高兴知道这一点,但是我正在使用提供的“Azure资源组部署”任务,并在ARM模板中进行输出。没有自定义PS。虽然如果无法在本机上完成,我可能需要使用自定义PS。我会尝试system.debug的东西,看看它是否有什么不同。我会尝试一下,但是你看到了吗:幸运的是,我们正在处理VMS:)太棒了!然而,VM名称似乎是我的一个坏例子。我试图做的是使用ARM模板的输出部分中指定的值。我澄清了问题的主体,使之更加具体。嗨,DrydenMaker,我可以知道你是否找到了解决这个问题的方法吗?我想在ARM模板中定义一些自定义输出的时候也遇到了同样的问题。嗯,这个链接现在断了。我想是现在,但是输出变量部分已经被删除了…有人知道为什么吗?看起来这正是我想要做的。我会试试看,然后回来。我正在运行你的脚本,我会得到“get AzureRmResourceGroupDeployment”