terraform init 失败 - git 必须可用并且在 PATH 上

met*_*ale 6 git terraform

我在运行 terraform init / get 时遇到问题。

我得到的错误:

Error downloading modules: Error loading modules: error downloading 'ssh://git@github.com/etc etc': git must be available and on the PATH
Run Code Online (Sandbox Code Playgroud)

github 路径是正确的,在另一台机器上工作正常。

Git 工作正常,它也在 PATH 中。TF_LOG 为空。在 ubuntu 上工作。

谢谢!

小智 7

我不知道你是否解决了你的问题,但我遇到了同样的问题,然后解决了它。分享答案,以防其他人需要帮助。

作为参考,我遇到了这个问题

  • Ubuntu 18.04
  • Linux Ubuntu 4.15.0-45-generic
  • Terraform v0.11.11 Rev 216 通过 Snap 安装

这个错误来自文件terraform/vendor/github.com/hashicorp/go-getter/get_git.goGet功能。该函数尝试的第一件事是以下调用

if _, err := exec.LookPath("git"); err != nil {
   return fmt.Errorf("git must be available and on the PATH")
}
Run Code Online (Sandbox Code Playgroud)

这会导致 go 搜索路径中列出的所有文件夹,以查找具有提供名称的文件,在本例中为git. 我创建并执行了一个执行相同调用的 go 脚本,它的行为符合预期,没有错误地找到 git。

在此之后,我从 snap 卸载了 terraform 并直接从 Hashicorp 网站下载了可执行文件。当我运行该版本的可执行文件时,它仍然v0.11.11运行没有问题。这让我相信该错误与 snap 运行可执行文件的方式或围绕 snap 安装的应用程序的权限模型有关。

TL;DR: 卸载 Terraform 的 snap 安装版本,而是下载并使用 Hashicorp 二进制文件。


R.S*_*R.S 2

根据terraform 文档

对于 github.com 项目,要通过 SSH 克隆,请使用
以下形式:

module "consul" {
  source = "git@github.com:hashicorp/example.git"
}
Run Code Online (Sandbox Code Playgroud)

对于任意 Git 存储库,请使用特殊的 git:: 前缀

module "storage" {
  source = "git::ssh://username@example.com/storage.git"
}
Run Code Online (Sandbox Code Playgroud)

另外,验证~/.ssh/config您的 github 帐户的 Config 是否正确:

希望这可以帮助。