在 Azure 管道中下载 Terraform 模块时出现不一致的失败

Inconsistent failure when downloading terraform modules in azure pipelines

使用简单的 terraform 构建管道,一切似乎都按计划进行,除了我在 terraform init 步骤中途收到“无法下载模块”错误

因此该步骤一开始运行良好,我可以开始初始化和下载模块:

这就是事情变得危险的地方,然后它出错说由于主机密钥验证失败而无法下载模块并且“无法从远程存储库读取”

错误

Error: Failed to download module
│ 
│ Could not download module "***_***_***_***_*******" (aks.tf:2) source code
│ from
│ "git::ssh://git@vs-ssh.visualstudio.com/v3/myOrg/Terraform/repo":
│ error downloading
│ 'ssh://git@vs-ssh.visualstudio.com/v3/***/Terraform/repo':
│ /usr/bin/git exited with 128: Cloning into
│ '.terraform/modules/***_***_***_***_cluster'...
│ Host key verification failed.
│ fatal: Could not read from remote repository.
│ 
│ Please make sure you have the correct access rights
│ and the repository exists.
│ 

我试过的:

Authenticating with Azure Repos git module sources in an Azure Pipelines build

steps:
  - task: DownloadSecureFile@1
    name: sshPrivateKey
    displayName: 'Download SSH Key'
    inputs:
      secureFile: 'pipelinekeys'
 
 
  - task: InstallSSHKey@0
    displayName: 'Install SSH Key'
    inputs:
      knownHostsEntry: '*'
      sshPublicKey: $(sshPublicKey)
      sshKeySecureFile: 'pipelinekeys'

它是如何消耗的

我正在从另一个存储库中的模板调用初始化步骤

steps:
  - task: Bash@3
    displayName: 'Terraform Init'
    env:
      ARM_CLIENT_ID: $(AZURE_CLIENT_ID)
      ARM_CLIENT_SECRET: $(AZURE_CLIENT_SECRET)
      ARM_SUBSCRIPTION_ID: $(AZURE_SUBSCRIPTION_ID)
      ARM_TENANT_ID: $(AZURE_TENANT_ID)
    inputs:
      targetType: 'inline'
      workingDirectory: $(System.DefaultWorkingDirectory)
      script: |
        set -euo pipefail
        
        echo "Initialize"
        terraform init \
            -input=false \
            -backend-config="resource_group_name=${storage_rg}" \
            -backend-config="storage_account_name=${storage_Account}" \
            -backend-config="container_name=${blob_container}" \
            -backend-config="key=${blob_name}" 

知道我遗漏了什么吗?只需要指出正确的方向,甚至不确定问题出在哪里

我明白了。这是一个已知的主机问题。

当我在管道上的 ssh 安装任务中指定“*”时,它不起作用

不得不运行

ssh-keyscan -H -t rsa vs-ssh.visualstudio.com > $env:userprofile/.ssh/known_hosts

然后在我的本地机器上将内容粘贴到我的管道中的一个变量中,并将其传递给 ssh 安装任务

- task: InstallSSHKey@0
  displayName: ‘Install SSH Key’
  Inputs:
    KnownHostEntry: ‘$(put-variable-here)’
    SshPublicKey: ‘$(put-public-key-var-her)’
    SshKeySecureFile: ‘put-private-key-file-here’

https://dev.to/pwd9000/connect-terraform-to-azure-devops-git-repos-over-ssh-163c