在模块内的模块之间传递 Terraform 变量?

Passing Terraform variables between Modules within Modules?

我在从 Kubespray 项目构建模块时遇到了问题。

我有以下文件夹结构:

terraform/
modules/
   kubespray/
      modules/
         compute/
         ips/
         network/
      main.tf
      variables.tf
roles/
    kubespray/
        terraform_setup/
             main.tf

kubespray/main.tf 模块中,传递了一些变量:

module "ips" {
  source = "./modules/ips"

  number_of_k8s_masters         = "${var.number_of_k8s_masters}"
  number_of_k8s_masters_no_etcd = "${var.number_of_k8s_masters_no_etcd}"
  number_of_k8s_nodes           = "${var.number_of_k8s_nodes}"
  floatingip_pool               = "${var.floatingip_pool}"
  number_of_bastions            = "${var.number_of_bastions}"
  external_net                  = "${var.external_net}"
  network_name                  = "${var.network_name}"
  router_id                     = "${module.network.router_id}"
}

module "compute" {
  source = "./modules/compute"
  ...
  k8s_master_fips                              = "${module.ips.k8s_master_fips}"
  k8s_master_no_etcd_fips                      = "${module.ips.k8s_master_no_etcd_fips}"
  k8s_node_fips                                = "${module.ips.k8s_node_fips}"
  bastion_fips                                 = "${module.ips.bastion_fips}"
  ...
}
output "private_subnet_id" {
  value = "${module.network.subnet_id}"
}

output "floating_network_id" {
  value = "${var.external_net}"
}

output "router_id" {
  value = "${module.network.router_id}"
}

output "k8s_master_fips" {
  value = "${concat(module.ips.k8s_master_fips, module.ips.k8s_master_no_etcd_fips)}"
}

output "k8s_node_fips" {
  value = "${module.ips.k8s_node_fips}"
}

output "bastion_fips" {
  value = "${module.ips.bastion_fips}"
}

如果我 运行 我的 terraform init/apply 来自 modules/kubespray/ 中的子模块,它工作正常

如果我 运行 来自我的 role/kubespray 那有

module "kubespray" {
  source    = "../../../modules/kubespray"
  providers = {
    openstack.src = openstack
  }
}

它失败了

Error: Invalid index

on ../../../modules/kubespray/modules/compute/main.tf line 670, in resource "openstack_compute_floatingip_associate_v2" "k8s_node": 670: floating_ip = "${var.k8s_node_fips[count.index]}" |---------------- | count.index is 0 | var.k8s_node_fips is empty list of dynamic

非常感谢帮助

无需询问更多信息,我的猜测是您传入了一个空数组 (${var.k8s_node_fips[count.index]}),var.node_root_volume_size_in_gbvar.number_of_k8s_nodes_no_floating_ip 的值大于 0,尝试为资源声明所需的字符串索引空数组时导致错误。

Error: Invalid index

on ../../../modules/kubespray/modules/compute/main.tf line 670, in resource "openstack_compute_floatingip_associate_v2" "k8s_node": 670: floating_ip = "${var.k8s_node_fips[count.index]}" |---------------- | count.index is 0 | var.k8s_node_fips is empty list of dynamic

根据错误,这似乎是${var.k8s_node_fips[count.index]}处的插值错误。 看起来正在发生的事情是变量为空,但 count 设置为非零数字,从而导致 TF 出错。

看原项目代码,好像count变量依赖var.node_root_volume_size_in_gb为0才设置为0;否则会将计数设置为 var.number_of_k8s_nodes_no_floating_ip.

的值

错误似乎发生在的代码段:

resource "openstack_compute_instance_v2" "k8s_node_no_floating_ip_custom_volume_size" {
  name              = "${var.cluster_name}-k8s-node-nf-${count.index+1}"
  count             = "${var.node_root_volume_size_in_gb > 0 ? var.number_of_k8s_nodes_no_floating_ip : 0}"
  availability_zone = "${element(var.az_list, count.index)}"
  image_name        = "${var.image}"
  flavor_id         = "${var.flavor_k8s_node}"
  key_pair          = "${openstack_compute_keypair_v2.k8s.name}"

  block_device {
    uuid                  = "${data.openstack_images_image_v2.vm_image.id}"
    source_type           = "image"
    volume_size           = "${var.node_root_volume_size_in_gb}"
    boot_index            = 0
    destination_type      = "volume"
    delete_on_termination = true
  }

希望对您有所帮助

当您 运行 terraform 时,目录变为隐式 "root module"。正如 Input Variables 文档中所述,根模块可以从 CLI 或环境访问 TF 输入,但所有其他被调用的模块都需要传入这些输入。

我假设发生的情况可能是您的 ~kubespray/variables.tf 输入变量分配了正在生效的默认值。如果是这种情况,则应将这些复制到 ~terraform_setup/variables.tf 并传递到调用模块:

module "kubespray" {
  source    = "../../../modules/kubespray"
  providers = {
    openstack.src = openstack
  }
}