如何将 AMI 名称放入打包程序清单中

Posted

技术标签:

【中文标题】如何将 AMI 名称放入打包程序清单中【英文标题】:how to put AMI name in packer manifest 【发布时间】:2021-12-08 23:20:18 【问题描述】:

我想将 AMI 名称添加到打包程序清单中。我了解如何为 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"
    
  

我得到的错误是Unsupported attribute; This object does not have an attribute named "ami_name". 据此:https://www.packer.io/docs/templates/hcl_templates/blocks/source 看起来我可以访问的唯一属性是名称和类型。如何将ami_name 放入清单中?

【问题讨论】:

您可能必须在任一块中将name 设置为等于ami_name,然后使用source.namebuild.name 访问,尽管如果模板或清单扩展,这最终可能会变得不清楚更多来源和构建。 @MattSchuchard 能否提供设置name的示例 【参考方案1】:

我认为您无法获得这样的名称,但您可以将其放在本地,然后在您的构建器和清单中使用它,如下所示:

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
    
  

【讨论】:

我喜欢,谢谢!

以上是关于如何将 AMI 名称放入打包程序清单中的主要内容,如果未能解决你的问题,请参考以下文章

postgres和h2 db中名称中的#字符问题

如何为给定字符串中名称的符号赋值

批量获取文件名称和路径

使用打包程序从列表中创建 AMI

Spring Integration Java DSL:如何使用 channelMapping 方法路由到标题中名称的通道?

修剪 PySpark 中名称之间的额外空格