Using Vagrant to manage development and production environments?

前端 未结 4 1643
北荒
北荒 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:34

    As for workaround, you should define config.vm.define (as suggested here), in order to support multiple providers.

    Please find the following configuration posted by @kzap as example:

    Vagrant.configure("2") do |config|
    
      # Store the current version of Vagrant for use in conditionals when dealing
      # with possible backward compatible issues.
      vagrant_version = Vagrant::VERSION.sub(/^v/, '')
    
      # Configuration options for the VirtualBox provider.
      def configure_vbox_provider(config, name, ip, memory = 2048, cpus = 1)
        config.vm.provider :virtualbox do |v, override| 
          # override box url
          override.vm.box = "ubuntu/trusty64"
          # configure host-only network
          override.vm.hostname = "#{name}.dev"
          override.vm.network :private_network, id: "vvv_primary", ip: ip
    
          v.customize ["modifyvm", :id, 
            "--memory", memory,
            "--cpus", cpus,
            "--name", name,
            "--natdnshostresolver1", "on",
            "--natdnsproxy1", "on"
          ]
        end
      end
    
      default_provider = "virtualbox"
      supported_providers = %w(virtualbox rackspace aws managed)
      active_provider = ENV['VAGRANT_ACTIVE_PROVIDER'] # it'd be better to get this from the CLI --provider option
      supported_providers.each do |provider|
      next unless (active_provider.nil? && provider == default_provider) || active_provider == provider
    
        #
        # VM per provider
        #
        config.vm.define :"sample-#{provider}" do | sample_web_config |
    
          case provider
          when "virtualbox"
            configure_vbox_provider(sample_web_config, "examine-web", "192.168.50.1")
    
          when "aws"
            configure_aws_provider(sample_web_config)
    
          when "managed"
            configure_managed_provider(sample_web_config, "1.2.3.4")
    
          when "rackspace"
            configure_rackspace_provider(sample_web_config)  
    
          end
      end
    
    end
    

    Or the following example posted at gist by @maxlinc:

    # -*- mode: ruby -*-
    # vi: set ft=ruby :
    
    # Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
    VAGRANTFILE_API_VERSION = "2"
    
    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
      config.vm.box = "dummy"
    
      config.vm.provider :rackspace do |rs|
        rs.username = ENV['RAX_USERNAME']
        rs.api_key  = ENV['RAX_API_KEY']
        rs.rackspace_region   = :ord
      end
    
      supported_providers = %w(virtualbox rackspace)
      active_provider = ENV['VAGRANT_ACTIVE_PROVIDER'] # it'd be better to get this from the CLI --provider option
      supported_providers.each do |provider|
        next unless active_provider.nil? || active_provider == provider
    
        config.vm.define "exact_name_#{provider}" do |box|
          box.vm.provider :rackspace do |rs|
            rs.flavor = '1 GB Performance'
            rs.image  = 'Ubuntu 14.04 LTS (Trusty Tahr) (PVHVM)'
          end
        end
    
        config.vm.define "regex_#{provider}" do |box|
          box.vm.provider :rackspace do |rs|
            rs.flavor = /1\s+GB\s+Performance/
            rs.image  = /Ubuntu.*Trusty Tahr.*(PVHVM)/
          end
        end
    
        config.vm.define "id_#{provider}" do |box|
          box.vm.provider :rackspace do |rs|
            rs.flavor = 'performance1-1'
            rs.image  = 'bb02b1a3-bc77-4d17-ab5b-421d89850fca'
          end
        end
    
        config.vm.define "unlisted_#{provider}" do |box|
          box.vm.provider :rackspace do |rs|
            rs.flavor = 'performance1-1'
            rs.image = '547a46bd-d913-4bf7-ac35-2f24f25f1b7a'
          end
        end
      end
    end
    

提交回复
热议问题