github.com/hashicorp/packer@v1.14.3/website/content/docs/templates/hcl_templates/onlyexcept.mdx (about)

     1  ---
     2  page_title: only and except keywords reference
     3  description: >-
     4    The `only` and `except` keywords are filters for Packer builds. Learn how to use `only` and `except` to selectively run provisioners and post-processors.  
     5  ---
     6  
     7  # `only` and `except` keywords
     8  
     9  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.
    10  
    11  ## Command line flags
    12  
    13  `@include 'commands/except.mdx'`
    14  
    15  `@include 'commands/only.mdx'`
    16  
    17  ## Templates 
    18  
    19  You can add the `only` and `except` keywords to run or skip provisioners and
    20  post-processors for a specific source:
    21  
    22  ```hcl
    23  source "amazon-ebs" "first-example" {
    24  }
    25  
    26  source "amazon-ebs" "second-example" {
    27  }
    28  
    29  source "amazon-ebs" "third-example" {
    30  }
    31  
    32  build {
    33    name = "my_build"
    34    sources = [
    35      "source.amazon-ebs.first-example",
    36    ]
    37    source "source.amazon-ebs.second-example" {
    38      // setting the name field allows you to rename the source only for this
    39      // build section. To match this builder, you need to use
    40      // second-example-local-name, not second-example
    41      name = "second-example-local-name"
    42    }
    43  
    44    provisioner "shell-local" {
    45      only   = ["amazon-ebs.first-example"]
    46      inline = ["echo I will only run for the first example source"]
    47    }
    48  
    49    provisioner "shell-local" {
    50      except = ["amazon-ebs.second-example-local-name"]
    51      inline = ["echo I will never run for the second example source"]
    52    }
    53  }
    54  
    55  build {
    56    sources = [
    57      "source.amazon-ebs.third-example",
    58    ]
    59  }
    60  
    61  # this file will result in Packer creating three builds named:
    62  #  my_build.amazon-ebs.first-example
    63  #  my_build.amazon-ebs.second-example
    64  #  amazon-ebs.third-example
    65  ```
    66  
    67  Note that CLI arguments can be used with a glob operator, using the previous
    68  configuration:
    69  
    70  - `packer build -only 'my_build.*' dir`: will only run the builds in blocks
    71    named `my_build`.
    72  
    73  - `packer build -only '*.amazon-ebs.*' dir`: will only run the builds with a
    74    source of type `amazon-ebs`.
    75  
    76  - `packer build -only '*.second-example-local-name' dir`: will only run that
    77    specifically named build.
    78  
    79  -> Note: In the cli `only` and `except` will match against **build names** (for
    80  example:`my_build.amazon-ebs.first-example`) but in a provisioner they will
    81  match on the **source name** (for example:`amazon-ebs.third-example`).