Azure DevOps自建Linux代理无法运行Terraform问题求助
Azure DevOps自建Linux代理执行Terraform Init失败问题
问题现象
TerraformInstaller@1任务成功安装Terraform(版本1.7.5),但后续TerraformTaskV4@4执行init命令时,报错提示Error: Failed to find terraform tool in paths。该流水线在微软托管代理上可正常运行。
流水线代码
trigger: none pr: '*' pool: name: Self-Hosted-Agent(pool name) demands: agent.name -equals xxx(agent name) stages: - stage: terraform_installation jobs: - job: installation steps: - task: TerraformInstaller@1 displayName: 'Terraform installation' inputs: terraformVersion: 'latest' - stage: terraform_init jobs: - job: init steps: - checkout: self - task: TerraformTaskV4@4 displayName: 'Terraform init' inputs: provider: 'azurerm' command: 'init' workingDirectory: '' environmentServiceNameAzureRM: '' backendServiceArm: '' backendAzureRmResourceGroupName: '' backendAzureRmStorageAccountName: '' backendAzureRmContainerName: '' backendAzureRmKey: '**'
Terraform安装成功日志
Starting: Terraform installation Task : Terraform tool installer Description : Find in cache or download a specific version of Terraform and prepend it to the PATH Version : 1.216.24 Author : Microsoft Corporation Help : Learn more about this task Getting latest Terraform version. Downloading: https://releases.hashicorp.com/terraform/1.7.5/terraform_1.7.5_linux_amd64.zip Extracting archive /usr/bin/unzip /package/_work/_temp/terraform-1.7.5-90331641-692a-439d-9d2d-eadd8ae50420.zip Archive: /package/_work/_temp/terraform-1.7.5-90331641-692a-439d-9d2d-eadd8ae50420.zip inflating: terraform Caching tool: terraform 1.7.5 x64 Prepending PATH environment variable with directory: /package/_work/_tool/terraform/1.7.5/x64 Verifying Terraform installation… /package/_work/_tool/terraform/1.7.5/x64/terraform version Terraform v1.7.5 on linux_amd64 Finishing: Terraform installation
Terraform Init失败日志
Starting: Terraform init Task : Terraform Description : Execute terraform commands to manage resources on AzureRM, Amazon Web Services(AWS) and Google Cloud Platform(GCP) Version : 4.227.24 Author : Microsoft Corporation Help : Learn more about this task ##[error]Error: Failed to find terraform tool in paths Finishing: Terraform init
已尝试操作
- 手动在Linux代理机器上将
/package/_work/_tool/terraform/1.7.5/x64加入系统PATH环境变量 - 放行所有HashiCorp和Terraform相关URL
解决方法
方法1:合并安装与Init步骤到同一Job
Azure DevOps中不同Job的环境变量相互隔离,第一个Job中TerraformInstaller设置的PATH无法传递到第二个Job。将两个步骤合并到同一Job即可解决:
trigger: none pr: '*' pool: name: Self-Hosted-Agent(pool name) demands: agent.name -equals xxx(agent name) stages: - stage: terraform_workflow jobs: - job: terraform_job steps: - checkout: self - task: TerraformInstaller@1 displayName: 'Terraform installation' inputs: terraformVersion: 'latest' - task: TerraformTaskV4@4 displayName: 'Terraform init' inputs: provider: 'azurerm' command: 'init' workingDirectory: '' environmentServiceNameAzureRM: '' backendServiceArm: '' backendAzureRmResourceGroupName: '' backendAzureRmStorageAccountName: '' backendAzureRmContainerName: '' backendAzureRmKey: '**'
方法2:在Init Job中手动注入PATH
若必须分Job执行,可在Init步骤前添加脚本,将Terraform路径注入当前Job的环境变量:
- stage: terraform_init jobs: - job: init steps: - checkout: self - script: | echo "##vso[task.setvariable variable=PATH]/package/_work/_tool/terraform/1.7.5/x64:$PATH" displayName: 'Add Terraform to PATH' - task: TerraformTaskV4@4 displayName: 'Terraform init' inputs: provider: 'azurerm' command: 'init' # 其余参数保持不变
方法3:在代理机器全局安装Terraform
跳过TerraformInstaller任务,直接在Linux代理机器上全局安装指定版本的Terraform,并确保其路径加入系统默认PATH,所有Job即可直接调用。
内容的提问来源于stack exchange,提问作者SAUPARNA DATTA
相关产品推荐
相关产品推荐

