问题描述
我想删除以下打包脚本中的重复代码。我想要的是运行相同的构建,我希望其结果是两个名称不同的 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
),我将不会收到错误消息。
当然,这样的事情一定是可能的,对吧?还是我应该放弃?显然它应该像 the documentation implies 一样简单,但同一个例子实际上对我不起作用......
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)