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

     1  ---
     2  description: |
     3    The `file` Packer provisioner uploads files to machines built by Packer. Learn how to use the `file` provisioner and about the Packer provisioner workflow.
     4  page_title: file provisioner reference
     5  ---
     6  
     7  <BadgesHeader>
     8    <PluginBadge type="official" />
     9  </BadgesHeader>
    10  
    11  # `file` provisioner
    12  
    13  The `file` Packer provisioner uploads files to machines built by Packer. We recommend implementing the following workflow:
    14  
    15  1. Use the `file` provisioner to upload files
    16  1. Use the [`shell` provisioner](/packer/docs/provisioners/shell) to move the files into the proper place, set permissions, and perform other tasks.
    17  
    18  You can only upload files to locations that the provisioning user
    19  (generally not root) has permission to access. Creating files in /tmp and
    20  using a shell provisioner to move them into the final location is the only
    21  way to upload files to root owned locations.
    22  
    23  The file provisioner can upload both single files and complete directories.
    24  
    25  ## Basic Example
    26  
    27  <Tabs>
    28  <Tab heading="HCL2">
    29  
    30  ```hcl
    31  provisioner "file" {
    32    source = "app.tar.gz"
    33    destination = "/tmp/app.tar.gz"
    34  }
    35  ```
    36  
    37  </Tab>
    38  <Tab heading="JSON">
    39  
    40  ```json
    41  {
    42    "type": "file",
    43    "source": "app.tar.gz",
    44    "destination": "/tmp/app.tar.gz"
    45  }
    46  ```
    47  
    48  </Tab>
    49  </Tabs>
    50  
    51  ## Configuration Reference
    52  
    53  The available configuration options are listed below.
    54  
    55  ## Configuration Reference
    56  
    57  Required Parameters:
    58  
    59  @include 'provisioner/file/Config-required.mdx'
    60  
    61  Optional Parameters:
    62  
    63  @include '/provisioner/file/Config-not-required.mdx'
    64  
    65  @include 'provisioners/common-config.mdx'
    66  
    67  ## Directory Uploads
    68  
    69  The file provisioner is also able to upload a complete directory to the remote
    70  machine. When uploading a directory, there are a few important things you
    71  should know.
    72  
    73  First, the destination directory must already exist. If you need to create it,
    74  use a shell provisioner just prior to the file provisioner in order to create
    75  the directory. If the destination directory does not exist, the file
    76  provisioner may succeed, but it will have undefined results.
    77  
    78  Next, the existence of a trailing slash on the source path will determine
    79  whether the directory name will be embedded within the destination, or whether
    80  the destination will be created. An example explains this best:
    81  
    82  If the source is `/foo` (no trailing slash), and the destination is `/tmp`,
    83  then the contents of `/foo` on the local machine will be uploaded to `/tmp/foo`
    84  on the remote machine. The `foo` directory on the remote machine will be
    85  created by Packer.
    86  
    87  If the source, however, is `/foo/` (a trailing slash is present), and the
    88  destination is `/tmp`, then the contents of `/foo` will be uploaded into `/tmp`
    89  directly.
    90  
    91  This behavior was adopted from the standard behavior of rsync. Note that under
    92  the covers, rsync may or may not be used.
    93  
    94  **Note:** Packer does not infer behaviors based on the guest operating system,
    95  as this would add unnecessary complexity and reduce maintainability. To ensure
    96  consistent behavior across all platforms, use a trailing forward slash when
    97  specifying directories, even when working with Windows guests.
    98  
    99  ## Uploading files that don't exist before Packer starts
   100  
   101  In general, local files used as the source **must** exist before Packer is run.
   102  This is great for catching typos and ensuring that once a build is started,
   103  that it will succeed. However, this also means that you can't generate a file
   104  during your build and then upload it using the file provisioner later. A
   105  convenient workaround is to upload a directory instead of a file. The directory
   106  still must exist, but its contents don't. You can write your generated file to
   107  the directory during the Packer run, and have it be uploaded later.
   108  
   109  ## Symbolic link uploads
   110  
   111  The behavior when uploading symbolic links depends on the communicator. The
   112  Docker communicator will preserve symlinks, but all other communicators will
   113  treat local symlinks as regular files. If you wish to preserve symlinks when
   114  uploading, it's recommended that you use `tar`. Below is an example of what
   115  that might look like:
   116  
   117  ```shell-session
   118  $ ls -l files
   119  total 16
   120  drwxr-xr-x  3 mwhooker  staff  102 Jan 27 17:10 a
   121  lrwxr-xr-x  1 mwhooker  staff    1 Jan 27 17:10 b -> a
   122  -rw-r--r--  1 mwhooker  staff    0 Jan 27 17:10 file1
   123  lrwxr-xr-x  1 mwhooker  staff    5 Jan 27 17:10 file1link -> file1
   124  $ ls -l toupload
   125  total 0
   126  -rw-r--r--  1 mwhooker  staff    0 Jan 27 17:10 files.tar
   127  ```
   128  
   129  <Tabs>
   130  <Tab heading="JSON">
   131  
   132  ```json
   133  {
   134    "provisioners": [
   135      {
   136        "type": "shell-local",
   137        "command": "tar cf toupload/files.tar files"
   138      },
   139      {
   140        "destination": "/tmp/",
   141        "source": "./toupload",
   142        "type": "file"
   143      },
   144      {
   145        "inline": [
   146          "cd /tmp && tar xf toupload/files.tar",
   147          "rm toupload/files.tar"
   148        ],
   149        "type": "shell"
   150      }
   151    ]
   152  }
   153  ```
   154  
   155  </Tab>
   156  <Tab heading="HCL2">
   157  
   158  ```hcl
   159  build {
   160    sources = [
   161      "source.docker.example"
   162    ]
   163  
   164    provisioner "shell-local" {
   165      command = "tar cf toupload/files.tar files"
   166    }
   167    provisioner "file" {
   168      destination = "/tmp/"
   169      source      = "./toupload"
   170    }
   171    provisioner "shell" {
   172      inline = [
   173        "cd /tmp && tar xf toupload/files.tar",
   174        "rm toupload/files.tar"
   175      ]
   176    }
   177  }
   178  ```
   179  
   180  </Tab>
   181  </Tabs>
   182  
   183  ## Slowness when transferring large files over WinRM.
   184  
   185  Because of the way our WinRM transfers works, it can take a very long time to
   186  upload and download even moderately sized files. If you're experiencing slowness
   187  using the file provisioner on Windows, it's suggested that you set up an SSH
   188  server and use the [ssh communicator](/packer/docs/communicators/ssh). If you only want
   189  to transfer files to your guest, and if your builder supports it, you may also
   190  use the `http_directory` or `http_content` directives. This will cause that
   191  directory to be available to the guest over HTTP, and set the environment
   192  variable `PACKER_HTTP_ADDR` to the address.