Only 与 Except

原文


onlyexcept 是用于过滤 Packer 构建中运行步骤的关键字,它们可以被视为命令行参数:

  • -except=foo,bar,baz - 阻止运行具有给定逗号分隔名称的构建和后处理器。在遗留 JSON 模板中,构建名称默认为其构建器的类型(例如 dockeramazon-ebsvirtualbox-iso),除非在配置中指定了特定的名称属性。在 HCL2 模板中,“名称”是 source 块的 name 标签,除非内置 source 定义添加了 name 配置参数。被跳过的后处理器之后的所有后处理器都不会运行。因为后处理器可以存在于数组中,所以不同的后处理器链仍然可以运行。名称为空的后处理器将被忽略。
  • -only=foo,bar,baz - 运行具有给定逗号分隔名称的构建。在遗留 JSON 模板中,构建名称默认为其构建器的类型(例如 dockeramazon-ebsvirtualbox-iso),除非在配置中指定了特定的名称属性。在 HCL2 模板中,“名称”是 source 块的 name 标签,除非内置 source 定义添加了 name 配置参数。

它们也可以在模板中设置,以选择或跳过在特定构建上运行配置器和/或后处理器:

source "amazon-ebs" "first-example" {
}

source "amazon-ebs" "second-example" {
}

source "amazon-ebs" "third-example" {
}

build {
  name = "my_build"
  sources = [
    "source.amazon-ebs.first-example",
  ]
  source "source.amazon-ebs.second-example" {
    // setting the name field allows you to rename the source only for this
    // build section. To match this builder, you need to use
    // second-example-local-name, not second-example
    name = "second-example-local-name"
  }

  provisioner "shell-local" {
    only   = ["amazon-ebs.first-example"]
    inline = ["echo I will only run for the first example source"]
  }

  provisioner "shell-local" {
    except = ["amazon-ebs.second-example-local-name"]
    inline = ["echo I will never run for the second example source"]
  }
}

build {
  sources = [
    "source.amazon-ebs.third-example",
  ]
}

# this file will result in Packer creating three builds named:
#  my_build.amazon-ebs.first-example
#  my_build.amazon-ebs.second-example
#  amazon-ebs.third-example

请注意,上面演示的配置文件,命令行参数可以与 * 运算符一起使用:

  • packer build -only 'my_build.*' dir: 只会在名为 my_build 的块中运行构建。
  • packer build -only '*.amazon-ebs.*' dir: 将只运行带有 amazon-ebs 类型源的构建。
  • packer build -only '*.second-example-local-name' dir: 只会运行含有指定名称的构建。

请注意:在命令行参数中,onlyexcept 将匹配构建名称(例如:my_build.amazon-ebs.first-example),但在配置器中,它们将匹配源名称(例如:amazon-ebs.third-example)。

results matching ""

    No results matching ""