Terraform-源模块能否为每个实例操作系统使用不同的数据源?

Terraform-源模块能否为每个实例操作系统使用不同的数据源?,terraform,terraform0.12+,fedora-coreos,Terraform,Terraform0.12+,Fedora Coreos,我有一个“aws_实例”资源(在源代码模块中),它可以通过user_data=data.template_file.user data.rendered引导centos、coreos或ubuntu实例,使用以下数据块进行渲染 data "template_file" "user-data" { template = file("${path.module}/bootstrap-${var.os_distro}.sh") var

我有一个“aws_实例”资源(在源代码模块中),它可以通过
user_data=data.template_file.user data.rendered
引导centos、coreos或ubuntu实例,使用以下数据块进行渲染

data "template_file" "user-data" {
  template = file("${path.module}/bootstrap-${var.os_distro}.sh")

  vars = {
    access_port     = var.access_port
    service_port1   = var.service_port1
    docker_api_port = var.docker_api_port
  }
}
然后根据根模块中$os_distro变量的值加载并呈现相关文件(例如bootstrap centos.sh)

一切都很好,直到我从coreos换成fedora coreos。。。问题是,我现在需要先调用不同的数据源(ct\u config)来传输我的bootstrap-fcos.yaml文件以便点火

当我想要部署fedora coreos AMI时,是否可以在源模块中使用任何逻辑来使用不同的数据源?似乎完全违背了terraform模块的强大功能,只为这个新操作系统简单地创建一个新的源模块

源模块和根模块的突出部分包括:

源模块

resource ` "my-ec2-instance" {
  count                       = var.node_count
  availability_zone           = element(var.azs, count.index)
  subnet_id                   = var.aws_subnet_id
  private_ip                  = length(var.private_ips) > 0 ? element(var.private_ips, count.index) : var.private_ip
  ami                         = var.machine_ami
  instance_type               = var.aws_instance_type
  vpc_security_group_ids      = [aws_security_group.my-sg-group.id]
  key_name                    = var.key_name
  user_data                   = data.template_file.user-data.rendered
  monitoring                  = false
  ebs_optimized               = false
  associate_public_ip_address = var.public_ip

  root_block_device {
    volume_type           = var.root_volume_type
    volume_size           = var.root_volume_size
    delete_on_termination = true
  }
}


data "template_file" "user-data" {
  template = file("${path.module}/bootstrap-${var.os_distro}.sh")

  vars = {
    access_port     = var.access_port
    service_port1   = var.service_port1
    docker_api_port = var.docker_api_port
  }
}

variable "user_data" {
  type        = string
  description = "userdata used to bootstrap the node"
}

variable "os_distro" {
  type        = string
  description = "choose centos coreos or ubuntu to load either bootstrap-centos.sh, bootstrap-ubuntu.sh or bootstrap-coreos.sh from this module"
}
根模块

module "demo_coreos_stg_ec2" {
  source = ".../aws/ec2"                    # as per source module code above
  node_count        = local.node_count
  azs               = local.azs
  aws_subnet_id     = "subnet-c18c0fbb"                         
  private_ips       = ["172.31.16.20"]                          
  machine_ami       = data.aws_ami.fcos-stable-latest.id    # latest stable fedora coreos release
  aws_instance_type = "t2.micro"
  key_name          = "keys-2020"
  user_data         = data.ct_config.boot_config.rendered       # convert the boot config in yaml to the ignition config in json via ct (config transpiler)
  os_distro         = var.os_distro             # enables either bootstrap-centos.sh, bootstrap-ubuntu.sh or bootstrap-coreos.sh from this module

data "ct_config" "boot_config" {
  content = data.template_file.fcos.rendered
  strict = true
  pretty_print = true
}

data "template_file" "fcos" {
  template = file("${path.module}/bootstrap-fcos.yaml")
  vars = {
    access_port     = var.access_port
    service_port1   = var.service_port1
    docker_api_port = var.docker_api_port
  }
}

请注意,在使用模板文件数据源加载引导fcos.yaml进行插值之前,根模块需要能够首先使用ct\u config数据源。以前,所有3个操作系统都可以使用模板文件加载它们的.sh文件。

请共享源模块和调用资源,否则不清楚您是什么doing@9bO3av5fw5-请参见上面的代码,谢谢,请共享源模块和调用资源,否则不清楚您是什么doing@9bO3av5fw5-请参阅上面的代码,谢谢