当前位置 : 主页 > 手机开发 > 其它 >

Terraform模块依赖项会破坏template_file

来源:互联网 收集:自由互联 发布时间:2021-06-22
不确定这是一个错误还是我做错了什么.当我在两个模块之间引入依赖性时,依赖模块的template_file数据中的变量起作用,而terraform抱怨它无法找到它. 我有两个模块,saltmaster和cassandra.目标是
不确定这是一个错误还是我做错了什么.当我在两个模块之间引入依赖性时,依赖模块的template_file数据中的变量起作用,而terraform抱怨它无法找到它.

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

如果我切断模块之间的依赖关系,通过将ip地址硬编码为cassandra模块的输入,则可以正确创建所有资源.但是当我介绍依赖时,terraform抱怨:

  • module.cassandra.aws_instance.momentum_cassandra[2]: Resource ‘data.template_file.momentum_cassandra’ does not have attribute ‘vars.hostname’ for variable ‘data.template_file.momentum_cassandra.*.vars.hostname’

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

cassandra.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]}"

main.tf

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}"
}
根据您使用的terraform版本,可能已经修复了 here,但是,请尝试更换

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

在cassandra.tf中使用元素查找

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

你实际上在saltmaster.tf中使用它

网友评论