Terraform 使用本地提供商/插件

问题描述 投票:0回答:4

我在 linux_amd64Oracle Linux Srv 8.4 64 位)上安装了 Terraform v1.0.1。

我正在尝试使用保存在文件夹中的本地提供程序/插件:/root/.terraform.d/plugins

# ll /root/.terraform.d/plugins
drwxr-xr-x. 2 root root       38 Jun 29 15:42 oldversion
-rwxr-xr-x. 1 root root 30068808 Jun 29 15:42 terraform-provider-zabbix
drwxr-xr-x. 2 root root       52 Jun 29 15:42 test_plugging

这是我的 vim /root/.terraformrc:

provider_installation {
  filesystem_mirror {
    path    = "/root/.terraform.d/plugins"
  }
  direct {
    exclude = ["registry.terraform.io/*/*"]
  }
}

这是我的main.tf

terraform {
    required_version    = ">= 0.12.6"
}
provider "zabbix" {
    username            = local.provider_vars.zabbix.username
    password            = local.provider_vars.zabbix.password
    url                 = local.provider_vars.zabbix.endpoint
    tls_insecure        = true
}

但是当我跑步时:

terraform init

正在初始化后端...

正在初始化提供商插件...

  • 正在寻找最新版本的 hashcorp/zabbix...

错误:无法查询可用的提供商包

无法检索提供商的可用版本列表 hashicorp/zabbix:提供商registry.terraform.io/hashicorp/zabbix 是 在任何搜索位置都找不到

  • /root/.terraform.d/plugins

如何解决这个问题? 感谢您的帮助

马可

terraform
4个回答
18
投票

假设你有一个二进制文件

~/.terraform.d/plugins/terraform.local/local/zabbix/1.0.0/linux_amd64/terraform-provider-zabbix_v1.0.0

按如下方式配置 Terraform

terraform {
  required_providers {
    zabbix = {
      source  = "terraform.local/local/zabbix"
      version = "1.0.0"
      # Other parameters...
    }
  }
}

其工作原理如下

terraform init

Initializing the backend...

Initializing provider plugins...
- Finding terraform.local/local/zabbix versions matching "1.0.0"...
- Installing terraform.local/local/zabbix v1.0.0...
- Installed terraform.local/local/zabbix v1.0.0 (unauthenticated)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

10
投票

上面的解决方案绝对正确,但需要澄清编辑.terraformrc

provider_installation {
  filesystem_mirror {
    path    = "/home/user/.terraform.d/plugins"
  }
  direct {
    exclude = ["terraform.local/*/*"]
  }
}

2
投票

这对我有用。我把我的插件放在下面的文件夹下。确保插件之后的第三个文件夹是提供程序的名称。就我而言,卡夫卡连接。 版本和机器架构也很重要。

 ~/.terraform.d/plugins/kyma-project.io/kyma-incubator/kafka-connect/0.3.0/darwin_arm64/terraform-provider-kafka-connect_v0.3.0

并确保二进制文件具有可执行权限

接下来我的提供者看起来像这样。

terraform {
  required_providers {
    kafka-connect = {
      source = "kyma-project.io/kyma-incubator/kafka-connect"
      version = "0.3.0"
    }
  }
}
provider "kafka-connect" {
  url = "http://localhost:8083"
}

0
投票

为此,您可以保留您的

main.tf
文件,就好像没有本地镜像一样。这使您的代码更加灵活,并且还可以在在线环境中运行,您可能不需要在本地提供插件。不过,请对
terraform
块使用正确的语法:

terraform {
    required_providers {
        zabbix = {
            source = "tpretz/zabbix"
            version = ">= 0.12.6"
        }
    }
}
provider "zabbix" {
    username            = local.provider_vars.zabbix.username
    password            = local.provider_vars.zabbix.password
    url                 = local.provider_vars.zabbix.endpoint
    tls_insecure        = true
}

然后您只需将

~/.terraformrc
文件更改为:

provider_installation {
    filesystem_mirror {
        path = "/root/.terraform.d/plugins"
    }
}

还要确保插件目录中有正确的子路径。对于你的情况:

./registry.terraform.io/tpretz/zabbix/0.12.6/...

如果您不再在本地提供插件,只需更改/删除

~/.terraformrc
,而您的 terraform 代码保持不变。

© www.soinside.com 2019 - 2024. All rights reserved.