github.com/hashicorp/packer@v1.14.3/website/content/docs/commands/console.mdx (about)

     1  ---
     2  description: |
     3    The `packer console` command starts an interactive console, letting you experiment with Packer variable interpolations.
     4  page_title: packer console command reference
     5  ---
     6  
     7  # `packer console` command reference
     8  
     9  The `packer console` command allows you to experiment with Packer variable
    10  interpolations. You may access variables in the Packer config you called the
    11  console with, or provide variables when you call console using the -var or
    12  -var-file command line options.
    13  
    14  ~> **Note:** `console` is available from version 1.4.2 and above.
    15  
    16  ~> **Note:** For HCL2 `console` is available from version 1.6.0 and above, use
    17  `packer console --config-type=hcl2` to try it without a config file. Go
    18  templating ( or `{{..}}` calls ) will not work in HCL2 mode.
    19  
    20  Type in the interpolation to test and hit `<enter>` to see the result.
    21  
    22  To exit the console, type "exit" and hit `<enter>`, or use Control-C.
    23  
    24  ```shell-session
    25  $ packer console my_template.json
    26  ```
    27  
    28  The full list of options that the console command will accept is visible in the
    29  help output, which can be seen via `packer console -h`.
    30  
    31  ## Options
    32  
    33  - `-var` - Set a variable in your Packer template. This option can be used
    34    multiple times. This is useful for setting version numbers for your build.
    35    example: `-var "myvar=asdf"`
    36  
    37  - `-var-file` - Set template variables from a file.
    38    example: `-var-file myvars.json`
    39  
    40  ## REPL commands
    41  
    42  - `help` - displays help text for Packer console.
    43  
    44  - `exit` - exits the console
    45  
    46  - `variables` - prints a list of all variables read into the console from the
    47    `-var` option, `-var-files` option, and template.
    48  
    49  ## Usage Examples - repl session ( JSON )
    50  
    51  Let's say you launch a console using a Packer template `example_template.json`:
    52  
    53  ```shell-session
    54  $ packer console example_template.json
    55  ```
    56  
    57  You'll be dropped into a prompt that allows you to enter template functions and
    58  see how they're evaluated; for example, if the variable `myvar` is defined in
    59  your example_template's variable section:
    60  
    61  ```json
    62  "variables":{
    63  	"myvar": "asdfasdf"
    64  },
    65  ...
    66  ```
    67  
    68  and you enter `` {{user `myvar`}} `` in the Packer console, you'll see the value of
    69  myvar:
    70  
    71  ```shell-session
    72  > {{user `myvar`}}
    73  asdfasdf
    74  ```
    75  
    76  From there you can test more complicated interpolations:
    77  
    78  ```shell-session
    79  > {{user `myvar`}}-{{timestamp}}
    80  asdfasdf-1559854396
    81  ```
    82  
    83  And when you're done using the console, just type "exit" or CTRL-C
    84  
    85  ```shell-session
    86  > exit
    87  $
    88  ```
    89  
    90  If you'd like to provide a variable or variable files, you'd do this:
    91  
    92  ```shell-session
    93  $ packer console -var "myvar=fdsafdsa" -var-file myvars.json example_template.json
    94  ```
    95  
    96  If you don't have specific variables or var files you want to test, and just
    97  want to experiment with a particular template engine, you can do so by simply
    98  calling `packer console` without a template file.
    99  
   100  ## Usage Examples - piped commands ( JSON )
   101  
   102  If you'd like to just see a specific single interpolation without launching
   103  the REPL, you can do so by echoing and piping the string into the console
   104  command:
   105  
   106  ```shell-session
   107  $ echo {{timestamp}} | packer console
   108  1559855090
   109  ```
   110  
   111  ## Usage Examples - repl session ( HCL2 )
   112  
   113  ~> **Note:** For HCL2 `console` is available from version 1.6.0 and above, use
   114  `packer console --config-type=hcl2` to try it without a config file. Go
   115  templating ( or `{{..}}` calls ) will not work in HCL2 mode.
   116  
   117  Without a config file, `packer console` can be used to experiment with the
   118  expression syntax and [built-in functions](/packer/docs/templates/hcl_templates/functions).
   119  
   120  ### Starting
   121  
   122  To start a session on a folder containing HCL2 config files, run:
   123  
   124  ```shell-session
   125  packer console folder/
   126  ```
   127  
   128  Because `folder/` is a folder Packer will start in HCL2 mode, you can also
   129  directly pass an HCL2 formatted config file:
   130  
   131  ```shell-session
   132  packer console file.pkr.hcl
   133  ```
   134  
   135  Because the file is suffixed with `.pkr.hcl` Packer will start in HCL2 mode.
   136  
   137  When you just want to play around without a config file you can set the
   138  `--config-type=hcl2` option and Packer will start in HCL2 mode:
   139  
   140  ```shell-session
   141  packer console --config-type=hcl2
   142  ```
   143  
   144  ### Scripting
   145  
   146  The `packer console` command can be used in non-interactive scripts by piping
   147  newline-separated commands to it. Only the output from the final command is
   148  printed unless an error occurs earlier.
   149  
   150  For example:
   151  
   152  ```shell-session
   153  $ echo "1 + 5" | packer console
   154  6
   155  ```