Packer: Packer ansible provisioner and home directory

Created on 18 Jul 2015  ·  6Comments  ·  Source: hashicorp/packer

Having trouble specifying playbook_dir as a path relative to ~ or $HOME in my packer.json Ansible local provisioner.

  "provisioners": [
    {
      "type": "shell",
      "script": "bootstrap.sh"
    },
    {
      "type": "ansible-local",
      "playbook_file": "registry.yml",
      "playbook_dir": "~/.ansible/roles/rococo-ansible"
    }
  ]

Produces

~/P/r/r/packer ❯❯❯ stat ~/.ansible/roles/rococo-ansible                       ⏎
  File: ‘/home/ross/.ansible/roles/rococo-ansible’
  Size: 4096        Blocks: 8          IO Block: 4096   directory
Device: 803h/2051d  Inode: 2459        Links: 5
Access: (0775/drwxrwxr-x)  Uid: ( 1000/    ross)   Gid: ( 1000/    ross)
Access: 2015-07-18 14:50:53.008967078 +1000
Modify: 2015-07-18 14:50:05.979517131 +1000
Change: 2015-07-18 14:50:05.979517131 +1000
 Birth: -
~/P/r/r/packer ❯❯❯ ~/do.sh packer build packer.json
digitalocean output will be in this color.

1 error(s) occurred:

* playbook_dir: ~/.ansible/roles/rococo-ansible/ is invalid: stat ~/.ansible/roles/rococo-ansible/: no such file or directory
core enhancement wontfix

Most helpful comment

The to go is to use:

{
  "variables": {
     "home": "{{ env `HOME` }}"
  },
  "provisioners": [ {
    "type": "ansible-local",
    "playbook_file": "registry.yml",
    "playbook_dir": "{{ user `home` }}/.ansible/roles/rococo-ansible"
  } ]

@cbednarski I'm voting close as _won't fix_.

All 6 comments

I don't think we do shell expansion on bashisms like ~. Packer assumes that resources are rooted under the same path as the template file so provisioning scripts and packer template get versioned together. This should work if you use a relative path or symlink, though.

+1 for supporting either ~ expansion or environment variables in provisioner values. My particular use case is for the file provisioner, and grabbing the SSH public key out of ~/.ssh/id_rsa.pub and putting it into the image. A symlink will do as a temporary workaround.

The to go is to use:

{
  "variables": {
     "home": "{{ env `HOME` }}"
  },
  "provisioners": [ {
    "type": "ansible-local",
    "playbook_file": "registry.yml",
    "playbook_dir": "{{ user `home` }}/.ansible/roles/rococo-ansible"
  } ]

@cbednarski I'm voting close as _won't fix_.

Ahh, excellent! Thanks, @rickard-von-essen

Please note the ENV statement should be lowercase i.e. env: https://www.packer.io/docs/templates/user-variables.html

@oryband thanks! updated the example.

Was this page helpful?
0 / 5 - 0 ratings