• 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
    • Http
    • 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

»console Command

The packer console command allows you to experiment with Packer variable interpolations. You may access variables in the Packer config you called the console with, or provide variables when you call console using the -var or -var-file command line options.

Note: console is available from version 1.4.2 and above.

Note: For HCL2 console is available from version 1.6.0 and above, use packer console --config-type=hcl2 to try it without a config file. Go templating ( or {{..}} calls ) will not work in HCL2 mode.

Type in the interpolation to test and hit <enter> to see the result.

To exit the console, type "exit" and hit <enter>, or use Control-C.

$ packer console my_template.json
$ packer console my_template.json

The full list of options that the console command will accept is visible in the help output, which can be seen via packer console -h.

»Options

  • -var - Set a variable in your Packer template. This option can be used multiple times. This is useful for setting version numbers for your build. example: -var "myvar=asdf"

  • -var-file - Set template variables from a file. example: -var-file myvars.json

»REPL commands

  • help - displays help text for Packer console.

  • exit - exits the console

  • variables - prints a list of all variables read into the console from the -var option, -var-files option, and template.

»Usage Examples - repl session ( JSON )

Let's say you launch a console using a Packer template example_template.json:

$ packer console example_template.json
$ packer console example_template.json

You'll be dropped into a prompt that allows you to enter template functions and see how they're evaluated; for example, if the variable myvar is defined in your example_template's variable section:

"variables":{
    "myvar": "asdfasdf"
},
...
"variables":{
    "myvar": "asdfasdf"
},
...

and you enter {{user `myvar`}} in the Packer console, you'll see the value of myvar:

> {{user `myvar`}}
asdfasdf
> {{user `myvar`}}
asdfasdf

From there you can test more complicated interpolations:

> {{user `myvar`}}-{{timestamp}}
asdfasdf-1559854396
> {{user `myvar`}}-{{timestamp}}
asdfasdf-1559854396

And when you're done using the console, just type "exit" or CTRL-C

> exit
$
> exit
$

If you'd like to provide a variable or variable files, you'd do this:

$ packer console -var "myvar=fdsafdsa" -var-file myvars.json example_template.json
$ packer console -var "myvar=fdsafdsa" -var-file myvars.json example_template.json

If you don't have specific variables or var files you want to test, and just want to experiment with a particular template engine, you can do so by simply calling packer console without a template file.

»Usage Examples - piped commands ( JSON )

If you'd like to just see a specific single interpolation without launching the REPL, you can do so by echoing and piping the string into the console command:

$ echo {{timestamp}} | packer console
1559855090
$ echo {{timestamp}} | packer console
1559855090

»Usage Examples - repl session ( HCL2 )

Note: For HCL2 console is available from version 1.6.0 and above, use packer console --config-type=hcl2 to try it without a config file. Go templating ( or {{..}} calls ) will not work in HCL2 mode.

Without a config file, packer console can be used to experiment with the expression syntax and built-in functions.

»Starting

To start a session on a folder containing HCL2 config files, run:

packer console folder/
packer console folder/

Because folder/ is a folder Packer will start in HCL2 mode, you can also directly pass an HCL2 formatted config file:

packer console file.pkr.hcl
packer console file.pkr.hcl

Because the file is suffixed with .pkr.hcl Packer will start in HCL2 mode.

When you just want to play around without a config file you can set the --config-type=hcl2 option and Packer will start in HCL2 mode:

packer console --config-type=hcl2
packer console --config-type=hcl2

»Scripting

The packer console command can be used in non-interactive scripts by piping newline-separated commands to it. Only the output from the final command is printed unless an error occurs earlier.

For example:

$ echo "1 + 5" | packer console
6
$ echo "1 + 5" | packer console
6
github logoEdit this page
IntroGuidesDocsCommunityPrivacySecurityPress KitConsent Manager