1

I am reading about how to create custom types and providers in Puppet.
But I am getting the error:

Error: Could not autoload puppet/provider/createfile/ruby: undefined method `[]' for nil:NilClass

when running the below code:

mymodule/lib/puppet/type/filecreate.rb

require 'fileutils'

Puppet::Type.newtype(:filecreate) do
  ensurable do
    defaultvalues
    defaultto :present
  end

  @doc = "Create a file."

  newproperty(:name, :namevar => true) do
    desc "The name of the file"
  end

  newproperty(:path) do
    desc "The path of the file"
  end
end

mymodule/lib/puppet/provider/filecreate/ruby.rb

require 'fileutils'

Puppet::Type.type(:filecreate).provide(:ruby) do
  desc "create file.."

  puts resource[:name]  # this line does not seem to work, why?
  puts resource[:path]  # this line does not seem to work, why?

  def create
    puts "create file..."
    puts resource[:name]
  end

 def destroy
    puts ("destroy file...")
    FileUtils.rm resource[:path]+resource[:name]
  end

  # Exit method never seems to be called
  def exists?
    puts "is method beeing called???"
    File.exists?(resource[:path])
  end
end

I guess the way of fetching the parameter values, puts resource[:name] not is correct. So how can I fetch the filename file.txt declared as the namevar for my custom type filecreate (see below)?

Also, method exists does not seem to be called. Why?

And my init.pp contains this simple code:

class myclass {
  filecreate{'file.txt':
    ensure => present,
    path   => '/home/myuser/',
  }
}
Rox
  • 2,647
  • 15
  • 50
  • 85

1 Answers1

1

Your puts calls do not work because you try and access an instance attribute (resource) on the class level. It makes no semantic sense to access the values in this context. Remove those calls.

Generally, it is better to use Puppet.debug instead of puts to collect this kind of information.

To find out where such errors come from, call puppet with the --trace option.

Felix Frank
  • 8,125
  • 1
  • 23
  • 30
  • Thanks for you input! I removed the `puts` but the my Puppet installation seemed to be corrupted, so I reinstalled the whole thing and now it works. :) – Rox Aug 03 '15 at 14:00