【问题标题】:Deploy multiple ARM Templates in parallel with PowerShell使用 PowerShell 并行部署多个 ARM 模板
【发布时间】:2018-08-28 16:06:32
【问题描述】:

我在 Visual Studio 2017 中创建了一个资源管理器项目,结果得到了将模板部署到 Azure 的 Deploy-AzureResourceGroup.ps1。但我有多个模板,希望能够与 PowerShell 并行部署它们。我目前的方法是遍历每个模板并按顺序部署它们,这需要花费大量时间。

我怎样才能做到这一点?

编辑:考虑到来自@4c74356b41 的响应。作为工作的一部分,我需要执行更多逻辑。因此,仅并行执行 ResourceGroupDeployment 是不够的。

【问题讨论】:

  • 如果您有兴趣,Terraform 可以大大简化部署。我们从 PowerShell 迁移到 Terraform 的那一天是美好的一天。

标签: azure powershell deployment azure-resource-manager


【解决方案1】:
  • 首先获取所有相关模板,例如通过

$armTemplateFiles = Get-ChildItem -Path $PSScriptRoot -Include *.JobTemplate.json -Recurse;

  • 现在遍历每个模板文件并为每个模板文件创建一个作业(这些作业随后并行执行)

代码:

foreach ($armTemplateFile in $armTemplateFiles) {
    $logic = {
        Param(
            [object] 
            [Parameter(Mandatory=$true)]
            $ctx,

            [object] 
            [Parameter(Mandatory=$true)]
            $armTemplateFile,

            [string] 
            [Parameter(Mandatory=$true)]
            $resourceGroupName
        )

        function Format-ValidationOutput {
            param ($ValidationOutput, [int] $Depth = 0)
            Set-StrictMode -Off
            return @($ValidationOutput | Where-Object { $_ -ne $null } | ForEach-Object { @('  ' * $Depth + ': ' + $_.Message) + @(Format-ValidationOutput @($_.Details) ($Depth + 1)) })
        }

        # Get related parameters file
        $paramTemplateFile = Get-ChildItem -Path $armTemplateFile.FullName.Replace("JobTemplate.json", "JobTemplate.parameters.json")

        # Test Deployment
        $ErrorMessages = Format-ValidationOutput (Test-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName `
                                                                                        -TemplateFile $armTemplateFile.FullName `
                                                                                        -TemplateParameterFile $paramTemplateFile.FullName `
                                                                                        -DefaultProfile $ctx)
        if ($ErrorMessages) {
            Write-Host '', 'Validation returned the following errors:', @($ErrorMessages), '', 'Template is invalid.'
        }
        else { # Deploy

            New-AzureRmResourceGroupDeployment -Name (($armTemplateFile.Name).Split(".")[0] + ((Get-Date).ToUniversalTime()).ToString('MMddHHmm')) `
                                                -ResourceGroupName $resourceGroupName `
                                                -TemplateFile $armTemplateFile.FullName `
                                                -TemplateParameterFile $paramTemplateFile.FullName `
                                                -Force `
                                                -ErrorVariable ErrorMessages `
                                                -DefaultProfile $ctx
            if ($ErrorMessages) {
                Write-Host '', 'Template deployment returned the following errors:', @(@($ErrorMessages) | ForEach-Object { $_.Exception.Message.TrimEnd("`r`n") })
            }
        }
    }
    Start-Job $logic -ArgumentList (Get-AzureRmContext), $armTemplateFile, $ResourceGroupName
}

While (Get-Job -State "Running")
{
    Start-Sleep 10
    Write-Host "Jobs still running..."
}

Get-Job | Receive-Job

【讨论】:

    【解决方案2】:

    非常糟糕的解决方案,非常复杂。

    New-AzureRmResourceGroupDeployment -ResourceGroup xxx -TemplateFile xxx -AsJob
    

    你还需要一个循环

    【讨论】:

    • 很高兴知道什么时候有更好的方法 :) 我会测试你的建议是否符合我的需要。但我所展示的只是整个故事的一部分。我在代码中有更多与问题无关的逻辑,这些步骤也需要并行化。因此我的方法可能有意义;)
    猜你喜欢
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 2021-08-22
    相关资源
    最近更新 更多