0

当我执行时,terraform init我有以下输出:

Initializing modules...
- compute in modules\compute
- private_network in modules\private_network
- public_ip in modules\public_ip
- route in modules\route

Initializing the backend...

Initializing provider plugins...
- Finding terraform-provider-openstack/openstack versions matching "~> 1.35.0"...
- Finding latest version of hashicorp/openstack...
- Installing terraform-provider-openstack/openstack v1.35.0...
- Installed terraform-provider-openstack/openstack v1.35.0 (self-signed, key ID 4F80527A391BEFD2)

Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/plugins/signing.html

Error: Failed to query available provider packages

Could not retrieve the list of available versions for provider
hashicorp/openstack: provider registry registry.terraform.io does not have a
provider named registry.terraform.io/hashicorp/openstack

这是我的 provider.tf 文件(灵感来自于 Terraform 的官方文档):

# Define required providers
terraform {
required_version = ">= 0.14.0"
  required_providers {
    openstack = {
      source = "terraform-provider-openstack/openstack"
      version = "~> 1.35.0"
    }
  }
}

# Configure the OpenStack Provider
provider "openstack" {
  user_name   = "username"
  tenant_name = "tenantname"
  password    = "mypasswd"
  auth_url    = "http://my.openstack.lan:5000"
  region      = "RegionOne"
}

# Create a web server
#resource "openstack_compute_instance_v2" "test-server" {
  # ...
#}

当我检查版本时,我有这个:

Terraform v0.14.2

Your version of Terraform is out of date! The latest version
is 1.0.3. You can update by downloading from https://www.terraform.io/downloads.html

这是我跑步的时候terraform apply


Error: Could not load plugin


Plugin reinitialization required. Please run "terraform init".

Plugins are external binaries that Terraform uses to access and manipulate
don't satisfy the version constraints, or are otherwise incompatible.

Terraform automatically discovers provider requirements from your
configuration, including providers used in child modules. To see the
requirements and constraints, run "terraform providers".

2 problems:

- Failed to instantiate provider "registry.terraform.io/hashicorp/openstack"
to obtain schema: unknown provider "registry.terraform.io/hashicorp/openstack"
- Failed to instantiate provider
"registry.terraform.io/terraform-provider-openstack/openstack" to obtain
schema: unknown provider

最后terraform providers

Providers required by configuration:
.
├── provider[registry.terraform.io/terraform-provider-openstack/openstack] ~> 1.35.0
├── module.compute
│   └── provider[registry.terraform.io/hashicorp/openstack]
├── module.private_network
│   └── provider[registry.terraform.io/hashicorp/openstack]
├── module.public_ip
│   └── provider[registry.terraform.io/hashicorp/openstack]
└── module.route
    └── provider[registry.terraform.io/hashicorp/openstack]
4

2 回答 2

1

要手动管理提供程序版本,请使用 terraform 镜像。

基本上:创建提供程序路径,下载 && 安装提供程序,设置 terraform 镜像路径。

# mkdir -p /root/.local/share/terraform/plugins/registry.terraform.io/terraform-provider-openstack/openstack/1.40.0/linux_amd64/
# curl -L https://releases.hashicorp.com/terraform-provider-openstack/1.40.0/terraform-provider-openstack_1.40.0_linux_amd64.zip -o terraform-provider-openstack.zip
# unzip terraform-provider-openstack.zip
# mv terraform-provider-openstack_v1.40.0 /root/.local/share/terraform/plugins/registry.terraform.io/terraform-provider-openstack/openstack/1.40.0/linux_amd64/
# terraform providers mirror /root/.local/share/terraform/plugins/

完成后,当 terraform init 时,它将从此路径加载提供程序。(我使用这条路径是因为在以前的 TF 版本中,提供者就在那里)。

此外,当我从 Gitlab 在 openstack 上部署 TF 时,我创建了一个 docker 映像。这是 Dockerfile:

FROM hashicorp/terraform:0.14.7

RUN apk update && \
    apk upgrade && \
    apk add --no-cache \
        curl && \
    mkdir -p /root/.local/share/terraform/plugins/registry.terraform.io/terraform-provider-openstack/openstack/1.40.0/linux_amd64/ && \
    curl -L https://releases.hashicorp.com/terraform-provider-openstack/1.40.0/terraform-provider-openstack_1.40.0_linux_amd64.zip -o terraform-provider-openstack.zip && \
    unzip terraform-provider-openstack.zip && \
    mv terraform-provider-openstack_v1.40.0 /root/.local/share/terraform/plugins/registry.terraform.io/terraform-provider-openstack/openstack/1.40.0/linux_amd64/ && \
    terraform providers mirror /root/.local/share/terraform/plugins/

ENTRYPOINT /bin/sh
于 2021-08-11T16:53:16.367 回答
0

有一个技巧可以避免此类错误:

您可以将 openstack 提供程序定义为空白,并在您要执行 terraform plan/apply 命令的同一终端上获取您的 openrc ->

provider "openstack" {}

你的 openrc.sh 也应该包含它

read -sr OS_PASSWORD_INPUT
export TF_VAR_os_password=$OS_PASSWORD_INPUT

我的 provider.tf 有以下几行:

terraform {
  required_providers {
    openstack = {
      source = "terraform-provider-openstack/openstack"
      version = "~> 1.42.0"
    }
  }
}

provider "openstack" {}

我在我的模板中使用 os_password。在那种情况下,我使用 TF_VAR_os_password 作为环境变量,在 variable.tf 中我只是将变量名定义为:

variable "os_password" {
  sensitive = true
}
于 2021-08-04T12:44:12.670 回答