vagrant icon indicating copy to clipboard operation
vagrant copied to clipboard

Puppet (apply) provisioner does not use correct default manifest with environments

Open Mattias- opened this issue 11 years ago • 11 comments

Vagrant does not handle manifest files when using puppet environments in a correct way. Using puppet apply requires the invoker to provide a manifest or manifest directory to be executed so there is in fact no default per se. Vagrant however provides a higher level interface that do have defaults as it let users avoid too much boilerplate code. In my opinion the default behavior should mimic how puppet agent work.

How it should be

From https://docs.puppetlabs.com/puppet/latest/reference/dirs_manifest.html#with-puppet-master we got:

  1. If no environment.conf file exist for an environment the default manifest should be the <ENVIRONMENTS DIRECTORY>/<ENVIRONMENT>/manifests directory.
  2. With the manifest setting in environment.conf can be set by using an absolute path or relative to the environment’s main directory.
  3. If manifest is not set in environment.conf the default_manifest setting in puppet.conf is used. Keep in mind that manifests can be either files or directories.

How it is

  • Without an environment.confthe manifest is set to: environments_path/environment/manifests/site.pp code ref
  • With an environment.conf and manifest set to an absolute path it is working as expected. code ref
  • With an environment.conf and manifest set to a relative path the path is relative to the current $PWD (whatever pwd is at this moment i don't know). code ref

The solution

It seems like all these issues can be fixed by changing the parse_environment_metadata function in plugins/provisioners/puppet/provisioner/puppet.rb

  1. Is fixed by adding an else clause to this if statement
  2. Can be fixed by appending the path to the environment dir if it's relative otherwise just keep it as it is. on this line
  3. Can be ignored in my opinion as it would add too much complexity.

The changes should be <5 lines of code or so. Let me know what you think and if it's a bug or not and how it could be implemented without breaking provisioning for current users.

Pinging @benh57 and @mitchellh per the git blame.

Mattias- avatar Jul 17 '15 21:07 Mattias-

1/2 make sense to me. Feel free to submit a PR or i can get to it soon.

benh57 avatar Jul 17 '15 21:07 benh57

Also environments support is so brand new that i don't think we need to worry about the behavior of it changing, since it'll just be correct now.

benh57 avatar Jul 17 '15 21:07 benh57

@ben57 Cool! I'll put up a PR tomorrow (european timezone)

Mattias- avatar Jul 17 '15 21:07 Mattias-

@Mattias- Not sure if this is related but I just upgraded to 1.7.4 and got the following error with puppet apply.

Error: Could not parse application options: invalid option: --manifestdir
The SSH command responded with a non-zero exit status. Vagrant
assumes that this means the command failed. The output for this command
should be in the log above. Please read the output to determine what
went wrong.
    rbs1.vm.provision :puppet do |puppet|
    #  puppet.binary_path = 'C:\Program Files (x86)\Puppet Labs\Puppet\bin'
      #puppet.manifests_path = 'manifests'
      #puppet.manifest_file  = 'default.pp'
      #puppet.module_path    = 'modules'
      puppet.options = '--verbose --debug'
    end

lmayorga1980 avatar Jul 18 '15 17:07 lmayorga1980

@lmayorga1980 The problem is that your VM is running Puppet 4.x and the default Vagrant behaviour is targeted against Puppet 3.x. Your best option is to switch over to use Puppet environments as it is the new way of doing things. Read more about that here

By doing so you would also need to change your Vagrantfile to something like this:

      rbs1.vm.provision :puppet do |puppet|
        puppet.options = '--verbose --debug'
        puppet.environment = 'test'
        puppet.environment_path = 'environments'
        # Working dir needs to be set because vagrant does not calculate path
        # relative to the environment
        puppet.working_directory = '/tmp/vagrant-puppet/environments/test'
      end

and a environment.conf that has the following line manifest = manifests/

Mattias- avatar Jul 18 '15 18:07 Mattias-

@Mattias- makes sense now :+1:

lmayorga1980 avatar Jul 18 '15 18:07 lmayorga1980

Thanks @Mattias- that has been super helpful to get my vagrant setup working.

ggeldenhuis avatar Aug 25 '15 14:08 ggeldenhuis

Is there any way to work with this if I'm not using environments based on directories? Currently our puppet manifests follow the git branch per environment style (deployed with r10k, and using puppetfiles), and rewriting that to support this seems.... silly.

@Mattias- or other?

frizop avatar Sep 24 '15 20:09 frizop

@frizop that's exactly how we do things. I just check out each git branch into a separate directory.

I guess some way could be devised to have vagrant 'git checkout' the correct environment branch. you could probably do that with a shell provisioner step that runs before the puppet provisioner.

benh57 avatar Sep 24 '15 22:09 benh57

similar issue is discussed here: https://github.com/mitchellh/vagrant/issues/5615

valeriominetti avatar Oct 26 '15 14:10 valeriominetti

@Mattias- I can't get this work, could you help me please?

Previous Vagrantfile:

 config.vm.define "accounting" do |box|
  config.vm.provision "shell", path: "puppet/bootstrap.sh"
  config.vm.provision "puppet" do |puppet|
    puppet.working_directory = "/vagrant"
    puppet.module_path       = "puppet/modules"
    puppet.manifests_path    = "puppet/manifests"
    puppet.manifest_file     = "init.pp"
    puppet.hiera_config_path = "hiera.yaml"
    puppet.options           = "--environment=development"
  end
end

New Vagrantfile to work with Puppet 4.2:

 config.vm.define "accounting" do |box|
  config.vm.provision "shell", path: "puppet/bootstrap.sh"
  config.vm.provision "puppet" do |puppet|
    puppet.module_path       = "puppet/modules"
    puppet.manifests_path    = "puppet/manifests"
    puppet.manifest_file     = "init.pp"
    puppet.hiera_config_path = "hiera.yaml"
    puppet.options = '--verbose --debug'
    puppet.environment = 'development'
    puppet.environment_path = 'environments'
    puppet.working_directory = '/tmp/vagrant-puppet/environments/development'
  end
end

I have this working directory:

-project -- puppet --- modules --- environment.conf --- manifiests ---- environments ------ development ------- init.pp

But is not working, do you have any idea?

SoldierCorp avatar Oct 28 '15 19:10 SoldierCorp