简体   繁体   English

将环境变量存储在 Packer 的 Vagrant 中

[英]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. 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. environment_vars = ["GOPATH=$HOME/go"]go安装包,但它不会留在盒子里。 I've tried export GOPATH=$HOME/go and export PATH=$PATH:$GOPATH/bin in the scripts/dependencies.sh but that didn't work.我已经在scripts/dependencies.sh中尝试export GOPATH=$HOME/goexport PATH=$PATH:$GOPATH/bin ,但这没有用。

Anybody know how to store env variables to a Vagrant box from a Packer build?有人知道如何将环境变量从 Packer 版本存储到 Vagrant 盒子吗?

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.好吧,我想不出通过 Packer 的 shell 导出变量,但我确实有一个解决方案。

https://www.packer.io/docs/provisioners/file 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.附带说明,这最初不起作用(并且 Packer 没有抛出任何信息/错误)这是因为.vagrant.d/boxes中的“文件夹” packer_alpine我删除了它,它修复了上传和其他一些问题有。

So I found another fix to something else, haha.所以我找到了另一个解决办法,哈哈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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