• HashiCorp Developer

  • HashiCorp Cloud Platform
  • Terraform
  • Packer
  • Consul
  • Vault
  • Boundary
  • Nomad
  • Waypoint
  • Vagrant
Packer
  • Install
  • Tutorials
  • Documentation
  • Guides
  • Plugins
  • Try Cloud(opens in new tab)
  • Sign up
Packer Home

Documentation

Skip to main contentOverview
  • HCP Packer

  • Terminology
    • Overview
      • Overview
        • Overview
          • abspath
          • basename
          • dirname
          • file
          • fileexists
          • fileset
          • pathexpand
          • templatefile
      • Variables
      • Locals
      • Contextual Variables
      • Data Sources
      • Path Variables
      • Syntax
      • Only Except
      • Expressions
      • JSON Syntax


  • Installing Packer
  • Configuring Packer

  • Integration Program

  • Debugging

  • Resources

  • Tutorial Library
  • Community Forum
    (opens in new tab)
  • Support
    (opens in new tab)
  • GitHub
    (opens in new tab)
  1. Developer
  2. Packer
  3. Documentation
  4. Templates
  5. HCL Templates
  6. Functions
  7. Filesystem Functions
  8. pathexpand
  • Packer
  • v1.7.x
  • v1.6.x
  • v1.5.x

ยปpathexpand Function

pathexpand takes a filesystem path that might begin with a ~ segment, and if so it replaces that segment with the current user's home directory path.

This function works only with the path string and does not access the filesystem itself. It is therefore unable to take into account filesystem features such as symlinks.

If the leading segment in the path is not ~ then the given path is returned unmodified.

Using this function in resource arguments will cause spurious diffs if the same configuration is run by multiple users with different home directory paths, or used on different host operating systems. We recommend using this function only for transient values, such as in connection and provisioner blocks to locate SSH keys, etc.

The rules for determining the "home directory" for the current user vary depending on host operating system.

For Unix systems, the following sources are consulted, in order of preference:

  • The HOME environment variable.
  • The result of running getent passwd followed by the Packer process uid.
  • The result of running cd && pwd in sh.

For Windows systems, there is not really the concept of a home directory in the same sense as on Unix, but the following sources are consulted in order of preference:

  • The HOME environment variable.
  • The HOMEDRIVE and HOMEPATH environment variables, if both are set.
  • The USERPROFILE environment variable.

The exact rules employed for each operating system may change in future releases of Packer.

Examples

> pathexpand("~/.ssh/id_rsa")
/home/steve/.ssh/id_rsa
> pathexpand("/etc/resolv.conf")
/etc/resolv.conf
Edit this page on GitHub

On this page

  1. pathexpand Function
  2. Examples
Give Feedback(opens in new tab)
  • Certifications
  • System Status
  • Terms of Use
  • Security
  • Privacy
  • Trademark Policy
  • Trade Controls
  • Give Feedback(opens in new tab)