I created Ohai plugin that solves the issue for me. Place the following code into a .rb file, and put the file into ohai/plugin/linux
folder. (Cannot provide absolute path since it varies depending on the platform and installation type)
Ohai.plugin(:PackageVersion) do
provides "package_version"
depends "platform_family"
collect_data do
pckg_list = Hash.new
case platform_family
when 'debian'
pckg_list = eval '{'+`dpkg-query -W -f='"${Package}"=> "${Version}", '`+'}'
when 'rhel' || 'fedora'
pckg_list = eval '{'+`rpm -qa --queryformat '"%{NAME}"=> "%{VERSION}", '`+'}'
when 'arch'
pckg_list = eval '{'+`package-query -Q -f '"%n"=> "%v", '`+'}'
when 'gentoo'
pckg_list = eval '{'+`equery list --format='"$name" => "$version", ' '*'`+'}'
end
package_version Mash.new pckg_list
end
end
Next time you run Chef, package version information will be in node["package_version"]["<package_name>"]
e.g. node["package_version"]["glibc"]
. I made it work for 5 platform families, but for Arch you'll have to make sure you have package-query
installed.
Important note: Ohai is loaded/populated before Chef recipe execution. This solution will not automatically update Ohai, so if you want to access data of the newly installed/upgraded packages, during current Chef run, you'll have to manually reload Ohai. To do it, place following block to your recipe under the package installation/upgrade block, just like shown below:
#example package instalation
package "whatever"
ohai "reload_ohai" do
action :reload
end
Thanks to @markoconnor. This solution is based on community plugins he pointed out. Those are no longer online. I adjusted them to work with Chef 11.0+ and here they are.