You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
packer/website/content/docs/templates/hcl_templates/onlyexcept.mdx

87 lines
2.9 KiB

---
page_title: only and except keywords reference
description: >-
The `only` and `except` keywords are filters for Packer builds. Learn how to use `only` and `except` to selectively run provisioners and post-processors.
---
⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
> [!IMPORTANT]
> **Documentation Update:** Product documentation previously located in `/website` has moved to the [`hashicorp/web-unified-docs`](https://github.com/hashicorp/web-unified-docs) repository, where all product documentation is now centralized. Please make contributions directly to `web-unified-docs`, since changes to `/website` in this repository will not appear on developer.hashicorp.com.
⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
# `only` and `except` keywords
The `only` and `except` keywords are filters that let you selectively run provisioners and post-processors in your Packer build. You can use the keywords in your Packer templates and as flags on the Packer CLI.
## Command line flags
`@include 'commands/except.mdx'`
`@include 'commands/only.mdx'`
## Templates
You can add the `only` and `except` keywords to run or skip provisioners and
post-processors for a specific source:
```hcl
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
```
Note that CLI arguments can be used with a glob operator, using the previous
configuration:
- `packer build -only 'my_build.*' dir`: will only run the builds in blocks
named `my_build`.
- `packer build -only '*.amazon-ebs.*' dir`: will only run the builds with a
source of type `amazon-ebs`.
- `packer build -only '*.second-example-local-name' dir`: will only run that
specifically named build.
-> Note: In the cli `only` and `except` will match against **build names** (for
example:`my_build.amazon-ebs.first-example`) but in a provisioner they will
match on the **source name** (for example:`amazon-ebs.third-example`).