I’ve found a need to make decisions in manifests based on the particular version of a package that is installed. For example the configuration options for PostgreSQL 7.4 and 8.1 differ significantly so you may want to copy a different config file for different versions.

There isn’t an easy way to do this that I’ve found apart from defining a custom fact that queries the particular package version:

require 'puppet'
pkg = Puppet::Type.type(:package).create(:name => "ntp")
Facter.add("ntpversion") do
  setcode do
    pkg.retrieve[pkg.property(:ensure)]
  end
end

Add that fact to your config and you will get something like this from facter:

ntpversion => 1:4.2.4p4+dfsg-3ubuntu2

Now you can branch based on this fact. To be of much use it probably requires regex matching which is another layer of complexity, unfortunately.

Many thanks to Fujin for doing the hard work for this recipe.