Manifest 后处理器

原文


类型:manifest 制品构建器 ID:packer.post-processor.manifest

manifest 后处理器会生成一个 JSON 文件,其中包含 Packer 程序在运行期间生成的所有制品的列表。如果您的 Packer 模板包含多个构建,该后处理器可以帮助您跟踪与每个构建相对应的输出制品(文件、AMI ID、Docker 容器等)。

每次构建完成并更新清单文件中的数据时都会调用 manifest 后处理器。构建通过名称和类型进行标识,并包括其构建时间、制品 ID 和文件列表。

如果 Packer 程序使用 -force 标志运行,则清单文件将在每次 Packer 运行期间自动截断。不然的话,后续构建将追加到该文件中。您可以使用时间戳来查看哪个是最新的制品。

您可以多次指定 manifest 并将每个构建写入其自己的文件,或将所有构建写入同一文件。对于简单的构建,manifest 只需指定一次(见下文),但您也可以将其与其他后处理器(例如 Docker 和 Artifice)链接在一起。

配置

可选参数

  • output(string)- 将 manifest 写入此文件。默认值为 packer-manifest.json
  • strip_path (bool) - 仅写入文件名,不往清单文件中写入路径。默认为 false
  • strip_time (bool) - 不要在输出时写入 build_time 字段。
  • custom_data (map[string]string) - 添加到清单的任意数据。该参数将搭配模板引擎。因此可以在此字段中使用用户变量和模板函数。

注意:与大多数其他后处理器不同,keep_input_artifact 选项不适用于本后处理器。我们将始终保留清单的输入制品,因为删除我们刚刚记录的文件是无意义的。

使用示例

Json:

{
  "post-processors": [
    {
      "type": "manifest"
    }
  ]
}

HCL2:

post-processor "manifest" {}

一个更全面一些的例子:

Json:

{
  "post-processors": [
    {
      "type": "manifest",
      "output": "manifest.json",
      "strip_path": true,
      "custom_data": {
        "my_custom_data": "example"
      }
    }
  ]
}

HCL2:

post-processor "manifest" {
    output = "manifest.json"
    strip_path = true
    custom_data = {
      my_custom_data = "example"
    }
}

生成的清单文件看起来可能像这样:

{
  "builds": [
    {
      "name": "docker",
      "builder_type": "docker",
      "build_time": 1507245986,
      "files": [
        {
          "name": "packer_example",
          "size": 102219776
        }
      ],
      "artifact_id": "Container",
      "packer_run_uuid": "6d5d3185-fa95-44e1-8775-9e64fe2e2d8f",
      "custom_data": {
        "my_custom_data": "example"
      }
    }
  ],
  "last_run_uuid": "6d5d3185-fa95-44e1-8775-9e64fe2e2d8f"
}

如果再次运行构建,新的构建制品将添加到清单文件中,而不是替换原内容。可以使用 packer_run_uuid 从清单中获取特定的构建制品。

上面的清单是使用以下模板生成的:

Json:

{
  "builders": [
    {
      "type": "docker",
      "image": "ubuntu:latest",
      "export_path": "packer_example",
      "run_command": ["-d", "-i", "-t", "--entrypoint=/bin/bash", "{ {.Image} }"]
    }
  ],
  "post-processors": [
    {
      "type": "manifest",
      "output": "manifest.json",
      "strip_path": true,
      "custom_data": {
        "my_custom_data": "example"
      }
    }
  ]
}

HCL2:

source "docker" "docker"{
    image = "ubuntu:latest"
    export_path = "packer_example"
    run_command = ["-d", "-i", "-t", "--entrypoint=/bin/bash", "{ {.Image} }"]
}

build {
    sources = ["docker.docker"]

    post-processor "manifest" {
        output = "manifest.json"
        strip_path = true
        custom_data = {
          my_custom_data = "example"
        }
    }
}

清单文件的用法示例:

清单对于清理旧制品或将重要值打印到日志非常有用。以下示例使用 jq(一种用于解析 json 输出的命令行工具)来查找并打印由构建创建的 AMI 的 AWS ami-id。


#!/bin/bash

AMI_ID=$(jq -r '.builds[-1].artifact_id' manifest.json | cut -d ":" -f2)
echo $AMI_ID

results matching ""

    No results matching ""