Query functions for PuppetDB

914 views
Skip to first unread message

Erik Dalén

unread,
Jun 19, 2012, 10:48:46 AM6/19/12
to puppet...@googlegroups.com
I've created a set of functions that exposes the PuppetDB query API
from inside puppet. Feel free to give them a spin and get back to me
with any bugs or feedback.

https://github.com/dalen/puppet-puppetdbquery

They require ruby-restclient, ruby-json and the puppetdb-terminus.

To give you an example of a query you can do inside puppet code using
these functions and PuppetDB:

# Return an array of active nodes with an uptime more than 30 days and
# having the class 'apache'
$hosts = pdbnodequery([ 'and', [ '=', [ 'node', 'active' ], true ],
['>', [ 'fact', 'uptime_days' ], 30 ] ],
[ 'and', [ '=', 'type', 'Class' ], [ '=', 'title', 'Apache' ] ] )"

This can be used for example to populate lists of nodes behind load
balancers and such dynamically instead of writing them down in your
puppet code.

--
Erik Dalén

David Schmitt

unread,
Jun 19, 2012, 10:55:07 AM6/19/12
to puppet...@googlegroups.com
Nice! Do you have any number comparing the performance of this against
collection of a similar amount of resources by the puppetmaster?


Best Regards, David

Erik Dalén

unread,
Jun 19, 2012, 11:05:40 AM6/19/12
to puppet...@googlegroups.com
On 19 June 2012 16:55, David Schmitt <da...@dasz.at> wrote:
Not entirely sure I know what you mean, but it should be a lot faster
than parsing through cached catalogs searching for matching nodes if
that's what you mean?

So far I haven't done any extensive performance testing on PuppetDB, I
think the puppetlabs guys have though with good results. The
performance hit from these functions themselves if any would be in
parsing the returned JSON and handing that back to puppet, but the
time consumed for that should be minimal.

--
Erik Dalén

Deepak Giridharagopal

unread,
Jun 19, 2012, 11:39:31 AM6/19/12
to puppet...@googlegroups.com
On Tue, Jun 19, 2012 at 9:55 AM, David Schmitt <da...@dasz.at> wrote:
Looking at the code, it's issuing queries against the same HTTP endpoint as the PuppetDB resource terminus. For the same query, it should perform (quite literally) exactly the same; the functions are getting the same data in the same way from the same place.

Keep in mind, though, that these functions allow for more advanced queries than collection syntax currently allows: arbitrarily nested booleans, you can query for things other than resources, you can query across types, etc. So it's not _quite_ apples-to-apples. :)

deepak

--
Deepak Giridharagopal / Puppet Labs / grim_radical

Deepak Giridharagopal

unread,
Jun 19, 2012, 11:43:56 AM6/19/12
to puppet...@googlegroups.com
This is fantastic, Erik!

Erik Dalén

unread,
Jul 19, 2012, 1:02:32 PM7/19/12
to puppet...@googlegroups.com
I've released a new version of this module, it no longer requires the
rest_client gem and uses the Puppet HTTP pool instead.
It also supports getting facts for an array of hosts at once. As an
example if you want the IPs of all hosts with Apache class you could
do this:

$hosts = pdbfactquery(pdbresourcequery([ 'and', [ '=', [ 'node',
'active' ], true ], [ '=', 'type', 'Class' ], [ '=', 'title', 'Apache'
] ], 'certname'), 'ipaddress')

It is also on the Puppet Forge now:
http://forge.puppetlabs.com/dalen/puppetdbquery
--
Erik Dalén

Martin Willemsma

unread,
Jul 19, 2012, 5:28:07 PM7/19/12
to puppet...@googlegroups.com
2012/7/19 Erik Dalén <erik.gus...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups "Puppet Users" group.
> To post to this group, send email to puppet...@googlegroups.com.
> To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
>

Hi Erik,

This is cool! Nice job. I'm going to play with this

--
---
Kind regards,

Martin Willemsma

Nick Fagerlund

unread,
Jul 19, 2012, 6:36:45 PM7/19/12
to puppet...@googlegroups.com


On Thursday, July 19, 2012 10:02:32 AM UTC-7, Erik Dalén wrote:
It also supports getting facts for an array of hosts at once. As an
example if you want the IPs of all hosts with Apache class you could
do this:

$hosts = pdbfactquery(pdbresourcequery([ 'and', [ '=', [ 'node',
'active' ], true ], [ '=', 'type', 'Class' ], [ '=', 'title', 'Apache'
] ], 'certname'), 'ipaddress')


WOWWW, that is extremely cool. I can see all kinds of uses for that. 

Deepak Giridharagopal

unread,
Jul 19, 2012, 8:27:43 PM7/19/12
to puppet...@googlegroups.com
On Thu, Jul 19, 2012 at 07:02:32PM +0200, Erik Dal�n <erik.gus...@gmail.com> wrote:
> I've released a new version of this module, it no longer requires the
> rest_client gem and uses the Puppet HTTP pool instead.
> It also supports getting facts for an array of hosts at once. As an
> example if you want the IPs of all hosts with Apache class you could
> do this:
>
> $hosts = pdbfactquery(pdbresourcequery([ 'and', [ '=', [ 'node',
> 'active' ], true ], [ '=', 'type', 'Class' ], [ '=', 'title', 'Apache'
> ] ], 'certname'), 'ipaddress')

Awesome!

Peter Brown

unread,
Jul 19, 2012, 11:01:59 PM7/19/12
to puppet...@googlegroups.com
Very cool!

My brain has extrapolated a few cool uses already. :)

Thanks Erik!
Reply all
Reply to author
Forward
0 new messages