Updated:
I am trying to teach myself how to write Puppet custom types. I have looked at this documentation: https://docs.puppet.com/puppet/4.10/custom_types.html and https://docs.puppet.com/puppet/4.10/provider_development.html
Here is my contrived attempt to create a custom type that simple takes an array of strings and writes them to the file '/tmp/track-titles.txt'.
Here is my type code (modules/hello_world/lib/puppet/type/track_titles.rb):
# blah blah blah
Puppet::Type.newtype(:track_titles) do
@doc = "Create track title file."
ensurable
newparam(:name) do
desc "Mandaorty paramteter name ."
end
newproperty(:tracks) do
desc "an arrary of strings"
end
end
Here is my provider code: (modules/hello_world/lib/puppet/provider/track_titles.rb)
Puppet::Type.type(:track_titles).provide(:foo) do
desc "contrived example."
def create
filename = @resource[:name]
tracks.each do |t|
system ( "echo #{t} >> #{filename}" )
end
end
def destroy
File.unlink(@resource[:name])
end
def exists?
File.exists?(@resource[:name]))
end
end
Here is my puppet module that uses the above: (modules/hello_world/manifests/init.pp)
class hello_world (
$msg = 'Hello World',
$track_titles = ['one','two','three'],
) {
# notify { $msg: }
track_titles { '/tmp/track-titles.txt':
tracks => $track_titles,
}
}
I execute this code like so:
$ puppet apply \
> --modulepath=/home/red/PUPPET/modules \
> --hiera_config=/home/red/PUPPET/hiera.yaml \
> -e 'include hello_world'
And this is the output I get:
Notice: Compiled catalog for localhost in environment production in 0.06 seconds
Error: /Stage[main]/Hello_world/Track_titles[/tmp/track-titles.txt]: Could not evaluate: No ability to determine if track_titles exists
Notice: Finished catalog run in 0.82 seconds
What am I doing wrong. Also there are part of the provider code I don't get like:
Puppet::Type.type(:track_titles).provide(:ruby) do
What is this .provide(:ruby) all about?
Please help :)
@resource[:name]is going to resolve correctly in your provider code. To learn what providers are conceptually, check out Peter's great answer over here: stackoverflow.com/questions/41781030/…. Also, check this out: garylarizza.com/blog/2013/11/25/fun-with-providers. Note Gary's article is for Puppet 3, but still relevant. - Matt Schuchardtrack_titlesis supposed to fall in this category. - John Bollinger