如何在Packer清单中添加AMI名称

3

我希望能将AMI名称添加到Packer清单中。我知道如何获取AMI ID的名称,但名称的获取方式有所不同。 目前我有:

data "amazon-ami" "ubuntu" {
  filters = {
     name = "ubuntu-minimal/images/hvm-ssd/ubuntu-focal-20.04-amd64-minimal-*"
     root-device-type    = "ebs"
     virtualization-type = "hvm"
  }
  most_recent = true
  region      = "us-east-1"
}
source "amazon-ebs" "ui" {
  ami_name      = "my-ami-${formatdate("YYYY-MM-DD-hhmmss", timestamp())}"
  instance_type = "t3.small"
  region        = "us-east-1"
  source_ami    = "${data.amazon-ami.ubuntu.id}"
  ssh_pty       = true
  ssh_username  = "ubuntu"
}
build {
  sources = ["source.amazon-ebs.ui"]
  
  post-processor "manifest" {
    output = "manifest.json"
    strip_path = true
    custom_data = {
      version = "${source.ami_name}"
    }
  }
}

我收到的错误是 不支持的属性;此对象没有名为“ami_name”的属性。 根据这个链接: https://www.packer.io/docs/templates/hcl_templates/blocks/source 看起来我只能访问 name 和 type 属性。我怎样才能将 ami_name 添加到清单中呢?

你可能需要在任一代码块中将 name 设置为 ami_name,然后使用 source.namebuild.name 进行访问。但是,如果模板或清单扩展到更多源和构建,则可能会变得不太清晰。 - Matt Schuchard
@MattSchuchard,您能否提供一个设置“name”的示例? - SNR
1个回答

4

我认为您不能这样获得名称,但可以将其放到本地中,然后像这样在您的构建器和清单中使用:

locals { 
  my_ami_name = "my-ami-${formatdate("YYYY-MM-DD-hhmmss", timestamp())}"
}
data "amazon-ami" "ubuntu" {
  filters = {
     name = "ubuntu-minimal/images/hvm-ssd/ubuntu-focal-20.04-amd64-minimal-*"
     root-device-type    = "ebs"
     virtualization-type = "hvm"
  }
  owners      = ["099720109477"]
  most_recent = true
  region      = "us-east-1"
}
source "amazon-ebs" "ui" {
  ami_name      = local.my_ami_name
  instance_type = "t3.small"
  region        = "us-east-1"
  source_ami    = "${data.amazon-ami.ubuntu.id}"
  ssh_pty       = true
  ssh_username  = "ubuntu"
}
build {
  sources = ["source.amazon-ebs.ui"]
  
  post-processor "manifest" {
    output = "manifest.json"
    strip_path = true
    custom_data = {
      version = local.my_ami_name
    }
  }
}

我喜欢那个,谢谢! - user1584120

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接