How do I include variables in my VagrantFile?
I use the approach of https://puphpet.com, I create a file config.yaml in the same directory of the Vagrantfile and...
In my Vagrantfile:
# encoding: utf-8
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'yaml'
current_dir = File.dirname(File.expand_path(__FILE__))
configs = YAML.load_file("#{current_dir}/config.yaml")
vagrant_config = configs['configs'][configs['configs']['use']]
Vagrant.configure('2') do |config|
config.vm.network 'public_network', ip: vagrant_config['public_ip']
...
In my config.yaml:
---
configs:
use: 'home'
office:
public_ip: '192.168.2.117'
<more variables>...
home:
public_ip: '192.168.1.117'
<more variables>...
How use local environment variable with Vagrant?
one way I manage to have settings dependency is to use an external file (I use yaml, but any file would work like json .... Vagrantfile is a ruby script so as long as you can easily read it using ruby you're fine)
An example of my Vagrantfile using a Yaml dependency
:# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'yaml'
settings = YAML.load_file 'settings/common.yaml'
Vagrant.configure("2") do |config|
config.vm.box = settings['host_box'] || "pws/centos65"
config.ssh.username = settings['ssh_user']
config.vm.define "db" do |db|
db.vm.hostname = settings['db_hostname']
db.vm.network "private_network", ip: settings['host_db_address']
end
...
end
the file settings/common.yaml
would be defined as
---
host_db_address: "192.168.90.51"
host_app_address: "192.168.90.52"
db_hostname: "local.db"
ssh_user: "pws"
As said in comment, the main advantage I found using this technique is when you distribute box. My team would git clone the project, has to fill up the settings (for password dependency and so on) and ready to go.
How do I reuse variables in quoted line in a Vagrantfile?
It's more a question about Ruby syntax. You can use:
ansible.playbook = 'install/mydir/install_' + name + '.yml'
or (thanks to Frédéric Henri)
ansible.playbook = "install/mydir/install_#{name}.yml"
To reference the value from the hash (per OP's own suggestion):
ansible.playbook = "install/mydir/install_#{host[:name]}.yml"
Vagrantfile: Assign result of inline shell script to variable
I ended up using an approach like this one.
Something I discovered along the way is that all Ruby conditionals in the Vagrantfile are evaluated in the very beginning, although the inline Shell scripts inside these conditionals (if there are any) are not evaluated at the same time. This lead to some strange behavior, which I could avoid by doing like I said above.
How to add environment variables. Vagrant and WSL
In case you still want to run with WSL, this should do the trick
you need to run this in your shell prompt (not add to your Vagrantfile)
$ export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"
Add this part to your Vagrantfile
config.vm.provider "virtualbox" do |vb|
vb.customize [ "modifyvm", :id, "--uartmode1", "disconnected" ]
end
This was reported in vagrant issues
Vagrantfile: How do I pass a host environment variable with an unescaped dollar sign in it?
With a hint from @matt-schuchard, I added this at the top of Vagrantfile:
require 'shellwords'
and changed
env: {"MY_VARIABLE"=>ENV['CONTAINS_A_DOLLAR_SIGN']}
to
env: {"MY_VARIABLE"=>Shellwords.escape(ENV['CONTAINS_A_DOLLAR_SIGN'])}
Now vagrant up
outputs before$after
not just before
!
Related Topics
Skip Over Iteration in Enumerable#Collect
Rails. How to Store Time of Day (For Schedule)
What Is the Correct Way to Detect If Ruby Is Running on Windows
Can Bundler Show Me Which Gems in Gemfile Have Newer Versions (Eg. Dry-Run of Bundle Update)
Problem Signing Out with Devise on My App
Why Doesn't Rails' "Errors.Full_Messages" Replace Attribute and Message Variables
How Does Defining [Square Bracket] Method in Ruby Work
Where in the Ruby Language Is %Q, %W, etc., Defined
Ruby Dropped in Netbeans 7,How to Use It in Netbeans7
Rails 3 Additional Session Configuration Options (Key, Expires_After, Secure)
Rubygems Do Not Install on Os X Lion