Updating Puppet Modules in Vagrant Boxes

If you are working with vagrant machines and provision those machines with puppet, you will find plenty of good modules at puppetforge and of course at github.

But in both cases you will download a specific version of the module and clone it into a directory called modules.

# e.g. for stdlib and nginx module

The problem is, that you can't see, which version of the puppet module is installed. Additionally you can't upgrade it, because so far I have not found an automatic way to do it.

That's why I show the way, how I worked around this issue. For the tldr; guys: example file.

My Vagrantfile starts with the following epilog:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!

PUPPET_MODULE_INSTALL_STRING = "if [ ! -d \"/etc/puppet/modules/%{module_name}-%{version}\" ];
   rm -rf /etc/puppet/modules/%{module_name} &&
   puppet module install %{forge_user}/%{module_name} --version %{version} &&
   mv /etc/puppet/modules/%{module_name} /etc/puppet/modules/%{module_name}-%{version}
if [ ! -L \"/etc/puppet/modules/%{module_name}\" ];
   ln -s /etc/puppet/modules/%{module_name}-%{version} /etc/puppet/modules/%{module_name}

PUPPET_GIT_MODULE_INSTALL_STRING = "if [ ! -d \"/etc/puppet/modules/%{module_name}\" ];
   git clone %{url} /etc/puppet/modules/%{module_name}
cd /etc/puppet/modules/%{module_name} && git pull origin master

These lines will be used in the shell provisioner to pin the modules, before puppet runs (config.vm.provision :puppet do |puppet|)!

config.vm.provision :shell do |shell|
  shell.inline = "test `find /var/cache/apt/pkgcache.bin -cmin +60` && apt-get update --fix-missing;" +
    "if [ ! -d \"/etc/puppet/modules\" ]; then mkdir -p /etc/puppet/modules; fi;" +
    "if [ ! -f \"/usr/bin/git\" ]; then apt-get install git -y; fi;" +
    PUPPET_MODULE_INSTALL_STRING % { :forge_user => 'puppetlabs', :module_name => 'stdlib', :version => '4.0.0' } +
    PUPPET_GIT_MODULE_INSTALL_STRING % { :url => 'https://github.com/jfryman/puppet-nginx.git', :module_name => 'nginx' }
  # ...

apt-get update with -cmin +60: The first line is a trick, to update apt-get on ubuntu only if the last update is at least 60 minutes ago. Very help full, if you don't want to see apt-get update output while testing your setup.

mkdir /etc/puppet/modules: To clone the modules or to install the modules from puppetforge, you need this folder to be available.

/usr/bin/git: Cloning git repositories is only possible, if you have git installed.

PUPPETMODULEINSTALL_STRING: This small example, installs the module stdlib by puppetlabs from puppetforge in version 4.1.0.

PUPPETGITMODULEINSTALLSTRING: This small example, clones https://github.com/jfryman/puppet-nginx.git into the folder nginx and keeps it up to date on every provisioning.

The resulting folder structure will be this:

  stdlib-4.0.0 # contains the module stdlib
  stdlib\ # symlink to stdlib-4.0.0
  nginx\ # git repository from https://github.com/jfryman/puppet-nginx.git

The advantage of this approach is, that git repositories will be updated on each vagrant provisioning execution. The other advantage is, that if you change the stdlib line to:

PUPPET_MODULE_INSTALL_STRING % { :forge_user => 'puppetlabs', :module_name => 'stdlib', :version => '4.1.0' } +

the module structure will look like this:

  stdlib-4.0.0 # contains the module stdlib in version 4.0.0
  stdlib-4.1.0 # contains the module stdlib in version 4.1.0
  stdlib\ # symlink to stdlib-4.1.0
  nginx\ # git repository from https://github.com/jfryman/puppet-nginx.git

This way you can ensure, that the modules are always installed in the version, you expect them to be installed!

Final hint: I noticed, that this way works very well. But usually, you'll ship your manifests WITH the required modules, because in this case you are not dependent on github's or puppetforge's availability and of the availability of the required modules!

In apt-get, git, open source, puppet, ubuntu, vagrant by
@ 07 Dec 2013, Comments at Reddit & Hackernews

Give something back

Were my blog posts useful to you? If you want to give back, support one of these charities, too!

Report hate in social media Campact e.V. With our technology and your help, we protect the oceans from plastic waste. Gesellschaft fur Freiheitsrechte e. V. The civil eye in the mediterranean

Recent Dev-Articles

Read recently

Recent Files