How can I list classes available on the puppet master?

3,163 views
Skip to first unread message

llo...@oreillyauto.com

unread,
Jul 12, 2012, 3:54:25 PM7/12/12
to puppet...@googlegroups.com
Is there a way to get a list of all the classes available from the puppet master?

I have 2 goals for this - one is documentation in a human readable form, and the other is potentially importing that data into dashboard.

I did find some info on the rest API (http://docs.puppetlabs.com/guides/rest_api.html#resource-types ) but I can't make any sense of the output from it.

Thanks.

Lee

Ryan Coleman

unread,
Jul 12, 2012, 8:08:10 PM7/12/12
to puppet...@googlegroups.com
One way to approach this (for documentation) is to build a file
resource backed by a template. The template would contain something
like the following. It prints out all the classes defined in that
agents catalog.

<% classes.each do |klass| -%>
The class <%= klass %> is defined
<% end -%>

This example could be used to build something like /etc/motd on each
system with the list of defined classes.

As for Dashboard, if you chose to use it as your ENC, the class
information is already there. Otherwise, that's not something you can
really do (afaik) outside of a custom fact that each machine populates
into the Dashboards inventory service.

HTH,
--Ryan
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/puppet-users/-/wmJFsgZDA8sJ.
> 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.



--
Ryan Coleman | about.me/ryc
Modules & Forge @ Puppet Labs

Ryan Coleman

unread,
Jul 12, 2012, 8:08:55 PM7/12/12
to puppet...@googlegroups.com

llo...@oreillyauto.com

unread,
Jul 12, 2012, 8:21:28 PM7/12/12
to puppet...@googlegroups.com
Comments inline.


On Thursday, July 12, 2012 3:08:10 PM UTC-5, Ryan Coleman wrote:
One way to approach this (for documentation) is to build a file
resource backed by a template. The template would contain something
like the following. It prints out all the classes defined in that
agents catalog.


I'm not really wanting a listing of classes currently applied to a given node.

I am wanting to get a listing of every class that is defined and available to be used in node defs.

 
<% classes.each do |klass| -%>
The class <%= klass %> is defined
<% end -%>

This example could be used to build something like /etc/motd on each
system with the list of defined classes.

As for Dashboard, if you chose to use it as your ENC, the class
information is already there. Otherwise, that's not something you can
really do (afaik) outside of a custom fact that each machine populates
into the Dashboards inventory service.


The class info is not already there, it only sees and can use classes that you define either through it's web UI or by inserting directly into it's DB.
 
HTH,
--Ryan

On Thu, Jul 12, 2012 at 8:54 AM, llowder wrote:
> Is there a way to get a list of all the classes available from the puppet
> master?
>
> I have 2 goals for this - one is documentation in a human readable form, and
> the other is potentially importing that data into dashboard.
>
> I did find some info on the rest API
> (http://docs.puppetlabs.com/guides/rest_api.html#resource-types ) but I
> can't make any sense of the output from it.
>
> Thanks.
>
> Lee
>
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/puppet-users/-/wmJFsgZDA8sJ.
> To post to this group, send email to puppet...@googlegroups.com.
> To unsubscribe from this group, send email to
> puppet-users+unsubscribe@googlegroups.com.

Nigel Kersten

unread,
Jul 12, 2012, 8:21:30 PM7/12/12
to puppet...@googlegroups.com
On Thu, Jul 12, 2012 at 8:54 AM, llo...@oreillyauto.com
<llo...@oreillyauto.com> wrote:
> Is there a way to get a list of all the classes available from the puppet
> master?

We've actually addressed this more directly in the upcoming Telly
branch, where we've extended the resource_type REST API to make this
simpler:

https://projects.puppetlabs.com/issues/14137

You'll be able to directly ask the question via an API of "what
classes exist in this environment?"

Note you can do this already prior to Telly, but you need to do
filtering yourself on the returned output to just grab class
information.




>
> I have 2 goals for this - one is documentation in a human readable form, and
> the other is potentially importing that data into dashboard.
>
> I did find some info on the rest API
> (http://docs.puppetlabs.com/guides/rest_api.html#resource-types ) but I
> can't make any sense of the output from it.
>
> Thanks.
>
> Lee
>
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/puppet-users/-/wmJFsgZDA8sJ.
> 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.



--
Nigel Kersten | http://puppetlabs.com | @nigelkersten
Schedule Meetings at: http://tungle.me/nigelkersten

R.I.Pienaar

unread,
Jul 12, 2012, 8:22:49 PM7/12/12
to puppet...@googlegroups.com


----- Original Message -----
> From: "Ryan Coleman" <ry...@puppetlabs.com>
> To: puppet...@googlegroups.com
> Sent: Thursday, July 12, 2012 9:08:55 PM
> Subject: Re: [Puppet Users] How can I list classes available on the puppet master?
>
> Crap, I forgot to mention my source:
> http://docs.puppetlabs.com/guides/templating.html#access-to-defined-tags-and-classes
>
> On Thu, Jul 12, 2012 at 1:08 PM, Ryan Coleman <ry...@puppetlabs.com>
> wrote:
> > One way to approach this (for documentation) is to build a file
> > resource backed by a template. The template would contain something
> > like the following. It prints out all the classes defined in that
> > agents catalog.
> >
> > <% classes.each do |klass| -%>
> > The class <%= klass %> is defined
> > <% end -%>

that classes array is built up during parsing so when used like this it wont be
complete till the end of the compile, so its not reliable.

same reason that defined() doesnt work.

Best way is to check classes.txt on the node.

this code:
-----
class one { }
class two { }

include one

notice(inline_template("<%= classes.inspect %>"))

include two

notice(inline_template("<%= classes.inspect %>"))
-----

does:
-----
notice: Scope(Class[main]): ["settings", "one"]
notice: Scope(Class[main]): ["settings", "one", "two"]
notice: Finished catalog run in 0.04 seconds
-----

you can see the contents depend on the parse order and will only be
complete at the end of the compile.

if you supply a class list from an ENC the whole ENC received list
will be in there from the start though

Dan White

unread,
Jul 12, 2012, 8:29:27 PM7/12/12
to puppet...@googlegroups.com
http://docs.puppetlabs.com/man/doc.html

I use a command like this:

puppet doc --outputdir <yadda...yadda>/apache/htdocs/puppetmaster --mode rdoc --manifestdir /etc/puppet/manifests --modulepath /etc/puppet/modules

And I get a fantastic web-tree describing every class on my PuppetMaster

It does require you to document your code, but it is worth it IMNSHO :)

“Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us.”
Bill Waterson (Calvin & Hobbes)

Walter Heck

unread,
Jul 12, 2012, 8:32:46 PM7/12/12
to puppet...@googlegroups.com
It's pretty ghetto, but we've used this script before, which I can confirm still works on Puppet 2.7.17: http://www.devco.net/archives/2010/02/26/what_does_puppet_manage_on_a_node-2.php

cheers,

Walter

--
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.




--
Walter Heck

--
Check out my startup: Puppet training and consulting @ http://www.olindata.com
Follow @olindata on Twitter and/or 'Like' our Facebook page at http://www.facebook.com/olindata

llo...@oreillyauto.com

unread,
Jul 12, 2012, 8:55:55 PM7/12/12
to puppet...@googlegroups.com

Comments inline

On Thursday, July 12, 2012 3:29:27 PM UTC-5, Ygor wrote:
http://docs.puppetlabs.com/man/doc.html

I use a command like this:

puppet doc --outputdir <yadda...yadda>/apache/htdocs/puppetmaster --mode rdoc --manifestdir /etc/puppet/manifests --modulepath /etc/puppet/modules

And I get a fantastic web-tree describing every class on my PuppetMaster

It does require you to document your code, but it is worth it IMNSHO :)


I had tried this before, unsuccessfully. But it is working on my dev environment (perhaps my prod and test masters don't have rdoc installed? Shall have to look into that later).

So I am glad that part is working.

Now I just need to find a good ay to get the info into dashboard.
 

R.I.Pienaar

unread,
Jul 12, 2012, 9:04:02 PM7/12/12
to puppet...@googlegroups.com
Here's a little script that finds all .pp files in an environment
parse them an print all the found classes.

I didnt test if it works for .pp files with more than 1 class in them

Just point it at your module path and environment at the top

--------
require 'puppet'
require 'find'

Puppet.settings[:modulepath] = "/etc/puppet/manifests/common/modules"
parser = Puppet::Parser::Parser.new(Puppet::Node::Environment.new("production"))

klasses = []

Find.find(Puppet.settings[:modulepath]) do |f|
next unless f =~ /\.pp$/

parser.file = f
ast = parser.parse

ast.instantiate('').each do |resource_type|
klasses << resource_type.namespace if resource_type.file == f
end

end

klasses.sort.each{|k| puts k}
----------

----- Original Message -----
> From: llo...@oreillyauto.com
> To: puppet...@googlegroups.com
> Sent: Thursday, July 12, 2012 9:55:55 PM
> Subject: Re: [Puppet Users] How can I list classes available on the puppet master?
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Puppet Users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/puppet-users/-/OFj4YYtBmBwJ .

Nan Liu

unread,
Jul 13, 2012, 12:17:19 AM7/13/12
to puppet...@googlegroups.com
So this is turning it into a small coding contest =). I needed
something similar to this, and there's a pretty easy way to do this
with the puppet resource_type face.

require 'puppet'
require 'puppet/face'

Puppet::Face[:resource_type,:current].search('*').find_all {|x|
x.type==:hostclass}.collect{|x| x.name}.sort

You can do some interesting things, such as get all the classes
parameters as well:

Puppet::Face[:resource_type,:current].search('*').find_all {|x|
x.type==:hostclass}.collect{|x| {x.name=>x.arguments.keys}}

Here's an example of classes and parameters it accept:

[{"apt::backports"=>["release", "location"]},
{"apt::debian::testing"=>[]},
{"apt::debian::unstable"=>[]},
{"apt::params"=>[]},
{"apt::release"=>["release_id"]},
{"apt::update"=>[]},
{"apt"=>
["purge_sources_list_d",
"purge_sources_list",
"always_apt_update",
"proxy_host",
"proxy_port",
"purge_preferences_d",
"disable_keys"]},
...

Thanks,

Nan

On Thu, Jul 12, 2012 at 9:50 AM, Nick Cammorato <nick_ca...@terc.edu> wrote:
> You could do something like this, it's not the cleanest in the world(I
> barely ever use sed anymore), but as something quick and dirty:
> curl -k -H "Accept: yaml" https://puppet:8140/mgmt/resource_types/class |
> more | grep " name: " | sed -e 's/^ *name: //;s/&id.* //' | sort | uniq
>
> It'll have some bad data you'll have to parse out(built-in classes like
> notify, fail, etc.), but should work.
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/puppet-users/-/m4XmKKAYwtkJ.

Nigel Kersten

unread,
Jul 13, 2012, 6:41:26 PM7/13/12
to puppet...@googlegroups.com
Nan, I just assigned https://projects.puppetlabs.com/issues/7079 to
you, it would be awesome if you could let us know in that ticket if
your sample code suffers the same problem of classes with the same
name as a node masking the existence of that node.

Nan Liu

unread,
Jul 13, 2012, 9:57:32 PM7/13/12
to puppet...@googlegroups.com
It doesn't seem to have the issue:

Puppet::Face[:resource_type,:current].search('*').collect{|x| x.type}
=> [:hostclass, :node]

Puppet::Face[:resource_type,:current].search('demo').collect{|x| x.type}
=> [:hostclass, :node]

Nan

Kamil Winczek

unread,
Jul 16, 2012, 11:57:44 AM7/16/12
to puppet...@googlegroups.com
On 12 Jul 12 08:54 -0700, llo...@oreillyauto.com wrote:
> Is there a way to get a list of all the classes available from the puppet
> master?
> I have 2 goals for this - one is documentation in a human readable form,
> and the other is potentially importing that data into dashboard.

Hi,

How about simple:

find . -name '*.pp' -exec awk '/^class [a-zA-Z]/ {print $2}' {} \;

?
--
Kamil Winczek, Gentoo Linux user, post no: 1443
GG:888677, M:kamil[at]winczek.com, JID:crs[at]jabster.pl
For a list of all the ways technology has failed to improve
the quality of life, please press three. - Alice Kahn

Ohad Levy

unread,
Jul 22, 2012, 1:03:03 PM7/22/12
to puppet...@googlegroups.com
On Mon, Jul 16, 2012 at 2:57 PM, Kamil Winczek <ka...@winczek.com> wrote:
On 12 Jul 12 08:54 -0700, llo...@oreillyauto.com wrote:
>   Is there a way to get a list of all the classes available from the puppet
>   master?
>   I have 2 goals for this - one is documentation in a human readable form,
>   and the other is potentially importing that data into dashboard.

Hi,

How about simple:

find . -name '*.pp' -exec awk '/^class [a-zA-Z]/ {print $2}' {} \;

You can also use foreman Proxy [1] to get that info for you :)

Ohad


[1]
 

?
--
Kamil Winczek, Gentoo Linux user, post no: 1443
GG:888677, M:kamil[at]winczek.com, JID:crs[at]jabster.pl
For a list of all the ways technology has failed to improve
the quality of life, please press three. - Alice Kahn
--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
Reply all
Reply to author
Forward
0 new messages