Terraform:模块输出未被识别为变量

时间:2018-11-27 14:35:55

标签: kubernetes terraform devops terraform-provider-gcp

我认为只是快速检查一下,也许我的眼睛就变得困惑了。我正在将一个整体的terraform文件分解为模块。

我的main.tf仅调用两个模块,gke用于google kubernetes引擎,而storage则在先前创建的集群上创建持久卷。

模块gke的{​​{1}}输出以下内容:

outputs.tf

然后在output "client_certificate" { value = "${google_container_cluster.kube-cluster.master_auth.0.client_certificate}" sensitive = true } output "client_key" { value = "${google_container_cluster.kube-cluster.master_auth.0.client_key}" sensitive = true } output "cluster_ca_certificate" { value = "${google_container_cluster.kube-cluster.master_auth.0.cluster_ca_certificate}" sensitive = true } output "host" { value = "${google_container_cluster.kube-cluster.endpoint}" sensitive = true } 中的存储模块中,我有:

main.tf

然后在根client_certificate = "${base64decode(var.client_certificate)}" client_key = "${base64decode(var.client_key)}" cluster_ca_certificate = "${base64decode(var.cluster_ca_certificate)}" host = "${var.host}" 中,我具有以下内容:

main.tf

从我所见,它看起来不错。 certs,key和host变量的值应由client_certificate = "${module.gke.client_certificate}" client_key = "${module.gke.client_key}" cluster_ca_certificate = "${module.gke.cluster_ca_certificate}" host = "${module.gke.host}" gke模块输出,由root的outputs.tf拾取,然后以下列形式传递到main.tf常规变量。

我弄错了吗?还是我只是疯了,似乎不对劲。

我被问到在运行计划时没有填写变量。

编辑:

添加一些其他信息,包括我的代码。

如果我手动为变量添加虚拟条目,则要求输入以下错误:

storage

似乎在抱怨data.google_container_cluster资源需要project属性。但这不是有效资源。是给提供者的,但已为提供者填写了。

以下代码:

文件夹结构:

Macbook: $ terraform plan
var.client_certificate
  Enter a value: 1

var.client_key
  Enter a value: 2

var.cluster_ca_certificate
  Enter a value: 3

var.host
  Enter a value: 4
...
(filtered out usual text)
...
 * module.storage.data.google_container_cluster.kube-cluster: 1 error(s) occurred:

* module.storage.data.google_container_cluster.kube-cluster: data.google_container_cluster.kube-cluster: project: required field is not set

root-folder / gke / main.tf:

root-folder/
├── gke/
│   ├── main.tf
│   ├── outputs.tf
│   ├── variables.tf
├── storage/
│   ├── main.tf
│   └── variables.tf
├── main.tf
├── staging.json
├── terraform.tfvars
└── variables.tf

root-folder / gke / outputs.tf:

provider "google" {
  credentials = "${file("staging.json")}"
  project     = "${var.project}"
  region      = "${var.region}"
  zone        = "${var.zone}"
}

resource "google_container_cluster" "kube-cluster" {
  name               = "kube-cluster"
  description        = "kube-cluster"
  zone               = "europe-west2-a"
  initial_node_count = "2"
  enable_kubernetes_alpha = "false"
  enable_legacy_abac = "true"

  master_auth {
    username = "${var.username}"
    password = "${var.password}"
  }

  node_config {
    machine_type = "n1-standard-2"
    disk_size_gb = "20"
    oauth_scopes = [
      "https://www.googleapis.com/auth/compute",
      "https://www.googleapis.com/auth/devstorage.read_only",
      "https://www.googleapis.com/auth/logging.write",
      "https://www.googleapis.com/auth/monitoring"
    ]
  }
}

root-folder / gke / variables.tf:

output "client_certificate" {
  value     = "${google_container_cluster.kube-cluster.master_auth.0.client_certificate}"
 sensitive = true
}
output "client_key" {
  value     = "${google_container_cluster.kube-cluster.master_auth.0.client_key}"
 sensitive = true
}
output "cluster_ca_certificate" {
  value     = "${google_container_cluster.kube-cluster.master_auth.0.cluster_ca_certificate}"
 sensitive = true
}
output "host" {
  value     = "${google_container_cluster.kube-cluster.endpoint}"
 sensitive = true
}

/root-folder/storage/main.cf:

variable "region" {
  description = "GCP region, e.g. europe-west2"
  default = "europe-west2"
}
variable "zone" {
  description = "GCP zone, e.g. europe-west2-a (which must be in gcp_region)"
  default = "europe-west2-a"
}
variable "project" {
  description = "GCP project name"
}
variable "username" {
  description = "Default admin username"
}
variable "password" {
  description = "Default admin password"
}

/root/storage/variables.tf:

provider "kubernetes" {
  host     = "${var.host}"
  username = "${var.username}"
  password = "${var.password}"
  client_certificate     = "${base64decode(var.client_certificate)}"
  client_key             = "${base64decode(var.client_key)}"
  cluster_ca_certificate = "${base64decode(var.cluster_ca_certificate)}"
}
data "google_container_cluster" "kube-cluster" {
  name   = "${var.cluster_name}"
  zone   = "${var.zone}"
}
resource "kubernetes_storage_class" "kube-storage-class" {
  metadata {
    name = "kube-storage-class"
  }
  storage_provisioner = "kubernetes.io/gce-pd"
  parameters {
    type = "pd-standard"
  }
}
resource "kubernetes_persistent_volume_claim" "kube-claim" {
  metadata {
    name      = "kube-claim"
  }
  spec {
    access_modes       = ["ReadWriteOnce"]
    storage_class_name = "kube-storage-class"
    resources {
      requests {
        storage = "10Gi"
      }
    }
  }
}

/root-folder/main.tf:

variable "username" {
  description = "Default admin username."
}
variable "password" {
  description = "Default admin password."
}
variable "client_certificate" {
  description = "Client certificate, output from the GKE/Provider module."
}
variable "client_key" {
  description = "Client key, output from the GKE/Provider module."
}
variable "cluster_ca_certificate" {
  description = "Cluster CA Certificate, output from the GKE/Provider module."
}
variable "cluster_name" {
  description = "Cluster name."
}
variable "zone" {
  description = "GCP Zone"
}
variable "host" {
  description = "Host endpoint, output from the GKE/Provider module."
}

/root-folder/variables.tf:

module "gke" {
  source = "./gke"
  project = "${var.project}"
  region = "${var.region}"
  username = "${var.username}"
  password = "${var.password}"
}
module "storage" {
  source = "./storage"
  host = "${module.gke.host}"
  username = "${var.username}"
  password = "${var.password}"
  client_certificate = "${module.gke.client_certificate}"
  client_key = "${module.gke.client_key}"
  cluster_ca_certificate = "${module.gke.cluster_ca_certificate}"
  cluster_name = "${var.cluster_name}"
  zone = "${var.zone}"
}

出于明显的原因,我不会粘贴variable "project" {} variable "region" {} variable "username" {} variable "password" {} variable "gc_disk_size" {} variable "kpv_vol_size" {} variable "host" {} variable "client_certificate" {} variable "client_key" {} variable "cluster_ca_certificate" {} variable "cluster_name" {} variable "zone" {} staging.json的内容:)

1 个答案:

答案 0 :(得分:3)

在您的/root-folder/variables.tf中,删除以下条目:

variable "host" {}
variable "client_certificate" {}
variable "client_key" {}
variable "cluster_ca_certificate" {}

那些本身不是根级别的Terraform代码所需的变量。而是将它们作为1个模块的输出->输入传递给第二个模块。