June 20-22 Announcing HashiConf Europe full schedule: keynotes, sessions, labs & more Register Now
  • Infrastructure
    • terraform
    • packer
  • Networking
    • consul
  • Security
    • vault
    • boundary
  • Applications
    • nomad
    • waypoint
    • vagrant
  • HashiCorp Cloud Platform

    A fully managed platform to automate infrastructure on any cloud with HashiCorp products.

    • consul
    • terraform
    • vault
    • packerbeta
    Visit cloud.hashicorp.com
  • Overview
  • Tutorials
  • Docs
  • Plugins
  • Community
GitHubInstall PackerTry HCP Packer
    • v1.8.x (latest)
    • v1.7.x
    • v1.6.x
    • v1.5.x

  • Terminology
    • Overview
    • init
      • Overview
      • install
      • installed
      • remove
      • required
    • build
    • console
    • fix
    • fmt
    • inspect
    • validate
    • hcl2_upgrade
    • Overview
      • Overview
        • Overview
          • Overview
          • hcp_packer_registry
          • source
          • provisioner
          • post-processor
          • post-processors
        • locals
        • source
        • variable
        • packer
        • data
        • Overview
          • aws_secretsmanager
          • consul
          • env
          • vault
          • abs
          • ceil
          • floor
          • log
          • max
          • min
          • parseint
          • pow
          • signum
          • chomp
          • format
          • formatlist
          • indent
          • join
          • lower
          • replace
          • regex_replace
          • regex
          • regexall
          • split
          • strrev
          • substr
          • title
          • trim
          • trimprefix
          • trimsuffix
          • trimspace
          • upper
          • chunklist
          • coalesce
          • coalescelist
          • compact
          • concat
          • contains
          • distinct
          • element
          • flatten
          • index
          • keys
          • length
          • lookup
          • merge
          • range
          • reverse
          • setintersection
          • setproduct
          • setunion
          • slice
          • sort
          • values
          • zipmap
          • base64decode
          • base64encode
          • csvdecode
          • jsondecode
          • jsonencode
          • urlencode
          • yamldecode
          • yamlencode
          • abspath
          • basename
          • dirname
          • file
          • fileexists
          • fileset
          • pathexpand
          • templatefile
          • formatdate
          • timeadd
          • timestamp
          • legacy_isotime
          • legacy_strftime
          • bcrypt
          • md5
          • rsadecrypt
          • sha1
          • sha256
          • sha512
          • uuidv4
          • uuidv5
          • cidrhost
          • cidrnetmask
          • cidrsubnet
          • cidrsubnets
          • can
          • convert
          • try
      • Variables
      • Locals
      • Contextual Variables
      • Data Sources
      • Path Variables
      • Syntax
      • Only Except
      • Expressions
      • JSON Syntax
      • Overview
      • Builders
      • Communicators
      • Engine
      • Post-Processors
      • Provisioners
      • User Variables

    • Overview
    • SSH
    • WINRM
    • Overview
    • File
    • Null
    • Custom
    • Community-Supported
    • Overview
      • Overview
      • Iteration
      • Image
      • Image-Deprecated
    • Overview
    • Breakpoint
    • File
    • PowerShell
    • Shell
    • Shell (Local)
    • Windows Shell
    • Windows Restart
    • Custom
    • Community-Supported
    • Overview
    • Artifice
    • Compress
    • Checksum
    • Manifest
    • Shell (Local)
    • Community-Supported
  • External Plugins

  • Installing Packer
  • Configuring Packer

    • Overview
      • Overview
      • Custom Builders
      • Custom Post-Processors
      • Custom Provisioners
      • Custom Data Sources
    • HCP Packer Support
  • Integration Program

  • Debugging
  • HCP Packer
Type '/' to Search

»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:

  • -except=foo,bar,baz - Run all the builds and post-processors except those with the given comma-separated names. In legacy JSON templates, build names default to the types of their builders (e.g. docker or amazon-ebs or virtualbox-iso), unless a specific name attribute is specified within the configuration. In HCL2 templates, the "name" is the source block's "name" label, unless an in-build source definition adds the "name" configuration option. Any post-processor following a skipped post-processor will not run. Because post-processors can be nested in arrays a different post-processor chain can still run. A post-processor with an empty name will be ignored.
  • -only=foo,bar,baz - Only run the builds with the given comma-separated names. In legacy JSON templates, build names default to the types of their builders (e.g. docker or amazon-ebs or virtualbox-iso), unless a specific name attribute is specified within the configuration. In HCL2 templates, the "name" is the source block's "name" label, unless an in-build source definition adds the "name" configuration option.

They can also be seen in a template to run or skip provisioners and/or post-processors for a specific source:

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
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).

github logoEdit this page
IntroGuidesDocsCommunityPrivacySecurityPress KitConsent Manager