我对 Ruby 的了解很少,但我正在为我的办公室开发一个 Vagrant VM。我在变量中配置了设置,以便我们的每个开发人员轻松自定义,但是当我尝试从外部文件中包含变量时遇到了问题。
这是我正在做的事情的基本要点(这有效):
# Local (host) system info
host_os = "Windows"
nfs_enabled = false
# IP and Port Configuration
vm_ip_address = "33.33.33.10"
vm_http_port = 80
host_http_port = 8888
vm_mysql_port = 3306
host_mysql_port = 3306 # Warning, mysql port configuration using 3306 will interfere with any locally run MySQL server.
local_sites_path = "D:\\Web"
vm_sites_path = ENV["HOME"] + "/Sites"
# VM Configuration
vm_memory = "1024"
Vagrant.configure("2") do |config|
... do vagrant stuff here
但是,这不起作用(config.local.rb 的内容与上面的变量声明匹配):
if(File.file?("config.local.rb"))
require_relative 'config.local.rb'
else
# Local (host) system info
host_os = "Mac"
nfs_enabled = true
# IP and Port Configuration
vm_ip_address = "33.33.33.10"
vm_http_port = 80
host_http_port = 8888
vm_mysql_port = 3306
host_mysql_port = 3306 # Warning, mysql port configuration using 3306 will interfere with any locally run MySQL server.
local_sites_path = ENV["HOME"] + "/Sites"
vm_sites_path = ENV["HOME"] + "/Sites"
# VM Configuration
vm_memory = "512"
end
Vagrant.configure("2") do |config|
... do vagrant stuff here
有什么想法吗?在这两种情况下,变量声明都在文件的顶部,所以我的理解是它们应该在全局范围内。
这是 config.local.rb 的内容:
# Duplicate to config.local.rb to activate. Override the variables set in the Vagrantfile to tweak your local VM.
# Local (host) system info
host_os = "Windows"
nfs_enabled = false
# IP and Port Configuration
vm_ip_address = "33.33.33.10"
vm_http_port = 80
host_http_port = 8888
vm_mysql_port = 3306
host_mysql_port = 3306 # Warning, mysql port configuration using 3306 will interfere with any locally run MySQL server.
local_sites_path = "D:\\Web"
vm_sites_path = ENV["HOME"] + "/Sites"
# VM Configuration
vm_memory = "1024"
正如我所说,我以前没有真正使用过 Ruby,但我所知道的关于编程和范围的一切都表明这应该可以正常工作。我已经检查(使用print
语句)该文件正在被脚本检测并包含,但由于某种原因,除非我直接在 Vagrantfile 中硬编码配置设置,否则它不起作用。
提前致谢。