简体   繁体   English

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

[英]how to put AMI name in packer manifest

I would like to get the AMI name into the packer manifest.我想将 AMI 名称添加到打包程序清单中。 I understand how to get this for the AMI id but its different for the name.我了解如何为 AMI id 获取此信息,但名称不同。 I have:我有:

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}"
    }
  }
}

The error I am getting is Unsupported attribute; This object does not have an attribute named "ami_name".我得到的错误是Unsupported attribute; This object does not have an attribute named "ami_name". Unsupported attribute; This object does not have an attribute named "ami_name". According to this: https://www.packer.io/docs/templates/hcl_templates/blocks/source it looks like the only attributes I do have access to are name and type.根据这个: https : //www.packer.io/docs/templates/hcl_templates/blocks/source看起来我可以访问的唯一属性是名称和类型。 How can I get the ami_name into the manifest?如何将ami_name放入清单?

I don't think you can get the name like that but you can put it in a local and then use it in both your builder and in the manifest like this:我不认为你可以得到这样的名字,但你可以把它放在本地,然后在你的构建器和清单中使用它,如下所示:

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
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM