dracoblue.net

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
modules\
  nginx
  stdlib

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!
VAGRANTFILE_API_VERSION = "2"

PUPPET_MODULE_INSTALL_STRING = "if [ ! -d \"/etc/puppet/modules/%{module_name}-%{version}\" ];
then
   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}
fi;
if [ ! -L \"/etc/puppet/modules/%{module_name}\" ];
then
   ln -s /etc/puppet/modules/%{module_name}-%{version} /etc/puppet/modules/%{module_name}
fi;"

PUPPET_GIT_MODULE_INSTALL_STRING = "if [ ! -d \"/etc/puppet/modules/%{module_name}\" ];
then
   git clone %{url} /etc/puppet/modules/%{module_name}
fi;
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' }
  end
  # ...

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:

modules\
  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:

modules\
  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 powered by Disqus

Recent Files

Advertisement

Recent Dev-Articles

Read recently

About

Blogroll