Skip to content

Commit

Permalink
Return an array of all installed versions including release and arch
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-mueller committed May 20, 2016
1 parent aa7ea72 commit 89ff036
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
39 changes: 26 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

## Description

This module is exporting a structured fact hash ("packages") which contains all installed (RPM-) packages and their versions.
This includes packages which are installed as a dependency and not managed via Puppet directly.
This module is exporting a structured fact hash ("packages") which contains all installed (RPM-) packages and their versions (including release and architecture).
It includes packages which are installed as a dependency and not managed via Puppet directly.
As packages can be installed multiple times with different versions (e.g. "kernel"), the output is always an array with all installed versions.

It is intended to be used together with PuppetDB to provide a fast and reliable way to query all packages and versions in your infrastructure.

Expand All @@ -23,10 +24,22 @@ You can check if the facts are working correctly, e.g.:
```
# facter -p packages
{
dnsmasq => "2.66",
filesystem => "3.2",
libdrm => "2.4.50",
kbd_misc => "1.15.5",
audit_libs => [
"2.3.3-4.el7.x86_64"
],
jansson => [
"2.4-6.el7.x86_64"
],
sed => [
"4.2.2-5.el7.x86_64"
],
kernel => [
"3.10.0-123.el7.x86_64",
"3.10.0-327.18.2.el7.x86_64"
],
libdb => [
"5.3.21-17.el7.x86_64"
],
(...)
}
```
Expand All @@ -35,18 +48,18 @@ You can now query PuppetDB for these facts.
E.g. "What version of yum is currently deployed on my servers?"

```
$ curl -s -X GET 'http://localhost:8080/pdb/query/v4/fact-contents?pretty=true' --data-urlencode 'query=["=", "path", [ "packages", "yum" ]]'
$ curl -s -X GET 'http://localhost:8080/pdb/query/v4/fact-contents?pretty=true' --data-urlencode 'query=["~>", "path", [ "packages", "yum", ".*" ]]'
[ {
"certname" : "node1",
"path" : [ "packages", "yum" ],
"certname" : "puppet",
"path" : [ "packages", "yum", 0 ],
"name" : "packages",
"value" : "3.4.3",
"value" : "3.4.3-118.el7.centos.noarch",
"environment" : "production"
}, {
"certname" : "puppet",
"path" : [ "packages", "yum" ],
"certname" : "node1",
"path" : [ "packages", "yum", 0 ],
"name" : "packages",
"value" : "3.4.3",
"value" : "3.4.3-118.el7.centos.noarch",
"environment" : "production"
} ]
```
Expand Down
8 changes: 6 additions & 2 deletions lib/facter/pkg_inventory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
setcode do
packages = {}

package_list = Facter::Util::Resolution.exec('/bin/rpm --query --all --qf "%{NAME};;%{VERSION}\n"')
package_list = Facter::Util::Resolution.exec('/bin/rpm --query --all --qf "%{NAME};;%{VERSION}-%{RELEASE}%|ARCH?{.%{ARCH}}|\n"')
package_list.split("\n").each do |line|

pkg_split = line.split(";;")
pkg_name = pkg_split[0].gsub('-', '_')
pkg_version = pkg_split[1]

packages[pkg_name] = pkg_version
if packages.key?(pkg_name)
packages[pkg_name].push(pkg_version)
else
packages[pkg_name] = [ pkg_version ]
end

end

Expand Down
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rmueller-pkg_inventory",
"version": "0.1.0",
"version": "0.2.0",
"author": "rmueller",
"summary": "Export package inventory facts",
"license": "Apache-2.0",
Expand Down

0 comments on commit 89ff036

Please sign in to comment.