3

When I execute terraform init I have the following output :

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

And this is my provider.tf file (Inspired from the official Terraform's documentation):

# 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" {
  # ...
#}

And when I check the version I have this:

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

This is when I run 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

And finally 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]
Elias Arellano
  • 433
  • 5
  • 16

2 Answers2

1

To manually manage provider versions, use terraform mirror.

Basically: create provider path, download && install provider, set terraform mirror path.

# 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/

Once done, when terraform init, it will load provider from this path. (I use this path because in previous versions of TF, providers were there).

In addition, I've created a docker image when I deploy TF on openstack from Gitlab. Here is the 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
Aarto
  • 23
  • 4
0

There is a hack to avoid such error:

You can define openstack provider as blank and source your openrc on the same terminal where you are going to execute your terraform plan/apply command ->

provider "openstack" {}

Your openrc.sh should contain that as well

read -sr OS_PASSWORD_INPUT
export TF_VAR_os_password=$OS_PASSWORD_INPUT

My provider.tf has the below lines:

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

provider "openstack" {}

I am using os_password in my template. In that case I used TF_VAR_os_password as environment variable and within variable.tf I am just defining the variable name as:

variable "os_password" {
  sensitive = true
}
lpeter
  • 1
  • 1
  • Ok, and in which step do I add the tenant_name, user_name, Region, etc.? – Elias Arellano Aug 04 '21 at 13:49
  • Once you source your openrc file ( which should contain tenant_name, user_name, Region, etc.) then you don't really need them to declare/define unless it's specifically required. I mean to say, generally to provision instances you don't require to mention it in terraform template. – lpeter Aug 04 '21 at 14:26