【发布时间】:2021-12-25 05:45:27
【问题描述】:
我需要从路径中获取 terraform 父模块名称,然后将其作为变量传递给其他模块。可能吗?。 例子: 从模块child1可以访问到模块child2中的目录src/yaml
父模块:
module "parent" {
source = "s3::https://s3.amazonaws.com/tf-modules-zip/child1.zip"
some_variable = "foo"
}
Child1 模块:
variable "some_variable" {
type = string
description = "Any value"
}
locals {
os_module_name = this.module.name
path_to_yamls = "${local.os_module_name}.child2/src/yaml"
}
module "child1" {
source = "s3::https://s3.amazonaws.com/tf-modules-zip/child2.zip"
some_variable = var.some_variable
path_to_yaml_files = local.path_to_yamls
}
Child2 模块:
variable "some_variable" {
type = string
description = "Any value"
}
variable "path_to_yaml_files" {
type = string
description = "Path to retrieve the yaml files"
}
locals {
path_to_configs = var.path_to_yaml_files
exclusions = yamldecode(file("${local.path_to_configs}/exclusions.yaml"))
}
resource "null_resource" "child2" {
local-exec {
interpreter = ["/bin/bash" ,"-c"],
command = <<-EOT
exec "command1 -yaml ${local.exclusions}"
exec "command2 -var ${var.some_variable}"
EOT
}
}
【问题讨论】:
标签: terraform terraform0.12+ terraform-modules