How do I include variables in my VagrantFile?

后端 未结 4 754
情话喂你
情话喂你 2020-12-13 00:29

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 co

相关标签:
4条回答
  • 2020-12-13 01:02

    Use require_relative:

    require_relative 'vagrant.rb'
    include MyVars
    # ...
    
    0 讨论(0)
  • 2020-12-13 01:03

    Try changing your require to this:

    require './vagrant'
    
    0 讨论(0)
  • 2020-12-13 01:05

    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!

    0 讨论(0)
  • 2020-12-13 01:29

    I use the approach of https://puphpet.com, I create a file config.yaml in the same directory of the Vagrantfile and...

    In my 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:

    ---
    configs:
        use: 'home'
        office:
            public_ip: '192.168.2.117'
            <more variables>...
        home:
            public_ip: '192.168.1.117'
            <more variables>...
    
    0 讨论(0)
提交回复
热议问题