Using Vagrant to manage development and production environments?

前端 未结 4 1647
北荒
北荒 2021-02-13 10:57

How are people handling simple automation (with puppet) for dev / prod environments with vagrant (ideally from the same vagrantfile)?

Use case I\'m trying to sol

4条回答
  •  忘了有多久
    2021-02-13 11:53

    Here is a simple way of dynamically changing the 'default' machine name depending on the specified --provider from the command line, so they won't conflict between the different providers:

    require 'getoptlong'
    opts = GetoptLong.new(
      [ '--provider', GetoptLong::OPTIONAL_ARGUMENT ],
      [ '--vm-name',  GetoptLong::OPTIONAL_ARGUMENT ]
    )
    
    provider=ENV['PROVIDER'] || 'virtualbox'
    vm_name=ENV['VM_NAME'] || 'default'
    opts.each do |opt, arg|
      case opt
        when '--provider'
          provider=arg
        when '--vm-name'
          vm_name=arg
      end
    end
    
    Vagrant.configure(2) do |config|
    
      # HERE you are dynamically changing the machine name to prevent conflict.
      config.vm.define "mt-#{provider}-#{vm_name}"
    
      # Below sections are just examples, not relevant.
      config.vm.provider "virtualbox" do |vm|
        vm.name = "test.local"
        vm.network "private_network", ip: "192.168.22.22"
        vm.customize ['modifyvm', :id, '--natdnshostresolver1', 'on']
        config.vm.box = "ubuntu/wily64"
      end
    
      config.vm.provider :aws do |aws, override|
        aws.aws_profile = "testing"
        aws.instance_type = "m3.medium"
        aws.ami = "ami-7747d01e"
        config.vm.box = "testing"
      end
    end
    

    Example usage:

    VM_NAME=dev PROVIDER=virtualbox vagrant up --provider=virtualbox
    VM_NAME=uat PROVIDER=aws vagrant up --provider=aws
    VM_NAME=test PROVIDER=aws vagrant up --provider=aws
    VM_NAME=prod PROVIDER=aws vagrant up --provider=aws
    VM_NAME=uat PROVIDER=aws vagrant destroy -f
    VM_NAME=test PROVIDER=aws vagrant status
    

    See also: Multiple provisioners in a single vagrant file?

提交回复
热议问题