4

不确定这是一个错误还是我做错了什么。当我在两个模块之间引入依赖关系时,依赖模块的 template_file 数据中的变量起作用并且 terraform 抱怨它找不到它。

我有两个模块,saltmaster 和 cassandra。目标是让 saltmaster 通过输出变量公开其 IP 地址,并让 cassandra 使用它。

如果我通过将 IP 地址硬编码为 cassandra 模块的输入来切断模块之间的依赖关系,那么所有资源都会正确创建。但是当我引入依赖项时,terraform 抱怨:

  • module.cassandra.aws_instance.momentum_cassandra[2]:资源“data.template_file.momentum_cassandra”没有变量“data.template_file.momentum_cassandra.*.vars.hostname”的属性“vars.hostname”

请注意,我在 template_file 的 vars 部分定义了 cassandra 实例的主机名,然后在 aws_instance 的 tags 块中使用它。并且它单独工作正常,但是当 cassandra 模块依赖于 saltmaster 模块时会中断。

卡桑德拉.tf

data "template_file" "momentum_cassandra" {
  count    = "${var.instance_count}"
  template = "${file("${path.module}/userdata.sh")}"

  vars {
    hostname   = "${format("%s%02d", var.name, count.index + 1)}"
    saltmaster = "${var.saltmaster}"
    salt_installtype = "${var.salt_installtype}"
    seed       = "${count.index == 0 ? "true" : "false"}"
    datacenter = "${var.datacenter}"
    rack       = "${var.rack}"
  }
}

resource "aws_instance" "momentum_cassandra" {
  count                  = 3
  provider               = "aws.us_dev"
  ami                    = "${var.ami}"
  instance_type          = "${var.instance_type}"
  key_name               = "${var.instance_key_name}"
  subnet_id              = "${element(var.vpc_subnet_ids, count.index)}"
  iam_instance_profile   = "${var.iam_instance_profile}"

  vpc_security_group_ids = ["${aws_security_group.momentum_cassandra.id}"]
  user_data              = "${data.template_file.momentum_cassandra.*.rendered[count.index]}"

  lifecycle {
    ignore_changes = ["ami", "user_data"]
  }
  tags {
    Name        = "${data.template_file.momentum_cassandra.*.vars.hostname[count.index]}"

主文件

module "cassandra" {
  source = "./tf-aws-momentum-cassandra"
  saltmaster = "${module.saltmaster.private_ip}"
}

saltmaster.tf

# create the userdata for bootstrapping
data "template_file" "saltmaster_userdata" {
  count    = "${var.instance_count}"
  template = "${file("${path.module}/userdata.sh")}"

  vars {
    hostname   = "${format("%s%02d", var.name, count.index + 1)}"
    saltmaster = "localhost"
    environment = "${var.environment}"
    installtype = "${var.installtype}"
  }
}
# create the salt master
resource "aws_instance" "momentum_salt_master" {
  provider               = "aws.us_dev"
  ami                    = "${var.ami}"
  instance_type          = "${var.instance_type}"
  key_name               = "${var.instance_key_name}"
  vpc_security_group_ids = ["${aws_security_group.momentum_salt_master.id}"]
  subnet_id              = "${element(var.vpc_subnet_ids, count.index)}"
  iam_instance_profile   = "${var.iam_instance_profile}"
  user_data              = "${element(data.template_file.saltmaster_userdata.*.rendered, count.index)}"


  tags {
    Name        = "${element(data.template_file.saltmaster_userdata.*.vars.hostname, count.index)}"
    Environment = "${var.environment}"
    Application = "${var.application}"
    Role        = "SaltMaster"
  }

  root_block_device {
    volume_type           = "gp2"
    volume_size           = 40
    delete_on_termination = true
  }
}

output "private_ip" {
  value = "${aws_instance.momentum_salt_master.private_ip}"
}
4

1 回答 1

0

根据您使用的 terraform 版本,这可能已在此处修复,但是,请尝试替换

"${data.template_file.momentum_cassandra.*.rendered[count.index]}"
"${data.template_file.momentum_cassandra.*.vars.hostname[count.index]}"

在查找cassandra.tf_element

"${element(data.template_file.momentum_cassandra.*.rendered, count.index)}"
"${element(data.template_file.momentum_cassandra.*.vars.hostname, count.index)}"

你实际使用的saltmaster.tf

于 2017-11-04T22:30:35.907 回答