首页 > 解决方案 > Terrafrom Init 在提供相对路径时无法定位模块

问题描述

我已经开始使用 terraform 构建基础设施。在该 TF 配置中,我通过使用相对路径调用要使用的模块。这在经典版本中是成功的,但我的任务是将管道转换为 yaml。当我运行 terraform init 步骤时,代理会找到 Tf 配置文件,但即使在之前的任务中下载了工件,也找不到模块文件夹。

yaml 文件:

trigger:
- master

resources:
  pipelines:
  - pipeline: Dashboard-infra
    project: Infrastructure
    source: IT Dashboard
  - pipeline: Infra-modules
    project: Infrastructure
    source: AWS Modules
    trigger: true

stages:
- stage: Test
  displayName: Test
  variables:
  - group: "Non-Prod Keys"
  jobs:
    - deployment:
      displayName: string
      variables:
       region: us-east-1
       app_name: it-dashboard
       environment: test
       tf.path: 'IT Dashboard'
      pool:
        vmImage: 'ubuntu-latest'
      environment: test
      strategy:
        runOnce:
         deploy:
            steps:
            - task: DownloadBuildArtifacts@1
              inputs:
                buildType: 'specific'
                project: '23e9505e-a627-4681-9598-2bd8b6c1204c'
                pipeline: '547'
                buildVersionToDownload: 'latest'
                downloadType: 'single'
                artifactName: 'drop'
                downloadPath: '$(Agent.BuildDirectory)/s'

            - task: DownloadBuildArtifacts@1
              inputs:
                buildType: 'specific'
                project: '23e9505e-a627-4681-9598-2bd8b6c1204c'
                pipeline: '88'
                buildVersionToDownload: 'latest'
                downloadType: 'single'
                artifactName: 'Modules'
                downloadPath: '$(agent.builddirectory)/s'
            - task: ExtractFiles@1
              inputs:
                archiveFilePatterns: 'drop/infrastructure.zip'
                destinationFolder: '$(System.DefaultWorkingDirectory)'
                cleanDestinationFolder: false
                overwriteExistingFiles: false
                
            - task: ExtractFiles@1
              inputs:
                archiveFilePatterns: 'Modules/drop.zip'
                destinationFolder: '$(System.DefaultWorkingDirectory)'
                cleanDestinationFolder: false
                overwriteExistingFiles: false
            - task: TerraformInstaller@0
              inputs:
                terraformVersion: '0.12.3'
            - task: TerraformTaskV2@2
              inputs:
                provider: 'aws'
                command: 'init'
                workingDirectory: '$(System.DefaultWorkingDirectory)/$(tf.path)'
                commandOptions: '-var "region=$(region)" -var "app_name=$(app.name)" -var "environment=$(environment)"'
                backendServiceAWS: 'tf_nonprod'
                backendAWSBucketName: 'wdrx-deployments'
                backendAWSKey: '$(environment)/$(app.name)/infrastructure/$(region).tfstate'

原始错误日志:

2021-10-29T12:30:16.5973748Z ##[section]Starting: TerraformTaskV2
2021-10-29T12:30:16.5981535Z ==============================================================================
2021-10-29T12:30:16.5981842Z Task         : Terraform
2021-10-29T12:30:16.5982217Z Description  : Execute terraform commands to manage resources on AzureRM, Amazon Web Services(AWS) and Google Cloud Platform(GCP)
2021-10-29T12:30:16.5982555Z Version      : 2.188.1
2021-10-29T12:30:16.5982791Z Author       : Microsoft Corporation
2021-10-29T12:30:16.5983122Z Help         : [Learn more about this task](https://aka.ms/AA5j5pf)
2021-10-29T12:30:16.5983461Z ==============================================================================
2021-10-29T12:30:16.7253372Z [command]/opt/hostedtoolcache/terraform/0.12.3/x64/terraform init -var region=*** -var app_name=$(app.name) -var environment=test -backend-config=bucket=wdrx-deployments -backend-config=key=test/$(app.name)/infrastructure/***.tfstate -backend-config=region=*** -backend-config=access_key=*** -backend-config=secret_key=***
2021-10-29T12:30:16.7532941Z [0m[1mInitializing modules...[0m
2021-10-29T12:30:16.7558115Z - S3-env in ../Modules/S3
2021-10-29T12:30:16.7578267Z - S3-env.Global-Vars in ../Modules/Global-Vars
2021-10-29T12:30:16.7585434Z - global-vars in 
2021-10-29T12:30:16.7589958Z [31m
2021-10-29T12:30:16.7597321Z [1m[31mError: [0m[0m[1mUnreadable module directory[0m
2021-10-29T12:30:16.7597847Z 
2021-10-29T12:30:16.7599087Z [0mUnable to evaluate directory symlink: lstat ../Modules/global-vars: no such
2021-10-29T12:30:16.7599550Z file or directory
2021-10-29T12:30:16.7599933Z [0m[0m
2021-10-29T12:30:16.7600324Z [31m
2021-10-29T12:30:16.7600779Z [1m[31mError: [0m[0m[1mFailed to read module directory[0m
2021-10-29T12:30:16.7600986Z 
2021-10-29T12:30:16.7601405Z [0mModule directory  does not exist or cannot be read.
2021-10-29T12:30:16.7601808Z [0m[0m
2021-10-29T12:30:16.7602135Z [31m
2021-10-29T12:30:16.7602573Z [1m[31mError: [0m[0m[1mUnreadable module directory[0m
2021-10-29T12:30:16.7602768Z 
2021-10-29T12:30:16.7603271Z [0mUnable to evaluate directory symlink: lstat ../Modules/global-vars: no such
2021-10-29T12:30:16.7603636Z file or directory
2021-10-29T12:30:16.7603964Z [0m[0m
2021-10-29T12:30:16.7604291Z [31m
2021-10-29T12:30:16.7604749Z [1m[31mError: [0m[0m[1mFailed to read module directory[0m
2021-10-29T12:30:16.7604936Z 
2021-10-29T12:30:16.7605370Z [0mModule directory  does not exist or cannot be read.
2021-10-29T12:30:16.7605770Z [0m[0m
2021-10-29T12:30:16.7743995Z ##[error]Error: The process '/opt/hostedtoolcache/terraform/0.12.3/x64/terraform' failed with exit code 1
2021-10-29T12:30:16.7756780Z ##[section]Finishing: TerraformTaskV2

我什至尝试将模块文件夹移动到 tf.path 中,使其与 tf 配置文件位于同一文件夹中,并将位置从“../”更改为“./”。无论我将模块文件夹提取到哪个存储库(在从另一个构建管道下载为工件后),在 tf 配置文件上调用它时都找不到它。我对 DevOps 还很陌生,如果能得到任何帮助或被指出正确的方向,我将不胜感激。

标签: amazon-web-servicesazure-devopsyamlterraformcicd

解决方案


  1. 在全局级别定义system.debug: true变量以启用调试日志 - 也许那里会给你一个提示:

    variables:
      system.debug: true
    
  2. 除了下载的工件之外,您是否希望从定义管道的存储库中签出文件?默认情况下,该deployment作业不会签出 git 文件,因此您可能需要在checkout: self此处添加步骤。

  3. Unable to evaluate directory symlink: lstat ../Modules/global-vars- 这很可疑,我不希望那里有任何符号链接。但也许错误信息只是误导。

  4. 一个有用的技巧是记录整个目录结构

    您可以使用 bash 脚本步骤执行此操作(可能需要apt install tree先执行此操作):

     - script: tree
    

    或者使用 powershell(将在 MS 托管的 linux 代理上工作):

    - pwsh: Get-ChildItem -Path '$(agent.builddirectory)' -recurse
    

推荐阅读