如何在 Vagrant 中使用本地环境变量?
How use local environment variable with Vagrant?
我正在像这样传递本地环境变量:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |de|
de.vm.box = 'ubuntu/trusty64'
de.vm.hostname = 'virtual_machine'
de.vm.network 'public_network', bridge:ENV['NETWORK_INTERFACE'], ip:'192.168.2.170'
de.vm.provider "virtualbox" do |v|
v.memory = 4096
v.cpus = 2
end
de.vm.synced_folder '.', '/vagrant', disabled:true
de.vm.synced_folder '../../synced/shared/', '/shared/'
de.vm.synced_folder '../../synced/devops/', '/devops/'
install = ENV['DEVOPS_HOME'] + '/vagrant/lib/install'
de.vm.provision 'shell', path: install + '/basic'
de.vm.provision 'shell', path: install + '/java8', args: ['automatic']
de.vm.provision 'shell', path: install + '/aws_cli', args: [ENV['S3_AWS_ACCESS_KEY_ID'],ENV['S3_AWS_SECRET_ACCESS_KEY']]
setup = ENV['DEVOPS_HOME'] + '/vagrant/lib/setup'
de.vm.provision 'shell', path: setup + '/hosts'
sys = ENV['DEVOPS_HOME'] + '/vagrant/lib/system'
de.vm.provision 'shell', path: sys + '/add_user', args: ['virtual-machine',ENV['VIRTUAL_MACHINE_PASSWORD']]
steps = ENV['DEVOPS_HOME'] + '/vagrant/server/virtual_machine/steps'
de.vm.provision 'shell', path: steps + '/install_rserve'
end
显然,为此我需要在我的 ~/.profile
文件中设置这个变量。但我想知道是否有另一种方法可以做到这一点。在我不需要通过 Vagrantfile
通知的地方,它看起来不太好。
我设法拥有设置依赖性的一种方法是使用外部文件(我使用 yaml,但任何文件都可以像 json .... Vagrantfile 是一个 ruby 脚本,因此只要您可以使用 ruby 轻松阅读就可以了)
我的 Vagrantfile 使用 Yaml 依赖项的示例
:# -*- 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
文件 settings/common.yaml
将被定义为
---
host_db_address: "192.168.90.51"
host_app_address: "192.168.90.52"
db_hostname: "local.db"
ssh_user: "pws"
如评论中所述,我发现使用此技术的主要优点是分发盒子时。我的团队会 git 克隆项目,必须填写设置(密码依赖等)并准备就绪。
我正在像这样传递本地环境变量:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |de|
de.vm.box = 'ubuntu/trusty64'
de.vm.hostname = 'virtual_machine'
de.vm.network 'public_network', bridge:ENV['NETWORK_INTERFACE'], ip:'192.168.2.170'
de.vm.provider "virtualbox" do |v|
v.memory = 4096
v.cpus = 2
end
de.vm.synced_folder '.', '/vagrant', disabled:true
de.vm.synced_folder '../../synced/shared/', '/shared/'
de.vm.synced_folder '../../synced/devops/', '/devops/'
install = ENV['DEVOPS_HOME'] + '/vagrant/lib/install'
de.vm.provision 'shell', path: install + '/basic'
de.vm.provision 'shell', path: install + '/java8', args: ['automatic']
de.vm.provision 'shell', path: install + '/aws_cli', args: [ENV['S3_AWS_ACCESS_KEY_ID'],ENV['S3_AWS_SECRET_ACCESS_KEY']]
setup = ENV['DEVOPS_HOME'] + '/vagrant/lib/setup'
de.vm.provision 'shell', path: setup + '/hosts'
sys = ENV['DEVOPS_HOME'] + '/vagrant/lib/system'
de.vm.provision 'shell', path: sys + '/add_user', args: ['virtual-machine',ENV['VIRTUAL_MACHINE_PASSWORD']]
steps = ENV['DEVOPS_HOME'] + '/vagrant/server/virtual_machine/steps'
de.vm.provision 'shell', path: steps + '/install_rserve'
end
显然,为此我需要在我的 ~/.profile
文件中设置这个变量。但我想知道是否有另一种方法可以做到这一点。在我不需要通过 Vagrantfile
通知的地方,它看起来不太好。
我设法拥有设置依赖性的一种方法是使用外部文件(我使用 yaml,但任何文件都可以像 json .... Vagrantfile 是一个 ruby 脚本,因此只要您可以使用 ruby 轻松阅读就可以了)
我的 Vagrantfile 使用 Yaml 依赖项的示例
:# -*- 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
文件 settings/common.yaml
将被定义为
---
host_db_address: "192.168.90.51"
host_app_address: "192.168.90.52"
db_hostname: "local.db"
ssh_user: "pws"
如评论中所述,我发现使用此技术的主要优点是分发盒子时。我的团队会 git 克隆项目,必须填写设置(密码依赖等)并准备就绪。