【问题标题】:Azure Devops - Terraform task fails with Error: Invalid backend configuration argumentAzure Devops - Terraform 任务失败并出现错误:后端配置参数无效
【发布时间】:2022-02-02 01:06:41
【问题描述】:

我打算在我的 azure Devops 管道上运行 terraform。我正在使用 MicrosoftDevLabs 市场上的 terraform 扩展版本 0.1.8 我的任务如下所示:

task: TerraformTaskV1@0
displayName: 'Terraform - Init'
inputs:
provider: 'azurerm'
command: 'init'
commandOptions: '-input=false'
backendServiceArm: 'service-connection'
backendAzureRmResourceGroupName: 'Project-RG'
backendAzureRmStorageAccountName: 'projectsa'
backendAzureRmContainerName: 'tfstate'
backendAzureRmKey: 'terraform.tfstate'
workingDirectory: terraform

它试图执行的命令是

`/opt/hostedtoolcache/terraform/0.13.5/x64/terraform init -backend-config=storage_account_name=projectsa -backend-config=container_name=tfstate -backend-config=key=terraform.tfstate -backend-config=resource_group_name=Project-RG -backend-config=arm_subscription_id=xxxx-xxxx-xxxx -backend-config=arm_tenant_id=*** -backend-config=arm_client_id=*** -backend-config=arm_client_secret=***’

错误信息是:

Initializing the backend...  
Error: Invalid backend configuration argument
The backend configuration argument "storage_account_name" given on the command line is not expected for the selected backend type.   
Error: Invalid backend configuration argument  
The backend configuration argument "container_name" given on the command line is not expected for the selected backend type.   
Error: Invalid backend configuration argument  
The backend configuration argument "key" given on the command line is not expected for the selected backend type.

【问题讨论】:

  • 可以粘贴错误信息吗?
  • 您好,错误是这样的:Initializing the backend... Error: Invalid backend configuration argument The backend configuration argument "storage_account_name" given on the command line is not expected for the selected backend type. Error: Invalid backend configuration argument The backend configuration argument "container_name" given on the command line is not expected for the selected backend type. Error: Invalid backend configuration argument The backend configuration argument "key" given on the command line is not expected for the selected backend type.

标签: azure-devops terraform azure-pipelines


【解决方案1】:

修复它。解决方案有点尴尬。 .tf 文件后端被提及为本地。现在这是有道理的,因为本地后端不支持这些参数。将后端更改为 azure 修复了它。确保您定义了正确的后端,因为错误确实表明后端的参数不受支持。

【讨论】:

  • 您好,感谢您的分享,您可以Accept it as an Answer,它可以帮助遇到相同问题的其他社区成员,我们可以存档此线程,谢谢。
  • 谢谢,现在就搞定了。
  • 感谢分享,记录一下,我把后台改成azurerm,已经修复了
  • 谢谢。我犯了同样愚蠢的错误。
【解决方案2】:

我遇到了类似的错误,发现在我的管道 yml 中使用任务 TerraformTaskV2@2 而不是旧的 TerraformTaskV1@0 解决了这个问题。这个较新的任务也适用于最近的 Terraform 版本,例如 1.1.4。

  # Install Terraform on Agent
  - task: TerraformInstaller@0
    displayName: 'install'
    inputs:
      terraformVersion: '1.1.4'

  # Initialize Terraform
  - task: TerraformTaskV2@2
    displayName: 'init'
    inputs:
      provider: 'azurerm'
      command: 'init'
      backendAzureRmResourceGroupName: 'prodbackendstf'
      backendAzureRmStorageAccountName: 'productiontfstate'
      backendAzureRmContainerName: 'tfstate'
      backendAzureRmKey: 'tf.state'
      backendServiceArm: 'IaC SPn'
      workingDirectory: '$(System.DefaultWorkingDirectory)/terraform'

【讨论】:

    【解决方案3】:

    我跑了这个

    - task: TerraformInstaller@0
      inputs:
        terraformVersion: '0.13.5'
    - task: TerraformTaskV1@0
      inputs:
        provider: 'azurerm'
        command: 'init'
        workingDirectory: '$(System.DefaultWorkingDirectory)/stackoverflow/74-terraform'
        backendServiceArm: 'rg-the-code-manual'
        backendAzureRmResourceGroupName: 'TheCodeManual'
        backendAzureRmStorageAccountName: 'thecodemanual'
        backendAzureRmContainerName: 'infra'
        backendAzureRmKey: 'tfstate-so-74'
        commandOptions: '-input=false'
    

    并让它工作

    2020-12-04T10:06:25.4318809Z [command]/opt/hostedtoolcache/terraform/0.13.5/x64/terraform init -backend-config=storage_account_name=thecodemanual -backend-config=container_name=infra -backend-config=key=tfstate-so-74 -backend-config=resource_group_name=TheCodeManual -backend-config=arm_subscription_id=<subscriptionId> -backend-config=arm_tenant_id=*** -backend-config=arm_client_id=*** -backend-config=arm_client_secret=***
    2020-12-04T10:06:25.4670082Z 
    2020-12-04T10:06:25.4675423Z [0m[1mInitializing the backend...[0m
    2020-12-04T10:06:25.4738557Z [0m[32m
    2020-12-04T10:06:25.4740133Z Successfully configured the backend "azurerm"! Terraform will automatically
    2020-12-04T10:06:25.4742265Z use this backend unless the backend configuration changes.[0m
    2020-12-04T10:06:25.9242628Z [33m
    2020-12-04T10:06:25.9244849Z [1m[33mWarning: [0m[0m[1m"arm_client_id": [DEPRECATED] `arm_client_id` has been replaced by `client_id`[0m
    2020-12-04T10:06:25.9246980Z 
    2020-12-04T10:06:25.9248608Z [0m[0m[0m
    2020-12-04T10:06:25.9249659Z [33m
    2020-12-04T10:06:25.9251909Z [1m[33mWarning: [0m[0m[1m"arm_client_secret": [DEPRECATED] `arm_client_secret` has been replaced by `client_secret`[0m
    2020-12-04T10:06:25.9252897Z 
    2020-12-04T10:06:25.9254321Z [0m[0m[0m
    2020-12-04T10:06:25.9255028Z [33m
    2020-12-04T10:06:25.9256913Z [1m[33mWarning: [0m[0m[1m"arm_tenant_id": [DEPRECATED] `arm_tenant_id` has been replaced by `tenant_id`[0m
    2020-12-04T10:06:25.9261480Z 
    2020-12-04T10:06:25.9262574Z [0m[0m[0m
    2020-12-04T10:06:25.9263605Z [33m
    2020-12-04T10:06:25.9264816Z [1m[33mWarning: [0m[0m[1m"arm_subscription_id": [DEPRECATED] `arm_subscription_id` has been replaced by `subscription_id`[0m
    2020-12-04T10:06:25.9265629Z 
    

    包含有关弃用设置的信息,但目前这不会导致失败。为此,github 上已经有 issue 和 PR。

    您在TerraformTaskV1 之前运行TerraformInstaller 吗?

    【讨论】:

    • 已修复。解决方案有点尴尬。 .tf 文件后端被提及为本地。现在这是有道理的,因为本地后端不支持这些参数。
    【解决方案4】:

    我正在使用 terraform v1.1.3 和 TerraformTaskV1@0 azure 管道任务。我遇到了同样的问题,奇怪的是我使用 0.14.X 时使用的同一个 azurerm 块。为了解决这个问题,我编辑了后端块,因此它将使用access_key blob 存储密钥,然后删除 TerraformTaskV1@0,而是使用 cmd 任务初始化 terraform,如下所示:

    terraform {
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "~> 2.92"
        }
      }
    
      backend "azurerm" {
        storage_account_name = "**terraformStorageAccount**"
        container_name       = "**terraformStateFileContainer**"
        key                  = "**terraformStateFile**"
        access_key           = "**storageKey**" #this is sensitive so should be retrieved from safe place via keyvault or dev ops pipeline variables
      }
    
      required_version = ">= 1.1.0"
    }
    

    我使用此任务将管道变量替换为 tf 文件中的变量。顺便说一句,您必须安装此任务:

    - task: replacetokens@3
      displayName: Replace Variable Tokens
      inputs:
        rootDirectory: '$(Pipeline.Workspace)'
        targetFiles: '**/*.tf'
        encoding: 'auto'
        writeBOM: true
        actionOnMissing: 'warn'
        keepToken: false
        tokenPrefix: '**'
        tokenSuffix: '**'
        useLegacyPattern: false
        enableTelemetry: false
    

    一旦变量被替换,使用一个 cmd 任务来 terraform init

    - task: CmdLine@2
      inputs:
        script: 'terraform init'
    

    【讨论】:

      猜你喜欢
      • 2019-10-25
      • 2020-02-23
      • 2019-08-15
      • 1970-01-01
      • 2021-04-04
      • 2020-07-19
      • 1970-01-01
      • 2021-09-12
      • 2014-12-03
      相关资源
      最近更新 更多