使用 cloudinit 配置在 ec2-server 上挂载多个卷

问题描述

我想在我的 ec2 服务器中动态挂载所有附加的 EBS 卷。我正在使用 cloudinit 和 userdata 来执行我的 mountebs.sh 脚本文件

现在,问题是我的安装卷的脚本仅从 userdata 运行一次,因为 aws_instance 的计数变量为 1,因此,只有 3 个附加的 EBS 中的 1 个被安装。

有没有一种方法可以指定为所有附加的 EBS 运行该脚本?

data "template_file" "run_script_mountebs" {
  count=  var.disk_count
  template = file("${path.module}/mountebs.sh")
  vars={
    mapping= var.ebs_mapping[count.index]
    mount_pt = var.mount_pt[count.index]
  }
}

# Cloudint
data "template_cloudinit_config" "master" {
  count=  var.instance_count
  gzip          = true
  base64_encode = true

   part {
    filename     = "mountebs.sh"
    content_type = "text/x-shellscript"
    content      = data.template_file.run_script_mountebs[count.index].rendered
  }
}

# EC2-instance
resource "aws_instance" "aws-ec2-linux" {
  count = var.instance_count
  ami   = data.aws_ami.ubuntu.id
  instance_type          = var.instance_type
  monitoring             = var.enable_monitoring
  vpc_security_group_ids = [var.security_groups]
  associate_public_ip_address= var.associate_public_ip_address
  subnet_id              = var.subnet_id[count.index]
  root_block_device {
    volume_type           = var.storage_os_disk["volume_type"]
    volume_size           = var.storage_os_disk["volume_size"]
    delete_on_termination = var.storage_os_disk["delete_on_termination"]
    encrypted             = var.storage_os_disk["encrypted"]
    kms_key_id            = data.aws_kms_key.datakey.arn
  }
  user_data = data.template_cloudinit_config.master.*.rendered[count.index]
  tags = merge(
    var.default_tags,map(
      "Name",format("%s-%s-%s",var.default_tags["ApplicationName"],"ec2",count.index)
    )
  )
}

#mount-ebs.sh
lsblk
if file -s ${mapping} | grep "${mapping}: data"; then
    echo "Creating fs"
   sudo mkfs -t ext4 ${mapping}
fi

sudo mkdir /home/ubuntu/${mount_pt}
# mount it
echo "${mapping}       /home/ubuntu/${mount_pt}   ext4    defaults,nofail  0 2" >> /etc/fstab
echo "Mounting"
mount -a
df -h .

P.S 我曾尝试在 template_file 资源中分配一个新的计数变量 (disk_count),但这对我不起作用。

非常感谢您对此的反馈。非常感谢提前!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)