gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:39:33 26 4
gpt4 key购买 nike

我遇到了一个问题,我在从 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}"
}

如果我从 modules/kubespray/ 中的子模块内部运行我的 terraform init/apply它工作正常

如果我从我的 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



帮助将非常感激

最佳答案

当你运行 terraform ,该目录成为一个隐含的“根模块”。正如 Input Variables 中所解释的那样文档中,根模块可以从 CLI 或环境访问 TF 输入,但所有其他被调用的模块都需要传入这些。

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

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

关于terraform - 在模块内的模块之间传递 Terraform 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59995337/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com