首页 > 解决方案 > 由于 github 身份验证,Ansible 播放失败

问题描述

我正在terragrunt使用 ansibleshell模块通过 ansible play 运行脚本。

Ansible 任务

environment:
        AWS_ACCESS_KEY_ID: "{{aws_access_key_id}}"
        AWS_SECRET_ACCESS_KEY: "{{aws_secret_access_key}}"
        AWS_DEFAULT_REGION: "{{aws_region}}"  
        AWS_SESSION_TOKEN: "{{aws_session_token}}"
  shell: "{{command}}" 

在这里,我给出了command一个论点,它看起来像

terragrunt plan --terragrunt-working-dir /workspace/terraform/vpc

在我的 terragrunt 脚本中,一些 terraform 模块是从私有的 Github 存储库引用的,它应该作为命令执行的一部分下载。

terraform {
    source = "git::https://<accesstoken>@<domanian>/vpc.git?ref=v0.2.0"
  }  

但是当我运行 ansible 角色时,我从 git 收到身份验证错误,但主机中的 repo 克隆工作正常(没有身份验证问题)。

我有来自 ansible play 的 git 身份验证问题,而不是来自主机(现在我的主机是 localhost)

来自 TERRAGRUNT 的错误

error downloading 'https://<token>@<domain>/repo.git?ref=v0.7.0': /usr/local/bin/git exited with 128: Cloning into '/Users/<name>/Projects/terraform/integration/hli/eks/.terragrunt-cache/odeTjZcnAck1LSD4uQ9-Wn_30cQ/xBn6R6SbWXfZ6nabGc-LFpX6RXk'...
fatal: Authentication failed for 'https://<token>@<domain>/repo.git/'

标签: ansibleterraformansible-roleterragrunt

解决方案


连接到私有 Git 存储库时建议使用 SSH。

参考:https ://terragrunt.gruntwork.io/docs/features/keep-your-terraform-code-dry/#using-terragrunt-with-private-git-repos

所以在你的例子中,它会是这样的。//请注意存储库 URL 后的双倍。

terraform {
  source = "git::ssh://git@github.com/foo/modules.git//path/to/module?ref=v0.2.0"
}


推荐阅读