Puppet Resource_Type Face

60 views
Skip to first unread message

Nan Liu

unread,
Feb 20, 2015, 12:03:03 PM2/20/15
to puppet-dev
In Puppet 3.3.x you can query the available resource_type via:

require 'puppet/face'
Puppet.parse_config
Puppet::Face[:resource_type,:current].find('*')

In Puppet 3.7.x, I can't seem to get it to return any resources (and I'm not sure the best way to pass in the puppet environment):

> require 'puppet/face'
>
> Puppet.settings.initialize_global_settings(['--environment=development'])
> Puppet['environment']
=> "development"
> Puppet['modulepath']
=> "/etc/puppetlabs/puppet/environments/$environment/modules:/opt/puppet/share/puppet/modules"
> Puppet::Face[:resource_type,:current].find('*')
=> nil

The command line option works, but not very useful:

$ puppet resource_type search *

#<Puppet::Resource::Type:0x00000002d139e8>
#<Puppet::Resource::Type:0x00000002d12840>
...

What's the right way to initialize the environment, and be able to get a list of resource_type?

Thanks,

Nan

Greg Sarjeant

unread,
Feb 26, 2015, 2:02:43 PM2/26/15
to puppe...@googlegroups.com
Hi, Nan.

If you're using directory environments, then you can do this in a couple of ways in PE 3.7.

First, The Node Classifier API exposes this information. You can use the puppetclassify gem to retrieve this information from the NC API. You can install the puppetclassify gem on your PE master with the vendored gem executable:
 
/opt/puppet/bin/gem install puppetclassify

Once that's installed, you can do something like this to fetch all of the classes in the development environment (replacing the dummy FQDN of the puppet master, of course).

#!/opt/puppet/bin/ruby

require 'puppetclassify'


cert_dir  = '/opt/puppet/share/puppet-dashboard/certs'
cert_name = 'pe-internal-dashboard'
auth_info = {
  'ca_certificate_path' => "#{cert_dir}/ca_cert.pem",
  'certificate_path'    => "#{cert_dir}/#{cert_name}.cert.pem",
  'private_key_path'    => "#{cert_dir}/#{cert_name}.private_key.pem"
}

puppetclassify = PuppetClassify.new(rest_api_url, auth_info)

environment = 'development'
puts puppetclassify.classes.get_environment_classes(environment)


This would run on the PE Master, and use the internal console certs for authentication. Here is the PE 3.7 NC REST API documentation for reference.


If you would like to continue using your current approach, then the search method of Puppet::Face[:resource_type, :current] will do a regular expression match. The 'find' method will instead return the first resource type with the specified name, which is why find('*') is returning no results. The search method accepts an optional hash through which you can specify the environment, and returns an array of Puppet::Resource::Type objects, which you can render as PSON and then parse to return a hash that you can work with. Here's a sample that does this in my PE 3.7.1 vagrant environment (I've created a 'dev' directory environment and installed the puppetlabs/ntp module there):

#!/opt/puppet/bin/ruby

require 'puppet/face'
require 'json'

Puppet.parse_config

resources =  Puppet::Face[:resource_type, :current].search('ntp', {:extra => { 'environment' => 'dev' }})

resources.each do |resource|
  puts JSON.parse(resource.render('pson'))
end



Let me know if you have any trouble with either of these approaches, or any questions.

Thanks,
Greg

Nan Liu

unread,
Feb 26, 2015, 3:27:53 PM2/26/15
to puppet-dev
This works great. Diving a bit deeper, I'm curious what exactly process the :extra option, because it's not obvious looking at the indirector code or the puppet face for resource_type.

Thanks for both solutions.

Nan
Reply all
Reply to author
Forward
0 new messages