本文介绍了如何使用Ruby制作Vagrantfile模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个Vagrantfile,每个Vagrantfile代表一个不同的provider,因为Vagrant有一个限制,即不允许同时使用相同的Vagrantfile进行两项或多项准备.因此,我分为两个或多个Vagrantfiles,但是我的"body",我的配置脚本对于两个都是相同的,唯一改变的是provider块.

I have several Vagrantfile, each one for a different provider, since Vagrant has a limitation that doesn't allow to make two or more provisions at the same time using the same Vagrantfile.So, I split into two or more Vagrantfiles, but my "body", my provisions scripts are the same for both, the only thing that changes is the provider block.

例如:

local_nagios/Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

require 'yaml'
set = YAML.load_file '../../../settings.yaml'

Vagrant.configure(2) do |nagios|

  nagios.vm.provider :virtualbox do |provider, override|
    override.vm.box = 'ubuntu/trusty64'
    override.vm.hostname = 'nagios.company.com'
    override.vm.synced_folder '.', '/vagrant', disabled:true
    override.vm.network 'public_network', bridge:set['network_interface'], ip:set['dev_nagios_ip']

    provider.memory = 4096
    provider.cpus = 2
  end

  install = set['devops_home'] + '/vagrant/lib/install'
  nagios.vm.provision 'shell', path: install + '/basic'
  nagios.vm.provision 'shell', path: install + '/devops'

  step = set['devops_home'] + '/vagrant/server/nagios/steps'
  nagios.vm.provision 'shell', path: step + '/install', args: [set['nagios_password']]

end

digital_nagios/Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

require 'yaml'
set = YAML.load_file '../../../settings.yaml'

Vagrant.configure(2) do |nagios|

  nagios.vm.provider :digital_ocean do |provider, override|
    override.vm.box = 'digital_ocean'
    override.vm.hostname = 'nagios.company.com'
    override.vm.synced_folder '.', '/vagrant', disabled:true
    override.ssh.private_key_path = '~/.ssh/id_rsa'

    provider.token = 'my-token'
    provider.image = 'ubuntu-14-04-x64'
    provider.region = 'fra1'
    provider.size = '4gb'
  end

  install = set['devops_home'] + '/vagrant/lib/install'
  nagios.vm.provision 'shell', path: install + '/basic'
  nagios.vm.provision 'shell', path: install + '/devops'

  step = set['devops_home'] + '/vagrant/server/nagios/steps'
  nagios.vm.provision 'shell', path: step + '/install', args: [set['nagios_password']]

end

我想知道是否可以从中制作模板.或像这样导入我的公共区域:

I wonder if is possible to make a template from this. Or import my common area like this:

Vagrant.configure(2) do |nagios|

  nagios.vm.provider :digital_ocean do |provider, override|
    ...
  end

  import '../provision.rb'

end

我对Ruby不熟悉,因此任何建议将不胜感激!

I'm not familiar with Ruby, so any suggestion would be very appreciated!

推荐答案

您可以做类似的事情

Vagrant.configure(2) do |nagios|

  nagios.vm.provider :digital_ocean do |provider, override|
    ...
  end

  eval File.read("../Vagrantfile-common")

end

这篇关于如何使用Ruby制作Vagrantfile模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 18:05