Puppet (apply) provisioner does not use correct default manifest with environments
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:
- If no
environment.conffile exist for an environment the default manifest should be the<ENVIRONMENTS DIRECTORY>/<ENVIRONMENT>/manifestsdirectory. - With the
manifestsetting inenvironment.confcan be set by using an absolute path or relative to the environment’s main directory. - If
manifestis not set inenvironment.confthedefault_manifestsetting inpuppet.confis 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.ppcode ref - With an
environment.confandmanifestset to an absolute path it is working as expected. code ref - With an
environment.confandmanifestset 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
- Is fixed by adding an else clause to this if statement
- 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
- 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.
1/2 make sense to me. Feel free to submit a PR or i can get to it soon.
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.
@ben57 Cool! I'll put up a PR tomorrow (european timezone)
@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 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- makes sense now :+1:
Thanks @Mattias- that has been super helpful to get my vagrant setup working.
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 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.
similar issue is discussed here: https://github.com/mitchellh/vagrant/issues/5615
@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?