Is it possible to get inside-out ordering of provisioners in a Vagrant multi-machine setup?

时光怂恿深爱的人放手 提交于 2019-12-13 07:16:33

问题


Is it possible to reverse the order of provisioners from innermost to outermost when using a multi-machine setup? I want a small shell provisioner to create some facts in /etc/facter/facts.d/ before provisioning with puppet, to mimic our current setup as much as possible. (I have inherited a large puppet repo and am trying to create a Vagrant testbed for it before I start doing changes.)

The puppet settings are the same for every box, but requires the shell provisioner to run first. Here's an example Vagrantfile to show what I want to do (some names changed to protect the innocent):

$facts =<<FACTS
set -x
mkdir -p /etc/facter/facts.d
echo role=$1        > /etc/facter/facts.d/role.txt
echo location=$2    > /etc/facter/facts.d/location.txt
echo environment=$3 > /etc/facter/facts.d/environment.txt
FACTS

Vagrant.configure(2) do |config|
  config.vm.box = "centos-6.6"
  config.vm.synced_folder "hiera", "/etc/puppet/hiera"

  config.vm.provision :puppet do |puppet|
    puppet.manifest_file = "site.pp"
    puppet.module_path = ["modules", "internal"]
    puppet.hiera_config_path = "hiera.yaml"
    puppet.options = "--test"
  end

  config.vm.define :foo1 do |c|
    c.vm.hostname = "foo-1.vagrant"
    c.vm.provision :shell, inline: $facts, args: "foo testing stage"
  end

  config.vm.define :bar do |c|
    c.vm.hostname = "bar-1.vagrant"
    c.vm.provision :shell, inline: $facts, args: "bar testing stage"
  end

  # ... more machines omitted ...

end

回答1:


Answering my own question as I found an acceptable workaround: I moved the puppet provisioning into the inner block. Here's what my current code looks like:

$facts =<<SET_FACTS
set -x
mkdir -p /etc/facter/facts.d
echo role=$1        > /etc/facter/facts.d/role.txt
echo location=$2    > /etc/facter/facts.d/location.txt
echo environment=$3 > /etc/facter/facts.d/environment.txt
SET_FACTS

module Vagrant
  module Config
    module V2
      class Root
        def provision(role, location, environment)
          vm.provision "set-facts",
                         type: :shell,
                         inline: $facts,
                         args: [role, location, environment].map { |x| x.to_s }
          vm.provision :puppet do |puppet|
            puppet.manifest_file = "site.pp"
            puppet.module_path = ["modules", "internal"]
            puppet.hiera_config_path = "hiera.yaml"
          end
        end
      end
    end
  end
end

Vagrant.configure(2) do |config|
  config.vm.box = "centos-6.6"
  config.vm.synced_folder "hiera", "/etc/puppet/hiera"

  config.vm.define :foo1 do |c|
    c.vm.hostname = "foo-1.vagrant"
    c.provision(:foo, :testing, :stage)
  end

  config.vm.define :bar1 do |c|
    c.vm.hostname = "bar-1.vagrant"
    c.provision(:bar, :testing, :stage)
  end
end


来源:https://stackoverflow.com/questions/32831492/is-it-possible-to-get-inside-out-ordering-of-provisioners-in-a-vagrant-multi-mac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!