Terraform 自定义提供程序

Terraform custom provider

我是 Terraform 的新手,我想通过设置我的新实验室来尝试一下。该实验室托管在远程 proxmox 管理程序上,我的地形是 运行 在 docker 容器上。我从 github (https://github.com/andrexus/terraform-provider-proxmox) 下载了 proxmox 提供程序。

如果做对了,我需要使用以下指令编写一个 terraform 配置文件,指定容器内的提供程序位置($HOME/.terraformrc):

providers {
  proxmox = "/path/to/provider/directory"
}

然后我写了一个 main.tf 来验证 terraform 提供程序:

provider "proxmox" {
  host = "https://myproxmux:port"

我试图用 url 和本地路径设置提供程序路径。我还尝试使用 --plugin-dir=path/to/dir 在 terraform init 命令中指定路径,但出现以下错误:

2020/12/02 19:53:00 [INFO] Terraform version: 0.13.5
2020/12/02 19:53:00 [INFO] Go runtime version: go1.14.7
2020/12/02 19:53:00 [INFO] CLI args: []string{"/usr/local/bin/terraform", "init"}
2020/12/02 19:53:00 [DEBUG] Attempting to open CLI config file: /root/.terraformrc
2020/12/02 19:53:00 Loading CLI configuration from /root/.terraformrc
2020/12/02 19:53:00 [DEBUG] checking for credentials in "/root/.terraform.d/plugins"
2020/12/02 19:53:00 [DEBUG] ignoring non-existing provider search directory terraform.d/plugins
2020/12/02 19:53:00 [DEBUG] will search for provider plugins in /root/.terraform.d/plugins
2020/12/02 19:53:00 [TRACE] getproviders.SearchLocalDirectory: /root/.terraform.d/plugins is a symlink to /root/.terraform.d/plugins
2020/12/02 19:53:00 [WARN] local provider path "/root/.terraform.d/plugins/proxomx/proxmox/config.go" contains invalid type "config.go"; ignoring
2020/12/02 19:53:00 [WARN] local provider path "/root/.terraform.d/plugins/proxomx/proxmox/resource_storage.go" contains invalid type "resource_storage.go"; ignoring
2020/12/02 19:53:00 [WARN] local provider path "/root/.terraform.d/plugins/proxomx/proxmox/resource_vm.go" contains invalid type "resource_vm.go"; ignoring
2020/12/02 19:53:00 [DEBUG] ignoring non-existing provider search directory /root/.local/share/terraform/plugins
2020/12/02 19:53:00 [DEBUG] ignoring non-existing provider search directory /usr/local/share/terraform/plugins
2020/12/02 19:53:00 [DEBUG] ignoring non-existing provider search directory /usr/share/terraform/plugins
2020/12/02 19:53:00 [INFO] CLI command args: []string{"init"}

Initializing the backend...

Initializing provider plugins... Finding latest version of hashicorp/proxmox...
2020/12/02 19:53:00 [TRACE] Meta.Backend: no config given or present on disk, so returning nil config
2020/12/02 19:53:00 [TRACE] Meta.Backend: backend has not previously been initialized in this working directory
2020/12/02 19:53:00 [DEBUG] New state was assigned lineage "bbde6bfd-55be-d7fa-f473-6aa043f492ca"
2020/12/02 19:53:00 [TRACE] Meta.Backend: using default local state only (no backend configuration, and no existing initialized backend)
2020/12/02 19:53:00 [TRACE] Meta.Backend: instantiated backend of type
2020/12/02 19:53:00 [DEBUG] checking for provisioner in "."
2020/12/02 19:53:00 [DEBUG] checking for provisioner in "/usr/local/bin"
2020/12/02 19:53:00 [DEBUG] checking for provisioner in "/root/.terraform.d/plugins"
2020/12/02 19:53:00 [INFO] Failed to read plugin lock file .terraform/plugins/linux_amd64/lock.json: open .terraform/plugins/linux_amd64/lock.json: no such file or directory
2020/12/02 19:53:00 [TRACE] Meta.Backend: backend does not support operations, so wrapping it in a local backend
2020/12/02 19:53:00 [TRACE] backend/local: state manager for workspace "default" will:

Error: Failed to install provider

Error while installing hashicorp/proxmox: provider registry registry.terraform.io does not have a provider named registry.terraform.io/hashicorp/proxmox

所以,我可以看到我的 terraformrc 文件是红色的,但我不明白为什么我的提供程序没有被使用以及我做错了什么...

有什么想法吗? :) 预先感谢您的回复!

遗憾的是,此提供程序似乎尚未针对最新版本的 Terraform 进行更新,因此它仅与 Terraform v0.11 或更早版本兼容。

与最新 Terraform 版本(在撰写本文时)兼容的提供程序发布在注册表已知的 Terraform Registry. The particular "proxmox" provider you were aiming to use is not published there, but at the time I'm writing this comment there are three other providers of type proxmox 中,不幸的是,它们中的每一个都与其他版本略有不同。我对这些提供商或 Proxmox 都不熟悉,所以我不能说最好选择哪个。

不过,一旦您确实选择了要使用的模块,好消息是在现代 Terraform 上您无需执行任何手动安装步骤,因为您可以直接在 Terraform 配置中指定这些注册表发布的模块,使用Provider Requirements:

terraform {
  required_providers {
    proxmox = {
      source = "username/proxmox"
    }
  }
}

上面的意思是声明当你在模块的其他地方使用提供者名称 proxmox 时,你希望 Terraform 理解它是指 username/proxmox 提供者。我在这里使用“用户名”作为任何一个可用 Proxmox 提供商的通用占位符;如果您 select 单击注册表详细信息页面右上角的“使用提供程序”按钮,您可以看到用于每个页面的特定源地址。

在您的模块中添加上述声明后,您应该能够 运行 terraform init 在根模块目录中并查看 Terraform 安装 selected 模块注册表。然后,您可以编写一个 provider "proxmox" 块来配置提供者并以通常的方式声明属于它的资源。


(您在问题中显示的 .terraformrc 设置 providers 已过时,已被新的基于注册表的安装机制取代。直接从注册表安装是简单、自动的情况,但如果您有更复杂的要求,您可以在 .terraformrc 中指定一些其他替代方案,记录在 Provider Installation 下。)