ruby 如何在 VagrantFile 中包含变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16708917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:59:12  来源:igfitidea点击:

How do I include variables in my VagrantFile?

rubydeploymentvagrant

提问by MechaStorm

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 文件,但我收到一条错误消息:未初始化的常量 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

编辑:我正在使用的更新代码

回答by geedelur

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>...

回答by strager

Use require_relative:

使用require_relative

require_relative 'vagrant.rb'
include MyVars
# ...

回答by Matt Cooper

Try changing your require to this:

尝试将您的要求更改为:

require './vagrant'

回答by Marcello de Sales

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 的回答!