With puppet, sometimes it is necessary to make case statements around what version of a package is installed rather than having puppet dictate what version is installed. Say "add x line to sshd config for x version." To do this we need to have fact with what package version is already installed on the system, here's a quick script to do that in bulk. To deploy, add this as a plugin in a module.

#!/usr/bin/ruby
require 'facter'

packages = '(^bash |^glibc.x86_64 |^httpd |^jdk |^jre |^mod_ssl |^openssl |^php |^sshd )'
version = Facter::Util::Resolution.exec("rpm -qa --queryformat '[%{NAME} %{VERSION}-%{RELEASE}\n]' | egrep '#{packages}'")

version.each_line do |package|
  Facter.add("package_#{package.split[0]}".gsub('-','_')) do
    setcode do
      "#{package.split[1]}"
    end
  end
end

In this case we've got a regex with all the packages we care about, I know you could make this an array and loop over it but the performance is better to call the rpm command just once and then loop over the output. Technically you could do all packages but that seems pointless to me and would also be a ton of packages. Then the other trick was changing '-' to '_' because dashes aren't allowed in fact names but are quite common in package names.

Then in our manifests we can make case statements or if blocks on these new fancy facts.

if $package_foo =~ /3.2.7/ {
    file { '/usr/local/foo/conf/config.yaml' :
        ensure  => 'file',
        source  => 'puppet:///modules/foo/3.2.7/usr/local/foo/conf/config.yaml',
    }
} else {
    notify { 'foo_unknown' :
        message => 'I don't know what version of 'foo' to configure, this run is foobar!',
    }
}

Let me know if you find this helpful, horrible, or both.