Can anyone guide me to how do I include variables in my VagrantFile? I am trying to inject configs into the Vagrantfile from an external file so that I can distribute the config to my colleagues without having them to hardcode configs directly on the Vagrantfile.
任何人都可以指导我如何在我的VagrantFile中包含变量?我试图从外部文件将配置注入Vagrantfile,这样我就可以将配置分发给我的同事,而无需直接在Vagrantfile上硬编码配置。
I had thought that since it was Ruby based I could just include a Ruby file but I get an error Message: unintialized constant MyVars
我曾经想过,因为它是基于Ruby的我可以只包含一个Ruby文件,但是我得到一个错误消息:unintialized constant MyVars
My VagrantFile simplified
我的VagrantFile简化了
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'vagrant.rb'
include MyVars
Vagrant.configure("2") do |config|
# Web
config.vm.define :joe do |joe|
joe.vm.box = "precise64_4.2.12"
joe.vm.hostname = WEBVMNAME
joe.vm.network :private_network, ip: "192.168.140.141"
# Port Forwarding
joe.vm.network :forwarded_port, guest: 22, host: 2201
joe.vm.network :forwarded_port, guest: 80, host: 8080
# Bootstrap Bash Script
joe.vm.provision :shell, :path => "bootstrap.sh"
end
end
And vagrant.rb contains
而vagrant.rb包含
module MyVars
WEBVMNAME = "rex"
end
Do note that I am also a newbie at Ruby so I am not sure as well if its just the syntax I got wrong?
请注意我也是Ruby的新手,所以我不确定它是否只是我的错误的语法?
Edit: Updated code I am using
编辑:我正在使用的更新代码
4 个解决方案
#1
41
I use the approach of https://puphpet.com, I create a file config.yaml in the same directory of the Vagrantfile and...
我使用https://puphpet.com的方法,在Vagrantfile的同一目录中创建一个文件config.yaml,然后...
In my Vagrantfile:
在我的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:
在我的config.yaml中:
---
configs:
use: 'home'
office:
public_ip: '192.168.2.117'
<more variables>...
home:
public_ip: '192.168.1.117'
<more variables>...
#2
5
Use require_relative
:
使用require_relative:
require_relative 'vagrant.rb'
include MyVars
# ...
#3
4
Try changing your require to this:
尝试将您的要求更改为:
require './vagrant'
#4
1
I created a library directory:
我创建了一个库目录:
require './lib/cfpEnvironment.rb'
include CFPEnvironment
And then did the scripting of what I need to be dynamic, defining the variables in the module created...
然后编写了我需要动态的脚本,在创建的模块中定义变量......
CFPPorts.select{ |key, value| value.numeric? }.each { |key, value|
config.vm.network :forwarded_port, guest: value, host: value
}
Thanks to @Matt and @strager for their answers above!
感谢@Matt和@strager的上述答案!
#1
41
I use the approach of https://puphpet.com, I create a file config.yaml in the same directory of the Vagrantfile and...
我使用https://puphpet.com的方法,在Vagrantfile的同一目录中创建一个文件config.yaml,然后...
In my Vagrantfile:
在我的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:
在我的config.yaml中:
---
configs:
use: 'home'
office:
public_ip: '192.168.2.117'
<more variables>...
home:
public_ip: '192.168.1.117'
<more variables>...
#2
5
Use require_relative
:
使用require_relative:
require_relative 'vagrant.rb'
include MyVars
# ...
#3
4
Try changing your require to this:
尝试将您的要求更改为:
require './vagrant'
#4
1
I created a library directory:
我创建了一个库目录:
require './lib/cfpEnvironment.rb'
include CFPEnvironment
And then did the scripting of what I need to be dynamic, defining the variables in the module created...
然后编写了我需要动态的脚本,在创建的模块中定义变量......
CFPPorts.select{ |key, value| value.numeric? }.each { |key, value|
config.vm.network :forwarded_port, guest: value, host: value
}
Thanks to @Matt and @strager for their answers above!
感谢@Matt和@strager的上述答案!