Implementing the self.instances method

Puppet provides an additional mode of operation, that being the discovery of resources using the puppet resource command. The self.instances method should implement the return of any instances of a particular resource type that the provider is able to find on the underlying system.

The following example illustrates the use of the rpm -qa command to query for all packages installed on the underlying Red Hat system:

Puppet::Type.type(:mynewtype).provide(:yum) do   ...   def self.instances     pkgs = rpm('-qa','--qf','%{NAME} %{VERSION}-%{RELEASE}\n')     pkgs.split("\n").collect do |entry|       name, version = entry.split(' ', 2)       new( :name => name,         :ensure => :present,         :version => version       )     end   end   ... end

Each resource ...

Get Mastering Puppet 5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.