如何在 Vagrant 中使用本地环境变量? [英] How use local environment variable with Vagrant?

查看:61
本文介绍了如何在 Vagrant 中使用本地环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我像这样传递我的本地环境变量:

I'm passing my local environment variables like this:

# -*- 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 通知的地方,它看起来不太好.

Obviously, for that I need to set this variable on my ~/.profile file. But I wonder if there is another way of doing this. Where I don't need to inform this via Vagrantfile, it doesn't look nice.

推荐答案

我设法获得设置依赖的一种方法是使用外部文件(我使用 yaml,但任何文件都可以像 json 一样工作.... Vagrantfile 是一个ruby 脚本,只要您可以使用 ruby​​ 轻松阅读它就可以了)

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)

我的 Vagrantfile 使用 Yaml 依赖项的示例

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

文件 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 clone 项目,必须填写设置(密码依赖等)并准备开始.

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.

这篇关于如何在 Vagrant 中使用本地环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆