Adrien Delorme fb337f8867
hcl work on only/except (#9454)
* HCL2: allow to skip a named build block too

* test that excepting a build block works

* test only on a named build block

* add/update docs
2020-06-23 10:53:16 +02:00

65 lines
1.8 KiB
Plaintext

---
layout: docs
page_title: Only Except - HCL Configuration Language
sidebar_title: Only Except
description: >-
Only and Except can be used as a command line argument to selectively run
builds. Only and Except can also be used in a provisioner to not run it for a
source.
---
# Only and Except
`only` and `except` are keywords used to filter what runs in your Packer build,
they can be seen as a command line argument:
`@include 'commands/except.mdx'`
`@include 'commands/only.mdx'`
They can also be seen in a template to run or skip provisioners and/or
post-processors for a specific source:
```hcl
build {
name = "my_build"
sources [
"source.amazon-ebs.first-example",
"source.amazon-ebs.second-example",
]
provisioner "shell-local" {
only = ["source.amazon-ebs.second-example"]
inline = ["echo I will only run for the second example source"]
}
provisioner "shell-local" {
except = ["source.amazon-ebs.second-example"]
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`.
-> Note: In the cli `only` and `except` will match agains **build names** (for
example:`my_build.amazon-ebs.first-example`) but in a provisioner they will
match on the **source type** (for example:`source.amazon-ebs.third-example`).