Is it possible to get inside-out ordering of provi

2019-09-10 18:01发布

问题:

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