简体   繁体   中英

Store env variables in Vagrant from Packer

I'm trying to export PATH=$PATH:$GOPATH/bin for go in the Vagrant box that my Packer built so when I vagrant ssh into the box I can call whatever binary I have in there.

The environment_vars = ["GOPATH=$HOME/go"] lets go install packages but it doesn't stay for the box. I've tried export GOPATH=$HOME/go and export PATH=$PATH:$GOPATH/bin in the scripts/dependencies.sh but that didn't work.

Anybody know how to store env variables to a Vagrant box from a Packer build?

build {
  name        = "test-vagrant"
  description = "Testing VM"

  sources = [
    "source.vagrant.alpine"
  ]

  provisioner "shell" {
    environment_vars = ["GOPATH=$HOME/go"]
    scripts = [
      "scripts/dependencies.sh",
      ////
    ]
    execute_command = "echo 'vagrant' | sudo -S -E sh -c '{{ .Vars }} {{ .Path }}'"
  }
}

#!/bin/sh
set -eu

apk update && \
apk add \
  
////
  
go \
unzip

go install github.com/OJ/gobuster/v3@latest

Well I couldn't figured out exporting the variables via Packer's shell but I do have a solution.

https://www.packer.io/docs/provisioners/file

provisioner "file" {
  source = "upload/go.sh"
  destination = "/tmp/go.sh"
}
provisioner "shell" {
  inline = [
  "sudo mv /tmp/go.sh /etc/profile.d/"]
}

A side note this wasn't working at first (and Packer didn't throw any info/errors) this was because of the "folder" packer_alpine in .vagrant.d/boxes I deleted it which fixed the uploading and some other issues I had.

So I found another fix to something else, haha.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM