Azure Devops - Terraform 任务失败并出现错误:无效的后端配置参数

Clo*_*saz 2 terraform azure-devops azure-pipelines

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

\n
task: TerraformTaskV1@0\ndisplayName: 'Terraform - Init'\ninputs:\nprovider: 'azurerm'\ncommand: 'init'\ncommandOptions: '-input=false'\nbackendServiceArm: 'service-connection'\nbackendAzureRmResourceGroupName: 'Project-RG'\nbackendAzureRmStorageAccountName: 'projectsa'\nbackendAzureRmContainerName: 'tfstate'\nbackendAzureRmKey: 'terraform.tfstate'\nworkingDirectory: terraform\n
Run Code Online (Sandbox Code Playgroud)\n

它尝试执行的命令是

\n
`/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=***\xe2\x80\x99\n
Run Code Online (Sandbox Code Playgroud)\n

错误消息是:

\n
Initializing the backend...  \nError: Invalid backend configuration argument\nThe backend configuration argument "storage_account_name" given on the command line is not expected for the selected backend type.   \nError: Invalid backend configuration argument  \nThe backend configuration argument "container_name" given on the command line is not expected for the selected backend type.   \nError: Invalid backend configuration argument  \nThe backend configuration argument "key" given on the command line is not expected for the selected backend type.\n
Run Code Online (Sandbox Code Playgroud)\n

Clo*_*saz 6

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


小智 5

我遇到了类似的错误,发现在管道 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'
Run Code Online (Sandbox Code Playgroud)