-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add environment variables to guest with simple vagrantfile command #7015
Comments
Hi @poshest There are a number of ways you can set environment variables on the host. First, you can use a shell provisioner with an |
Hey @sethvargo, thanks for your help! The "env hash" documententation says "List of key-value pairs to pass in as environment variables to the script..." I understand that this will not add environment variables to every new session upon vagrant up, but rather simply pass env vars "to the script" for use only while it's provisioning. Please correct me if I'm wrong. Also, the second method you mentioned was similar to the hack I already linked in my question. In the meantime, I've abandoned altogether using env variables as config for these reasons
If Vagrant would add the functionality I'm calling for above, and in addition remedy the first two dot points, I think it would be very welcome by many developers. |
Hey @poshest, I've just tried to do the same. And based on this SO question I came up with the solution:
Because of |
I took @starikovs solution and (in my mind) simplified it a little. This example does the same, but with fewer steps. It also shows how to read environment variables from the host to pass to the guest. $set_environment_variables = <<SCRIPT
tee "/etc/profile.d/myvars.sh" > "/dev/null" <<EOF
# Ansible environment variables.
export ANSIBLE_STDOUT_CALLBACK=debug
# AWS environment variables.
export AWS_DEFAULT_REGION=#{ENV['AWS_DEFAULT_REGION']}
export AWS_ACCESS_KEY_ID=#{ENV['AWS_ACCESS_KEY_ID']}
export AWS_SECRET_ACCESS_KEY=#{ENV['AWS_SECRET_ACCESS_KEY']}
EOF
SCRIPT
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.provider "hyperv" do |machine|
machine.vmname = "centos7"
end
config.vm.provision "shell", inline: $set_environment_variables, run: "always"
config.vm.provision "ansible_local" do |ansible|
ansible.playbook = "playbook.yml"
ansible.verbose = true
ansible.install_mode = "pip"
ansible.version = "2.2.1.0"
end
end |
Where file (on Host)
As result:
|
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
I want to put something simple in the
vagrantfile
, likewhich creates an environment variables on the guest machine based on their current values in the
vagrantfile
uponvagrant up
and not during provisioning.There are provisioning hacks like this, but I can't get it working on my Windows 7 (host), precise32 (guest), maybe because of this? And I use PuTTY to ssh in, not
vagrant ssh
so I don't thinkconfig.ssh.forward_env
is possible in my case either.This seems to be a very common use case for anyone doing a 12 factor app. Is there a chance you could provide this functionality?
Or if there's a better pattern to achieve this, please explain! :)
The text was updated successfully, but these errors were encountered: