0

我想在以下打包脚本中删除重复代码。我想要的是运行相同的构建,我希望结果是两个具有不同名称的 AMI。

这是一个工作示例的味道:

source "amazon-ebs" "head" {
    profile = "$(var.aws_profile}"
    access_key = "${var.aws_access_key}"
    secret_key = "${var.aws_secret_key}"
    instance_type = "${var.instance_type}"
    region = "${var.aws_region}"
    source_ami_filter {
        filters = {
            virtualization-type = "hvm"
            name = "${var.base_ami_name}"
            root-device-type = "ebs"
        }
        owners = [ "1234567890" ]
        most_recent = true
    }
    launch_block_device_mappings {
            device_name = "/dev/sda1"
            volume_size = "${var.volume_size}"
            volume_type = "gp2"
            delete_on_termination = true
    }
    ssh_username = "centos"
    ami_name = "head-{{timestamp}}"
}


source "amazon-ebs" "worker" {
    profile = "$(var.aws_profile}"
    access_key = "${var.aws_access_key}"
    secret_key = "${var.aws_secret_key}"
    instance_type = "${var.instance_type}"
    region = "${var.aws_region}"
    source_ami_filter {
        filters = {
            virtualization-type = "hvm"
            name = "${var.base_ami_name}"
            root-device-type = "ebs"
        }
        owners = [ "1234567890" ]
        most_recent = true
    }
    launch_block_device_mappings {
            device_name = "/dev/sda1"
            volume_size = "${var.volume_size}"
            volume_type = "gp2"
            delete_on_termination = true
    }
    ssh_username = "centos"
    ami_name = "worker-{{timestamp}}"
}



################
# Builder
################

build {
    #source "amazon-ebs.head" {
    #  name = "worker"
    #}

    sources = ["source.amazon-ebs.head", "source.amazon-ebs.worker"]

显然,这有点长,几乎完全重复。我想要的是更接近:

source "amazon-ebs" "head" {
    profile = "$(var.aws_profile}"
    access_key = "${var.aws_access_key}"
    secret_key = "${var.aws_secret_key}"
    instance_type = "${var.instance_type}"
    region = "${var.aws_region}"
    source_ami_filter {
        filters = {
            virtualization-type = "hvm"
            name = "${var.base_ami_name}"
            root-device-type = "ebs"
        }
        owners = [ "125523088429" ]
        most_recent = true
    }
    launch_block_device_mappings {
            device_name = "/dev/sda1"
            volume_size = "${var.volume_size}"
            volume_type = "gp2"
            delete_on_termination = true
    }
    ssh_username = "centos"
    ami_name = "head-{{timestamp}}"
}


################
# Builder
################

build {
    source "amazon-ebs.head" {
      ami_name = "worker-{{timestamp}}"
    }

    sources = ["source.amazon-ebs.head", ]

或任何类似的东西。但我似乎无法让它工作。首先,我无法ami_name从第一个来源中完全删除,因为这是必填字段。{{.Name}}如果我尝试使用变量,例如将名称字段添加到源然后使用而不是显式,我不会得到错误ami_name

当然,这样的事情一定是可能的吧?还是我应该放弃?显然它应该像文档所暗示的那样简单,但同样的例子对我来说根本不起作用......

4

0 回答 0