Puppetdb query. Search two classes

746 views
Skip to first unread message

Maksim Podlesniy

unread,
Aug 7, 2014, 5:02:33 AM8/7/14
to puppet...@googlegroups.com
How I may find all nodes with two specified classes? Example: find all nodes with class Nginx and Php

Sorry for my bad english.

Ken Barber

unread,
Aug 7, 2014, 9:18:45 AM8/7/14
to Puppet Users
I might be thinking about this the wrong way, but I think the API can
only do so much on the server side to achieve this. In particular I
want to do a distinct aggregation but we don't support that.

Fortunately, this is achievable on the command line with a tool like
JGrep: http://jgrep.org/#howto

Here is how I would do it:

https://gist.github.com/kbarber/6840f5b1a74d985d8167

Any entry that has a 2 next to it, meets the criteria basically.

ken.
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to puppet-users...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/8ae479f4-c700-490f-83d3-4e6ec1df292a%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

José Luis Ledesma

unread,
Aug 7, 2014, 9:27:23 AM8/7/14
to puppet...@googlegroups.com

If you have puppetdb you can query it through curl or puppetdbquery

https://docs.puppetlabs.com/puppetdb/latest/api/query/tutorial.html

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

El 07/08/2014 14:45, "Maksim Podlesniy" <crypt...@gmail.com> escribió:
How I may find all nodes with two specified classes? Example: find all nodes with class Nginx and Php

Sorry for my bad english.

--

Maksim Podlesniy

unread,
Aug 8, 2014, 7:58:40 AM8/8/14
to puppet...@googlegroups.com
Thanks, but I says about query like this:

["and",
["and",
["=", "type", "Class"],
["=", "title", "Php"]],
["and",
["=", "type", "Class"],
["=", "title", "Nginx"]]]

Unfortunately, this query doesn't work.

I use generate function and my own python script for puppetdb query. My question is how get list of nodes with two resources in one query.

четверг, 7 августа 2014 г., 16:18:45 UTC+3 пользователь Ken Barber написал:

Ken Barber

unread,
Aug 8, 2014, 8:15:06 AM8/8/14
to Puppet Users
> Thanks, but I says about query like this:
>
> ["and",
> ["and",
> ["=", "type", "Class"],
> ["=", "title", "Php"]],
> ["and",
> ["=", "type", "Class"],
> ["=", "title", "Nginx"]]]

Think about what this does behind the scenes on the resources endpoint
(see the query here:
https://gist.github.com/kbarber/d557da843b363ce3af3a#file-gistfile1-sql-L9-L13)
, it ultimately is just going to attempt to find a class resource that
has the title php AND nginx. Since a column can't have two values,
this will return nothing. If you had a base OR at the top, it would
return both the php and nginx classes, but this still isn't going to
provide you everything, but at least it will give you enough
information to break this down on the client end.

In the SQL world, the way I might construct such a query, is to get
all the class resources with nginx and php as the title, and then you
would need a list of distinct certificate names for both, and you
would want to find the overlap between the two, using an intersection
query for example
(http://www.postgresql.org/docs/9.3/static/queries-union.html). None
of this is supported by the PuppetDB query API today.

> Unfortunately, this query doesn't work.
>
> I use generate function and my own python script for puppetdb query. My
> question is how get list of nodes with two resources in one query.

Take another look at my suggestion:
https://gist.github.com/kbarber/6840f5b1a74d985d8167. It _is_ one
query. There is no shame in doing some work on the client side to
finish the job, you should be able to accomplish this last step in
Python quite easily.

ken.

Ken Barber

unread,
Aug 11, 2014, 2:19:54 PM8/11/14
to Puppet Users
>> Thanks, but I says about query like this:
>>
>> ["and",
>> ["and",
>> ["=", "type", "Class"],
>> ["=", "title", "Php"]],
>> ["and",
>> ["=", "type", "Class"],
>> ["=", "title", "Nginx"]]]
>
> Think about what this does behind the scenes on the resources endpoint
> (see the query here:
> https://gist.github.com/kbarber/d557da843b363ce3af3a#file-gistfile1-sql-L9-L13)
> , it ultimately is just going to attempt to find a class resource that
> has the title php AND nginx. Since a column can't have two values,
> this will return nothing. If you had a base OR at the top, it would
> return both the php and nginx classes, but this still isn't going to
> provide you everything, but at least it will give you enough
> information to break this down on the client end.
>
> In the SQL world, the way I might construct such a query, is to get
> all the class resources with nginx and php as the title, and then you
> would need a list of distinct certificate names for both, and you
> would want to find the overlap between the two, using an intersection
> query for example
> (http://www.postgresql.org/docs/9.3/static/queries-union.html). None
> of this is supported by the PuppetDB query API today.

Contrary to my last statement, this might work:
https://gist.github.com/kbarber/61d7c04f4d898148a06f

Deepak pointed that last one out. Let us know if it works.

ken.
Reply all
Reply to author
Forward
0 new messages