From 89ff036681361b58aceb9d949ba652da3fd1d2f6 Mon Sep 17 00:00:00 2001 From: Roman Mueller Date: Fri, 20 May 2016 21:52:25 +0200 Subject: [PATCH] Return an array of all installed versions including release and arch --- README.md | 39 ++++++++++++++++++++++++------------- lib/facter/pkg_inventory.rb | 8 ++++++-- metadata.json | 2 +- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index c5cea39..ba1cf0a 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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" + ], (...) } ``` @@ -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" } ] ``` diff --git a/lib/facter/pkg_inventory.rb b/lib/facter/pkg_inventory.rb index 3eecd1b..16eccec 100644 --- a/lib/facter/pkg_inventory.rb +++ b/lib/facter/pkg_inventory.rb @@ -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 diff --git a/metadata.json b/metadata.json index e4794fc..c666019 100644 --- a/metadata.json +++ b/metadata.json @@ -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",