简体   繁体   English

在Azure DevOps管道中从PowerShell运行Terraform

[英]Running Terraform from PowerShell in an Azure DevOps Pipeline

I'm trying to run Terraform from a PowerShell step in a YAML-configured Azure DevOps Pipeline, but I can't get it to accept my incantation: instead of initializing the state, it prints the usage instructions and exits. 我正在尝试在YAML配置的Azure DevOps管道中从PowerShell步骤运行Terraform ,但是我无法接受它的咒骂:它没有初始化状态,而是打印使用说明并退出。

I've tried a number of combinations of with/without quotes for the -backend-config parameter, but nothing seems to work. 我已经尝试了-backend-config参数的带/不带引号的多种组合,但似乎没有任何效果。

If I cut-and-paste the script into a ps1 file locally and run it, everything works as expected. 如果我在本地将脚本剪切并粘贴到ps1文件中并运行它,那么一切都会按预期进行。

What is wrong with my incantation of ./terraform.exe init here? 我对./terraform.exe init咒骂是./terraform.exe init了?

Here's the pipeline configuration, in its entirety: 这是整个管道配置:

pool:
  vmImage: 'win1803'

steps:
- powershell: |
    # Download the Terraform executable into the current directory
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

    $url = 'https://releases.hashicorp.com/terraform/0.11.8/terraform_0.11.8_windows_amd64.zip'
    $output = "$PSScriptRoot\terraform.zip"

    Write-Host "Downloading from $url to $output"
    $wc = New-Object System.Net.WebClient
    $wc.DownloadFile($url, $output)

    Expand-Archive $output -DestinationPath . -Force

    Get-ChildItem
  displayName: 'Download Terraform executable'
- powershell: |
    # Create a config file with secrets, and initialize Terraform
    "storage_account_name = `"$env:TerraformStateAccountName`"" | Out-File "./terraform-state.secrets.tfvars" -Append
    "access_key = `"$env:TerraformStateAccountKey`"" | Out-File "./terraform-state.secrets.tfvars" -Append
    "container_name = `"$env:TerraformStateContainer`"" | Out-File "./terraform-state.secrets.tfvars" -Append

    ./terraform.exe init -backend-config=./terraform-state.secrets.tfvars -input=false
  env:
    TerraformStateAccountName: $(Terraform.State.StorageAccountName)
    TerraformStateAccountKey: $(Terraform.State.StorageAccountKey)
    TerraformStateContainer: $(Terraform.State.ContainerName)
  displayName: 'Initialize Terraform'

I would remove the blanks surrounding the = sign. 我将删除=符号周围的空格。

I prefer using environment variables, documented here : 我更喜欢使用这里记录的环境变量:

  1. ARM_SUBSCRIPTION_ID
  2. ARM_CLIENT_ID
  3. ARM_CLIENT_SECRET
  4. ARM_TENANT_ID
  5. ARM_OBJECT_ID
  6. ARM_ACCESS_KEY

You can also initialize Terraform variables using environment variables as documented here . 您还可以使用此处记录的环境变量来初始化Terraform变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Azure DevOps 管道中运行 PowerShell 脚本 - Running a PowerShell Script in Azure DevOps Pipeline Azure DevOps 从管道发送参数到 powershell - Azure DevOps send parameters from pipeline to powershell Azure devOps管道中的Powershell脚本 - Powershell script in azure devOps pipeline 从 Azure DevOps 管道启动 Developer PowerShell for VS 2019 - Start Developer PowerShell for VS 2019 from Azure DevOps pipeline Powershell 从 azure devops build pipeline 获取组件治理结果 - Powershell to fetch the Component Governance result from azure devops build pipeline 在 Azure DevOps 管道中运行 Azure powershell 命令 - Run Azure powershell commands in Azure devops pipeline 在运行 Azure DevOps 管道时,如果我们不给管道的 powershell 脚本中使用的变量任何值 - While running Azure DevOps pipeline, If we dont give any value to the variable which is used in powershell script of pipeline 如何将 Powershell 脚本文件路径从 azure devops repo 传递到 Azure devops 构建管道 - how to pass Powershell script filepath from azure devops repo into Azure devops build pipeline Azure DevOps:如何从 Release Pipeline 中的 PowerShell 脚本构建 Azure Pipeline 检索构建工件? - Azure DevOps: How to retrieve a build artifact from build Azure Pipeline from a PowerShell Script in Release Pipeline? Azure DevOps 中的 PowerShell 任务中的管道变量是否可用? - Are pipeline variables available in PowerShell task in Azure DevOps?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM