kubernetes - 如何从 Terraform 中的不同目录访问文件和变量?

标签 kubernetes terraform terraform-provider-gcp

我们在 Linux 系统上有以下目录结构。

 /root
   ├─dirA
   │  ├─main.tf
   │  ├─terraform.tfvars
   │  └─variables.tf
   └─dirB
      └─main.tf

==FIRST==

我们在 dirAmain.tf 文件中使用以下代码片段来创建本地 kubeconfig 文件。

resource "local_file" "kubeconfig" {
  content  = module.gke_auth.kubeconfig_raw
  filename = "./kubeconfig"
}

现在我们想在以下代码段中访问 dirBmain.tf 文件中的这个 kubeconfig 文件。请建议如何做到这一点?

provider "kubernetes" {
  config_path    = "<PATH_TO_KUBECONFIG_FILE>"
}

==SECOND==

我们在 dirAterraform.tfvars 文件中定义了一些变量,我们想在 main.tf 中访问这些变量dirB 的文件。请建议如何执行此操作。

最佳答案

我们已经通过以下设置解决了这两个问题。

文件 dirA/main.tf 包含类似于

resource "local_file" "kubeconfig" {
  content  = module.gke_auth.kubeconfig_raw
  filename = "${path.module}/kubeconfig"
}

output "kubeconfig_file" {
  value = "${path.cwd}/kubeconfig"
}

文件 dirB/main.tf 包含类似于

data "terraform_remote_state" "kubeconfig_file" {
  backend = "local"

  config = {
    path = "${path.module}/../dirA/terraform.tfstate"
  }
}

provider "kubernetes" {
  config_path = "${data.terraform_remote_state.kubeconfig_file.outputs.kubeconfig_file}"
}

最后:

cd dirA
terraform apply
cd ../dirB
terraform apply

注意:以类似的方式,我们可以从不同目录中的堆栈访问变量

关于kubernetes - 如何从 Terraform 中的不同目录访问文件和变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72630074/

相关文章:

kubernetes - k8s 仪表板中的 Argo Workflow Engine 实体

powershell - Windows 自定义不适用于 Terraform

google-cloud-platform - Terraform 和 GCP : Google kubernetes cluster problem: Can't see monitoring section (memory and cpu) inside workloads (deployments, 状态集)

kubernetes - GKE 使用 gcePersistentDisk

kubernetes - Kubernetes AWS EKS无法加载资源:net::ERR_NAME_NOT_RESOLVED

kubernetes - 哪种 Kubernetes 模式适用于对等点配置略有不同的点对点场景?

Terraform 0.13 - 模块、for_each 和提供者

git - 我应该将 .tfstate 文件提交到 Git 吗?

kubernetes - 无法在 Terraform 中部署 Helm Chart(Kubernetes 集群无法访问)

google-cloud-platform - 使用 Google Cloud Platform 时 Terraform 状态锁定的机制是什么?