Terraform如何通过cloudinit用户数据块进行迭代

时间:2019-03-05 12:55:17

标签: terraform cloud-init

我想使用标签“ ins-1” “ ins-2” 启动2个ec2实例。此外,在同时启动ins-1 / ins-2时,我想使用以下命令将其主机名设置为“服务器1” “服务器2” 使用cloudinit。下面的代码使用标签ins-1和ins-2完美地创建了两个实例,但是两个实例的主机名均为“ server-1”

variable "host_count" {
  default = "2"
}
variable "host_name_prefix" {
  default = "cluster-node-"
}
variable "aws_keypair_privatekey_filepath" {
  default = "../mykey.pem"
}


resource "aws_instance" "ins" {
  count = "${var.host_count}"
  ami       = "${lookup(var.AMIS, var.aws_region)}"
  instance_type = "t2.micro"
  subnet_id = "${aws_subnet.mypublic-1.id}"
  tags {
    Name = "server-${count.index + 1}"
  }
  vpc_security_group_ids = ["${aws_security_group.allow-ssh.id}"]
  user_data = "${data.template_cloudinit_config.example.rendered}"
  }


data "template_cloudinit_config" "example" {
  gzip          = true
  base64_encode = true
  # count = "${var.host_count}"

  part {
    content_type = "text/cloud-config"
    content      = <<EOF
    preserve_hostname: false
    fqdn: "server-${count.index + 1}"
    hostname: "server-${count.index + 1}"
EOF
  }
} 

非常感谢您对此提供的反馈。预先致谢

0 个答案:

没有答案