github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/website/source/docs/provisioners/shell-local.html.md (about)

     1  ---
     2  description: |
     3      shell-local will run a shell script of your choosing on the machine where Packer
     4      is being run - in other words, shell-local will run the shell script on your
     5      build server, or your desktop, etc., rather than the remote/guest machine being
     6      provisioned by Packer.
     7  layout: docs
     8  page_title: 'Shell (Local) - Provisioners'
     9  sidebar_current: 'docs-provisioners-shell-local'
    10  ---
    11  
    12  # Local Shell Provisioner
    13  
    14  Type: `shell-local`
    15  
    16  shell-local will run a shell script of your choosing on the machine where Packer
    17  is being run - in other words, shell-local will run the shell script on your
    18  build server, or your desktop, etc., rather than the remote/guest machine being
    19  provisioned by Packer.
    20  
    21  The [remote shell](/docs/provisioners/shell.html) provisioner executes
    22  shell scripts on a remote machine.
    23  
    24  ## Basic Example
    25  
    26  The example below is fully functional.
    27  
    28  ``` json
    29  {
    30    "type": "shell-local",
    31    "command": "echo foo"
    32  }
    33  ```
    34  
    35  ## Configuration Reference
    36  
    37  The reference of available configuration options is listed below. The only
    38  required element is "command".
    39  
    40  Exactly *one* of the following is required:
    41  
    42  -   `command` (string) - This is a single command to execute. It will be written
    43      to a temporary file and run using the `execute_command` call below.
    44      If you are building a windows vm on AWS, Azure or Google Compute and would
    45      like to access the generated password that Packer uses to connect to the
    46      instance via WinRM, you can use the template variable `{{.WinRMPassword}}`
    47      to set this as an environment variable.
    48  
    49  -   `inline` (array of strings) - This is an array of commands to execute. The
    50      commands are concatenated by newlines and turned into a single file, so they
    51      are all executed within the same context. This allows you to change
    52      directories in one command and use something in the directory in the next
    53      and so on. Inline scripts are the easiest way to pull off simple tasks
    54      within the machine.
    55  
    56  -   `script` (string) - The path to a script to execute. This path can be
    57      absolute or relative. If it is relative, it is relative to the working
    58      directory when Packer is executed.
    59  
    60  -   `scripts` (array of strings) - An array of scripts to execute. The scripts
    61      will be executed in the order specified. Each script is executed in
    62      isolation, so state such as variables from one script won't carry on to the
    63      next.
    64  
    65  Optional parameters:
    66  
    67  -   `environment_vars` (array of strings) - An array of key/value pairs to
    68      inject prior to the `execute_command`. The format should be `key=value`.
    69      Packer injects some environmental variables by default into the environment,
    70      as well, which are covered in the section below. If you are building a
    71      windows vm on AWS, Azure or Google Compute and would like to access the
    72      generated password that Packer uses to connect to the instance via WinRM,
    73      you can use the template variable `{{.WinRMPassword}}` to set this as an
    74      environment variable. For example:
    75      `"environment_vars": "WINRMPASS={{.WinRMPassword}}"`
    76  
    77  -   `execute_command` (array of strings) - The command used to execute the script.
    78      By default this is `["/bin/sh", "-c", "{{.Vars}}, "{{.Script}}"]`
    79      on unix and `["cmd", "/c", "{{.Vars}}", "{{.Script}}"]` on windows.
    80      This is treated as a [template engine](/docs/templates/engine.html).
    81      There are two available variables: `Script`, which is the path to the script
    82      to run, and `Vars`, which is the list of `environment_vars`, if configured.
    83  
    84      If you choose to set this option, make sure that the first element in the
    85      array is the shell program you want to use (for example, "sh"), and a later
    86      element in the array must be `{{.Script}}`.
    87  
    88      This option provides you a great deal of flexibility. You may choose to
    89      provide your own shell program, for example "/usr/local/bin/zsh" or even
    90      "powershell.exe". However, with great power comes great responsibility -
    91      these commands are not officially supported and things like environment
    92      variables may not work if you use a different shell than the default.
    93  
    94      For backwards compatibility, you may also use {{.Command}}, but it is
    95      decoded the same way as {{.Script}}. We recommend using {{.Script}} for the
    96      sake of clarity, as even when you set only a single `command` to run,
    97      Packer writes it to a temporary file and then runs it as a script.
    98  
    99      If you are building a windows vm on AWS, Azure or Google Compute and would
   100      like to access the generated password that Packer uses to connect to the
   101      instance via WinRM, you can use the template variable `{{.WinRMPassword}}`
   102      to set this as an environment variable.
   103  
   104  -   `inline_shebang` (string) - The
   105      [shebang](http://en.wikipedia.org/wiki/Shebang_%28Unix%29) value to use when
   106      running commands specified by `inline`. By default, this is `/bin/sh -e`. If
   107      you're not using `inline`, then this configuration has no effect.
   108      **Important:** If you customize this, be sure to include something like the
   109      `-e` flag, otherwise individual steps failing won't fail the provisioner.
   110  
   111  -  `use_linux_pathing` (bool) - This is only relevant to windows hosts. If you
   112     are running Packer in a Windows environment with the Windows Subsystem for
   113     Linux feature enabled, and would like to invoke a bash script rather than
   114     invoking a Cmd script, you'll need to set this flag to true; it tells Packer
   115     to use the linux subsystem path for your script rather than the Windows path.
   116     (e.g. /mnt/c/path/to/your/file instead of C:/path/to/your/file). Please see
   117     the example below for more guidance on how to use this feature. If you are
   118     not on a Windows host, or you do not intend to use the shell-local
   119     provisioner to run a bash script, please ignore this option.
   120  
   121  ## Execute Command
   122  
   123  To many new users, the `execute_command` is puzzling. However, it provides an
   124  important function: customization of how the command is executed. The most
   125  common use case for this is dealing with **sudo password prompts**. You may also
   126  need to customize this if you use a non-POSIX shell, such as `tcsh` on FreeBSD.
   127  
   128  ### The Windows Linux Subsystem
   129  
   130  The shell-local provisioner was designed with the idea of allowing you to run
   131  commands in your local operating system's native shell. For Windows, we've
   132  assumed in our defaults that this is Cmd. However, it is possible to run a
   133  bash script as part of the Windows Linux Subsystem from the shell-local
   134  provisioner, by modifying the `execute_command` and the `use_linux_pathing`
   135  options in the provisioner config.
   136  
   137  The example below is a fully functional test config.
   138  
   139  One limitation of this offering is that "inline" and "command" options are not
   140  available to you; please limit yourself to using the "script" or "scripts"
   141  options instead.
   142  
   143  Please note that the WSL is a beta feature, and this tool is not guaranteed to
   144  work as you expect it to.
   145  
   146  ```
   147  {
   148      "builders": [
   149        {
   150          "type":         "null",
   151          "communicator": "none"
   152        }
   153      ],
   154      "provisioners": [
   155        {
   156            "type": "shell-local",
   157            "environment_vars": ["PROVISIONERTEST=ProvisionerTest1"],
   158            "execute_command": ["bash", "-c", "{{.Vars}} {{.Script}}"],
   159            "use_linux_pathing": true,
   160            "scripts": ["C:/Users/me/scripts/example_bash.sh"]
   161        },
   162        {
   163            "type": "shell-local",
   164            "environment_vars": ["PROVISIONERTEST=ProvisionerTest2"],
   165            "execute_command": ["bash", "-c", "{{.Vars}} {{.Script}}"],
   166            "use_linux_pathing": true,
   167            "script": "C:/Users/me/scripts/example_bash.sh"
   168        }
   169    ]
   170  }
   171  ```
   172  
   173  ## Default Environmental Variables
   174  
   175  In addition to being able to specify custom environmental variables using the
   176  `environment_vars` configuration, the provisioner automatically defines certain
   177  commonly useful environmental variables:
   178  
   179  -   `PACKER_BUILD_NAME` is set to the name of the build that Packer is running.
   180      This is most useful when Packer is making multiple builds and you want to
   181      distinguish them slightly from a common provisioning script.
   182  
   183  -   `PACKER_BUILDER_TYPE` is the type of the builder that was used to create the
   184      machine that the script is running on. This is useful if you want to run
   185      only certain parts of the script on systems built with certain builders.
   186  
   187  ## Safely Writing A Script
   188  
   189  Whether you use the `inline` option, or pass it a direct `script` or `scripts`,
   190  it is important to understand a few things about how the shell-local
   191  provisioner works to run it safely and easily. This understanding will save
   192  you much time in the process.
   193  
   194  ### Once Per Builder
   195  
   196  The `shell-local` script(s) you pass are run once per builder. That means that
   197  if you have an `amazon-ebs` builder and a `docker` builder, your script will be
   198  run twice. If you have 3 builders, it will run 3 times, once for each builder.
   199  
   200  ### Always Exit Intentionally
   201  
   202  If any provisioner fails, the `packer build` stops and all interim artifacts
   203  are cleaned up.
   204  
   205  For a shell script, that means the script **must** exit with a zero code. You
   206  *must* be extra careful to `exit 0` when necessary.
   207  
   208  
   209  ## Usage Examples:
   210  
   211  Example of running a .cmd file on windows:
   212  
   213  ```
   214        {
   215            "type": "shell-local",
   216            "environment_vars": ["SHELLLOCALTEST=ShellTest1"],
   217            "scripts": ["./scripts/test_cmd.cmd"]
   218        },
   219  ```
   220  
   221  Contents of "test_cmd.cmd":
   222  
   223  ```
   224  echo %SHELLLOCALTEST%
   225  ```
   226  
   227  Example of running an inline command on windows:
   228  Required customization: tempfile_extension
   229  
   230  ```
   231        {
   232            "type": "shell-local",
   233            "environment_vars": ["SHELLLOCALTEST=ShellTest2"],
   234            "tempfile_extension": ".cmd",
   235            "inline": ["echo %SHELLLOCALTEST%"]
   236        },
   237  ```
   238  
   239  Example of running a bash command on windows using WSL:
   240  Required customizations: use_linux_pathing and execute_command
   241  
   242  ```
   243        {
   244            "type": "shell-local",
   245            "environment_vars": ["SHELLLOCALTEST=ShellTest3"],
   246            "execute_command": ["bash", "-c", "{{.Vars}} {{.Script}}"],
   247            "use_linux_pathing": true,
   248            "script": "./scripts/example_bash.sh"
   249        }
   250  ```
   251  
   252  Contents of "example_bash.sh":
   253  
   254  ```
   255  #!/bin/bash
   256  echo $SHELLLOCALTEST
   257  ```
   258  
   259  Example of running a powershell script on windows:
   260  Required customizations: env_var_format and execute_command
   261  
   262  ```
   263  
   264        {
   265            "type": "shell-local",
   266            "environment_vars": ["SHELLLOCALTEST=ShellTest4"],
   267            "execute_command": ["powershell.exe", "{{.Vars}} {{.Script}}"],
   268            "env_var_format": "$env:%s=\"%s\"; ",
   269        }
   270  ```
   271  
   272  Example of running a powershell script on windows as "inline":
   273  Required customizations: env_var_format, tempfile_extension, and execute_command
   274  
   275  ```
   276        {
   277            "type": "shell-local",
   278            "tempfile_extension": ".ps1",
   279            "environment_vars": ["SHELLLOCALTEST=ShellTest5"],
   280            "execute_command": ["powershell.exe", "{{.Vars}} {{.Script}}"],
   281            "env_var_format": "$env:%s=\"%s\"; ",
   282            "inline": ["write-output $env:SHELLLOCALTEST"]
   283        }
   284  ```
   285  
   286  
   287  Example of running a bash script on linux:
   288  
   289  ```
   290        {
   291            "type": "shell-local",
   292            "environment_vars": ["PROVISIONERTEST=ProvisionerTest1"],
   293            "scripts": ["./scripts/dummy_bash.sh"]
   294        }
   295  ```
   296  
   297  Example of running a bash "inline" on linux:
   298  
   299  ```
   300        {
   301            "type": "shell-local",
   302            "environment_vars": ["PROVISIONERTEST=ProvisionerTest2"],
   303            "inline": ["echo hello",
   304                       "echo $PROVISIONERTEST"]
   305        }
   306  ```
   307